結果

問題 No.749 クエリ全部盛り
ユーザー risujirohrisujiroh
提出日時 2018-10-30 21:35:02
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 331 ms / 3,000 ms
コード長 4,146 bytes
コンパイル時間 1,841 ms
コンパイル使用メモリ 173,484 KB
実行使用メモリ 81,300 KB
最終ジャッジ日時 2023-08-12 10:36:09
合計ジャッジ時間 4,881 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 11 ms
4,376 KB
testcase_11 AC 11 ms
4,376 KB
testcase_12 AC 11 ms
4,376 KB
testcase_13 AC 11 ms
4,380 KB
testcase_14 AC 11 ms
4,376 KB
testcase_15 AC 331 ms
81,160 KB
testcase_16 AC 328 ms
81,300 KB
testcase_17 AC 331 ms
81,124 KB
testcase_18 AC 322 ms
81,184 KB
testcase_19 AC 331 ms
81,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
template<class T = int> using V  = vector<T>;
template<class T = int> using VV = V< V<T> >;
template<class T> void assign(V<T>& v, int n, const T& a = T()) { v.assign(n, a); }
template<class T, class... Args> void assign(V<T>& v, int n, const Args&... args) { v.resize(n); for (auto&& e : v) assign(e, args...); }


#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif
int read_int(){int c,r;while((c=getchar())<48);r=c-48;while((c=getchar())>47)r=r*10+c-48;return r;}
void write_int(int x){int b[10],*p=b;do*p++=48+x%10,x/=10;while(x);do putchar(*--p);while(p>b);}

constexpr lint mod = 1e9 + 7;
inline lint emod(lint a, lint p = mod) { return (a % p + p) % p; }

struct Monoid {
  using T = struct { lint v, fib; };
  static T op(const T& lhs, const T& rhs) { return {(lhs.v + rhs.v) % mod, (lhs.fib + rhs.fib) % mod}; }
  static constexpr T e() { return {0, 0}; }
};

template<class Monoid> struct MonoidAct {
  using T = struct { lint a, b, c; };
  static void ap(const T& f, typename Monoid::T& a, int len) { a.v = (f.a * a.v + f.b * len + f.c * a.fib) % mod; }
  static void cp(const T& f, T& g) { (g.a *= f.a) %= mod, g.b = (f.a * g.b + f.b) % mod, g.c = (f.a * g.c + f.c) % mod; }
  static constexpr T id() { return {1, 0, 0}; }
};

template<class Monoid, class MonoidAct> struct SegmentTree {
  using Tv = typename Monoid::T;
  using Ta = typename MonoidAct::T;

  const int n;
  V<Tv> val;
  V<Ta> act;
  V<> len;

  SegmentTree(int n) : n(n), val(2 * n, Monoid::e()), act(n, MonoidAct::id()), len(2 * n, 1) {
    for (int i = n - 1; i > 0; --i) len[i] = len[2 * i] + len[2 * i + 1];
  }
  template<class Itr> SegmentTree(Itr first, Itr last) : n(distance(first, last)), val(2 * n, Monoid::e()), act(n, MonoidAct::id()), len(2 * n, 1) {
    copy(first, last, next(begin(val), n));
    build();
    for (int i = n - 1; i > 0; --i) len[i] = len[2 * i] + len[2 * i + 1];
  }

  void build() {
    for (int i = n - 1; i > 0; --i) val[i] = Monoid::op(val[2 * i], val[2 * i + 1]);
  }

  Tv get(int l, int r) {
    _push(l, r);
    Tv resl = Monoid::e(), resr = Monoid::e();
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1) resl = Monoid::op(resl, val[l++]);
      if (r & 1) resr = Monoid::op(val[--r], resr);
    }
    return Monoid::op(resl, resr);
  }
  Tv get(int i) { return get(i, i + 1); }

  void push() {
    for (int i = 1; i < n; ++i) _push(i);
  }

  void set(int l, int r, const Ta& f) {
    _push(l, r);
    for (int i = l + n, j = r + n; i < j; i >>= 1, j >>= 1) {
      if (i & 1) _ap(f, i++);
      if (j & 1) _ap(f, --j);
    }
    for (l += n; !(l & 1); l >>= 1);
    while ((l >>= 1) > 0) val[l] = Monoid::op(val[2 * l], val[2 * l + 1]);
    for (r += n; !(r & 1); r >>= 1);
    while ((r >>= 1) > 0) val[r] = Monoid::op(val[2 * r], val[2 * r + 1]);
  }
  void set(int i, const Tv& a) {
    _push(i, i + 1);
    for (val[i += n] = a, i >>= 1; i > 0; i >>= 1) val[i] = Monoid::op(val[2 * i], val[2 * i + 1]);
  }

  void _ap(const Ta& f, int i) {
    MonoidAct::ap(f, val[i], len[i]);
    if (i < n) MonoidAct::cp(f, act[i]);
  }

  void _push(int i) {
    _ap(act[i], 2 * i);
    _ap(act[i], 2 * i + 1);
    act[i] = MonoidAct::id();
  }
  void _push(int l, int r) {
    for (int hl = __lg(l + n), hr = __lg(r - 1 + n); hr > 0; --hl, --hr) {
      int al = l + n >> hl, ar = r - 1 + n >> hr;
      if (al < n) _push(al);
      if (ar != al) _push(ar);
    }
  }
};

int main() {
  int n = read_int(), Q = read_int();
  V<Monoid::T> v(n);
  v[1].fib = 1;
  for (int i = 2; i < n; ++i) v[i].fib = (v[i - 1].fib + v[i - 2].fib) % mod;
  SegmentTree< Monoid, MonoidAct<Monoid> > st(begin(v), end(v));
  for (int i = 0; i < Q; ++i) {
    int q = read_int(), l = read_int(), r = read_int() + 1, k = read_int();
    switch (q) {
      case 0: write_int(emod(k * st.get(l, r).v)), putchar('\n'); break;
      case 1: st.set(l, r, {0, k, 0}); break;
      case 2: st.set(l, r, {1, k, 0}); break;
      case 3: st.set(l, r, {k, 0, 0}); break;
      case 4: st.set(l, r, {1, 0, k}); break;
    }
  }
}
0