CF1114F Please, another Queries on Array? [线段树,状压表示]

无脑线段树,告辞。

// powered by c++11
// by Isaunoya
#include <bits/stdc++.h>

#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)

using namespace std;
using db = double;
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;

#define pii pair<int, int>
#define fir first
#define sec second

template <class T>

void cmax(T& x, const T& y) {
  if (x < y) x = y;
}

template <class T>

void cmin(T& x, const T& y) {
  if (x > y) x = y;
}

#define all(v) v.begin(), v.end()
#define sz(v) ((int)v.size())
#define pb emplace_back

template <class T>

void sort(vector<T>& v) {
  sort(all(v));
}

template <class T>

void reverse(vector<T>& v) {
  reverse(all(v));
}

template <class T>

void unique(vector<T>& v) {
  sort(all(v)), v.erase(unique(all(v)), v.end());
}

void reverse(string& s) { reverse(s.begin(), s.end()); }

const int io_size = 1 << 23 | 233;
const int io_limit = 1 << 22;
struct io_in {
  char ch;
#ifndef __WIN64
  char getchar() {
    static char buf[io_size], *p1 = buf, *p2 = buf;

    return (p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, io_size, stdin), p1 == p2) ? EOF : *p1++;
  }
#endif
  io_in& operator>>(char& c) {
    for (c = getchar(); isspace(c); c = getchar())
      ;

    return *this;
  }
  io_in& operator>>(string& s) {
    for (s.clear(); isspace(ch = getchar());)
      ;

    if (!~ch) return *this;

    for (s = ch; !isspace(ch = getchar()) && ~ch; s += ch)
      ;

    return *this;
  }

  io_in& operator>>(char* str) {
    char* cur = str;
    while (*cur) *cur++ = 0;

    for (cur = str; isspace(ch = getchar());)
      ;
    if (!~ch) return *this;

    for (*cur = ch; !isspace(ch = getchar()) && ~ch; *++cur = ch)
      ;

    return *++cur = 0, *this;
  }

  template <class T>

  void read(T& x) {
    bool f = 0;
    while ((ch = getchar()) < 48 && ~ch) f ^= (ch == 45);

    x = ~ch ? (ch ^ 48) : 0;
    while ((ch = getchar()) > 47) x = x * 10 + (ch ^ 48);
    x = f ? -x : x;
  }

  io_in& operator>>(int& x) { return read(x), *this; }

  io_in& operator>>(ll& x) { return read(x), *this; }

  io_in& operator>>(uint& x) { return read(x), *this; }

  io_in& operator>>(ull& x) { return read(x), *this; }

  io_in& operator>>(db& x) {
    read(x);
    bool f = x < 0;
    x = f ? -x : x;
    if (ch ^ '.') return *this;

    double d = 0.1;
    while ((ch = getchar()) > 47) x += d * (ch ^ 48), d *= .1;
    return x = f ? -x : x, *this;
  }
} in;

struct io_out {
  char buf[io_size], *s = buf;
  int pw[233], st[233];

  io_out() {
    set(7);
    rep(i, pw[0] = 1, 9) pw[i] = pw[i - 1] * 10;
  }

  ~io_out() { flush(); }

  void io_chk() {
    if (s - buf > io_limit) flush();
  }

  void flush() { fwrite(buf, 1, s - buf, stdout), fflush(stdout), s = buf; }

  io_out& operator<<(char c) { return *s++ = c, *this; }

  io_out& operator<<(string str) {
    for (char c : str) *s++ = c;
    return io_chk(), *this;
  }

  io_out& operator<<(char* str) {
    char* cur = str;
    while (*cur) *s++ = *cur++;
    return io_chk(), *this;
  }

  template <class T>

  void write(T x) {
    if (x < 0) *s++ = '-', x = -x;

    do {
      st[++st[0]] = x % 10, x /= 10;
    } while (x);

    while (st[0]) *s++ = st[st[0]--] ^ 48;
  }

  io_out& operator<<(int x) { return write(x), io_chk(), *this; }

  io_out& operator<<(ll x) { return write(x), io_chk(), *this; }

  io_out& operator<<(uint x) { return write(x), io_chk(), *this; }

  io_out& operator<<(ull x) { return write(x), io_chk(), *this; }

  int len, lft, rig;

  void set(int _length) { len = _length; }

  io_out& operator<<(db x) {
    bool f = x < 0;
    x = f ? -x : x, lft = x, rig = 1. * (x - lft) * pw[len];
    return write(f ? -lft : lft), *s++ = '.', write(rig), io_chk(), *this;
  }
} out;
#define int long long

template <int sz, int mod>

struct math_t {
  math_t() {
    fac.resize(sz + 1), ifac.resize(sz + 1);
    rep(i, fac[0] = 1, sz) fac[i] = fac[i - 1] * i % mod;

    ifac[sz] = inv(fac[sz]);
    Rep(i, sz - 1, 0) ifac[i] = ifac[i + 1] * (i + 1) % mod;
  }

  vector<int> fac, ifac;

  int qpow(int x, int y) {
    int ans = 1;
    for (; y; y >>= 1, x = x * x % mod)
      if (y & 1) ans = ans * x % mod;
    return ans;
  }

  int inv(int x) { return qpow(x, mod - 2); }

  int C(int n, int m) {
    if (n < 0 || m < 0 || n < m) return 0;
    return fac[n] * ifac[m] % mod * ifac[n - m] % mod;
  }
};

int gcd(int x, int y) { return !y ? x : gcd(y, x % y); }
int lcm(int x, int y) { return x * y / gcd(x, y); }

int n, _, cnt = 0;
const int maxn = 4e5 + 54;
int a[maxn];
int bit[maxn << 2], tag[maxn << 2];
int vis[333], qwq[333], rev[333];

void build(int l, int r, int p) {
  tag[p] = 0;
  if (l == r) {
    bit[p] = qwq[a[l]];
    return;
  }
  int mid = l + r >> 1;
  build(l, mid, p << 1);
  build(mid + 1, r, p << 1 | 1);
  bit[p] = bit[p << 1] | bit[p << 1 | 1];
}

void pushdown(int p) {
  if (!tag[p]) return;
  tag[p << 1] |= tag[p];
  tag[p << 1 | 1] |= tag[p];
  bit[p << 1] |= tag[p];
  bit[p << 1 | 1] |= tag[p];
  tag[p] = 0;
}

void upd(int a, int b, int l, int r, int p, int v) {
  if (a <= l && r <= b) {
    bit[p] |= v, tag[p] |= v;
    return;
  }
  pushdown(p);
  int mid = l + r >> 1;
  if (a <= mid) upd(a, b, l, mid, p << 1, v);
  if (b > mid) upd(a, b, mid + 1, r, p << 1 | 1, v);
  bit[p] = bit[p << 1] | bit[p << 1 | 1];
}

int qry(int a, int b, int l, int r, int p) {
  if (a <= l && r <= b) return bit[p];
  pushdown(p);
  int mid = l + r >> 1, ans = 0;
  if (a <= mid) ans |= qry(a, b, l, mid, p << 1);
  if (b > mid) ans |= qry(a, b, mid + 1, r, p << 1 | 1);
  return ans;
}

const int mod = 1e9 + 7;
math_t<0, mod> fuck;
char opt[233];
int mul(int x, int y) {
  x *= y;
  if (x >= mod) return x % mod;
  return x;
}

struct smt {
  int s[maxn << 2], tag[maxn << 2];

  void build(int l, int r, int p) {
    tag[p] = 1;
    if (l == r) {
      s[p] = a[l];
      return;
    }
    int mid = l + r >> 1;
    build(l, mid, p << 1);
    build(mid + 1, r, p << 1 | 1);
    s[p] = mul(s[p << 1], s[p << 1 | 1]);
  }

  void pushdown(int p, int l, int r) {
    if (tag[p] == 1) return;
    int mid = l + r >> 1;
    s[p << 1] = mul(s[p << 1], fuck.qpow(tag[p], mid - l + 1));
    s[p << 1 | 1] = mul(s[p << 1 | 1], fuck.qpow(tag[p], r - mid));
    tag[p << 1] = mul(tag[p << 1], tag[p]);
    tag[p << 1 | 1] = mul(tag[p << 1 | 1], tag[p]);
    tag[p] = 1;
  }

  void upd(int a, int b, int l, int r, int p, int v) {
    if (a <= l && r <= b) {
      tag[p] = mul(tag[p], v);
      s[p] = mul(s[p], fuck.qpow(v, r - l + 1));
      return;
    }
    pushdown(p, l, r);
    int mid = l + r >> 1;
    if (a <= mid) upd(a, b, l, mid, p << 1, v);
    if (b > mid) upd(a, b, mid + 1, r, p << 1 | 1, v);
    s[p] = mul(s[p << 1], s[p << 1 | 1]);
  }

  int qry(int a, int b, int l, int r, int p) {
    if (a <= l && r <= b) return s[p];
    pushdown(p, l, r);
    int mid = l + r >> 1, ans = 1;
    if (a <= mid) ans = mul(ans, qry(a, b, l, mid, p << 1));
    if (b > mid) ans = mul(ans, qry(a, b, mid + 1, r, p << 1 | 1));
    return ans;
  }
} smt;

signed main() {
  // code begin.
  rep(i, 2, 300) if (!vis[i]) {
    rev[++cnt] = mul(i - 1, fuck.inv(i));
    for (int j = i; j <= 300; j += i) vis[j] = 1, qwq[j] |= 1ll << cnt;
  }
  in >> n >> _;
  rep(i, 1, n) in >> a[i];
  build(1, n, 1), smt.build(1, n, 1);
  while (_--) {
    int l, r;
    in >> opt >> l >> r;
    if (*opt == 'M') {
      int x;
      in >> x;
      smt.upd(l, r, 1, n, 1, x), upd(l, r, 1, n, 1, qwq[x]);
    } else {
      int ansmul = smt.qry(l, r, 1, n, 1), ansbit = qry(l, r, 1, n, 1);
      int ans = ansmul;
      rep(i, 1, cnt) if (ansbit & (1ll << i)) ans = mul(ans, rev[i]);
      out << ans << '\n';
    }
  }
  return 0;
  // code end.
}
posted @ 2020-04-10 12:43  _Isaunoya  阅读(149)  评论(0编辑  收藏  举报