結果

問題 No.879 Range Mod 2 Query
ユーザー tjaketjake
提出日時 2019-09-07 11:47:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 359 ms / 3,000 ms
コード長 4,558 bytes
コンパイル時間 1,084 ms
コンパイル使用メモリ 88,352 KB
実行使用メモリ 13,728 KB
最終ジャッジ日時 2023-09-08 14:12:43
合計ジャッジ時間 5,961 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,628 KB
testcase_01 AC 4 ms
11,560 KB
testcase_02 AC 5 ms
11,648 KB
testcase_03 AC 4 ms
9,496 KB
testcase_04 AC 5 ms
11,556 KB
testcase_05 AC 4 ms
9,516 KB
testcase_06 AC 3 ms
9,824 KB
testcase_07 AC 5 ms
11,696 KB
testcase_08 AC 5 ms
11,576 KB
testcase_09 AC 4 ms
11,552 KB
testcase_10 AC 4 ms
9,852 KB
testcase_11 AC 357 ms
13,456 KB
testcase_12 AC 214 ms
13,436 KB
testcase_13 AC 258 ms
13,432 KB
testcase_14 AC 240 ms
13,596 KB
testcase_15 AC 236 ms
12,828 KB
testcase_16 AC 338 ms
13,728 KB
testcase_17 AC 354 ms
13,600 KB
testcase_18 AC 359 ms
13,552 KB
testcase_19 AC 324 ms
13,540 KB
testcase_20 AC 317 ms
13,704 KB
testcase_21 AC 344 ms
13,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<functional>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cassert>
#include<ctime>
using namespace std;

#define mind(a,b) (a>b?b:a)
#define maxd(a,b) (a>b?a:b)
#define absd(x) (x<0?-(x):x)
#define pow2(x) ((x)*(x))
#define rep(i,n) for(int i=0; i<n; ++i)
#define repr(i,n) for(int i=n-1; i>=0; --i)
#define repl(i,s,n) for(int i=s; i<=n; ++i)
#define replr(i,s,n) for(int i=n; i>=s; --i)
#define repf(i,s,n,j) for(int i=s; i<=n; i+=j)
#define repe(e,obj) for(auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X,Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i,n,val) for(int i=0; i<n; ++i) cout << val << " "; cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 1000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define P_TYPE ll
typedef pair<P_TYPE, P_TYPE> P;
typedef pair<P, P_TYPE> PI;
typedef pair<P_TYPE, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P> > pvqueue;

#define N (1 << 17)

class SegmentTree {
  ll data[2*N], lazy[2*N], cnt0[2*N], cnt1[2*N];
  bool flag[2*N];
  int p[2*N];
  int n0, n;

  void propagate(int k) {
    if(k >= n0-1) return;
    if(p[k]) {
      swap(cnt0[2*k+1], cnt1[2*k+1]);
      p[2*k+1] ^= 1;
      swap(cnt0[2*k+2], cnt1[2*k+2]);
      p[2*k+2] ^= 1;
      p[k] = 0;
    }
    if(flag[k]) {
      data[2*k+1] = cnt1[2*k+1];
      data[2*k+2] = cnt1[2*k+2];
      if(lazy[2*k+1] & 1) p[2*k+1] ^= 1;
      if(lazy[2*k+2] & 1) p[2*k+2] ^= 1;
      lazy[2*k+1] = lazy[2*k+2] = 0;

      flag[2*k+1] = flag[2*k+2] = true;
      flag[k] = false;
    }
    if(lazy[k] != 0) {
      if(lazy[k] & 1) {
        swap(cnt0[2*k+1], cnt1[2*k+1]);
        swap(cnt0[2*k+2], cnt1[2*k+2]);
      }
      data[2*k+1] += lazy[k] * (cnt0[2*k+1] + cnt1[2*k+1]);
      data[2*k+2] += lazy[k] * (cnt0[2*k+2] + cnt1[2*k+2]);

      lazy[2*k+1] += lazy[k];
      lazy[2*k+2] += lazy[k];

      lazy[k] = 0;
    }
  }
  void build(int k) {
    cnt0[k] = cnt0[2*k+1] + cnt0[2*k+2];
    cnt1[k] = cnt1[2*k+1] + cnt1[2*k+2];
    data[k] = data[2*k+1] + data[2*k+2];
  }

  void _set(int a, int b, int k, int l, int r) {
    propagate(k);
    if(b <= l || r <= a) {
      return;
    }
    if(a <= l && r <= b) {
      flag[k] = true;
      data[k] = cnt1[k];
      lazy[k] = 0;
      return;
    }
    _set(a, b, 2*k+1, l, (l+r)/2);
    _set(a, b, 2*k+2, (l+r)/2, r);
    build(k);
  }

  void _add(ll x, int a, int b, int k, int l, int r) {
    propagate(k);
    if(b <= l || r <= a) {
      return;
    }
    if(a <= l && r <= b) {
      if(x & 1) {
        swap(cnt0[k], cnt1[k]);
      }
      data[k] += x * (cnt0[k] + cnt1[k]);
      lazy[k] += x;
      return;
    }
    _add(x, a, b, 2*k+1, l, (l+r)/2);
    _add(x, a, b, 2*k+2, (l+r)/2, r);
    build(k);
  }

  ll _query(int a, int b, int k, int l, int r) {
    propagate(k);
    if(b <= l || r <= a) {
      return 0;
    }
    if(a <= l && r <= b) {
      return data[k];
    }
    ll lv = _query(a, b, 2*k+1, l, (l+r)/2);
    ll rv = _query(a, b, 2*k+2, (l+r)/2, r);
    build(k);
    return lv + rv;
  }

public:
  SegmentTree(int n, ll *a) {
    n0 = 1;
    while(n0 < n) n0 <<= 1;
    for(int i=0; i<n; ++i) {
      data[n0-1+i] = a[i];
      if(a[i] % 2) {
        cnt1[n0-1+i] = 1; cnt0[n0-1+i] = 0;
      } else {
        cnt1[n0-1+i] = 0; cnt0[n0-1+i] = 1;
      }
    }
    for(int i=n; i<n0; ++i) {
      data[n0-1+i] = 0;
      cnt0[n0-1+i] = cnt1[n0-1+i] = 0;
    }
    for(int i=0; i<2*n0-1; ++i) lazy[i] = 0, flag[i] = false, p[i] = 0;
    for(int i=n0-2; i>=0; --i) build(i);
  }

  void set(int l, int r) {
    _set(l, r, 0, 0, n0);
  }

  void add(int l, int r, ll x) {
    _add(x, l, r, 0, 0, n0);
  }

  ll query(int l, int r) {
    return _query(l, r, 0, 0, n0);
  }
};

ll a[N];

int main() {
  int n, q;
  cin >> n >> q;
  rep(i, n) cin >> a[i];
  SegmentTree st(n, a);
  while(q--) {
    int t, l, r; ll x;
    cin >> t >> l >> r;
    switch(t) {
      case 1:
        st.set(l-1, r);
        break;
      case 2:
        cin >> x;
        st.add(l-1, r, x);
        break;
      case 3:
        cout << st.query(l-1, r) << endl;
        break;
    }
  }
  return 0;
}
0