結果

問題 No.879 Range Mod 2 Query
ユーザー tko919tko919
提出日時 2020-09-29 23:03:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 300 ms / 3,000 ms
コード長 2,850 bytes
コンパイル時間 1,847 ms
コンパイル使用メモリ 175,672 KB
実行使用メモリ 17,944 KB
最終ジャッジ日時 2023-09-17 23:16:09
合計ジャッジ時間 6,303 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 289 ms
16,880 KB
testcase_12 AC 177 ms
16,824 KB
testcase_13 AC 219 ms
16,844 KB
testcase_14 AC 206 ms
17,600 KB
testcase_15 AC 200 ms
11,208 KB
testcase_16 AC 280 ms
17,528 KB
testcase_17 AC 289 ms
17,944 KB
testcase_18 AC 300 ms
17,552 KB
testcase_19 AC 278 ms
17,724 KB
testcase_20 AC 279 ms
17,932 KB
testcase_21 AC 292 ms
17,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end

template<typename M,typename N
   ,M (*f)(M,M),M (*g)(M,N),N (*h)(N,N)>
class LazySegmentTree{
   int sz,height; vector<M> data; vector<N> lazy;
   const M m1; const N n1;
   M ref(int k){return g(data[k],lazy[k]);}
   void recalc(int k){while(k>>=1)data[k]=f(ref(2*k),ref(2*k+1));}
   void thrust(int k){for(int i=height;i>0;i--)eval(k>>i);}
   void eval(int k){
      lazy[2*k]=h(lazy[2*k],lazy[k]); lazy[2*k+1]=h(lazy[2*k+1],lazy[k]);
      data[k]=ref(k); lazy[k]=n1;
   }
public:
   LazySegmentTree(int n,const M &m1,const N n1):m1(m1),n1(n1){
      sz=1,height=0; while(sz<n)sz<<=1,height++;
      data.assign(2*sz,m1); lazy.assign(2*sz,n1);
   }
   void build(vector<M> v){
      rep(i,0,v.size())data[i+sz]=v[i];
      for(int k=sz-1;k>0;k--)data[k]=f(data[2*k],data[2*k+1]);
   }
   void set(int a,M x){thrust(a+=sz); data[a]=x; recalc(a);}
   void update(int a,int b,N x){
      thrust(a+=sz); thrust(b+=sz-1);
      for(int l=a,r=b+1;l<r;l>>=1,r>>=1){
         if(l&1)lazy[l]=h(lazy[l],x),++l;
         if(r&1)--r,lazy[r]=h(lazy[r],x);
      }
      recalc(a); recalc(b);
   }
   M query(int a,int b){
      thrust(a+=sz); thrust(b+=sz-1);
      M L=m1,R=m1;
      for(int l=a,r=b+1;l<r;l>>=1,r>>=1){
         if(l&1)L=f(L,ref(l++));
         if(r&1)R=f(ref(--r),R);
      } return f(L,R);
   }
};

struct S{ll sum,ecnt,ocnt;};
struct T{ll state,x;};
S f(S a,S b){
   a.sum+=b.sum;
   a.ecnt+=b.ecnt;
   a.ocnt+=b.ocnt;
   return a;
}

S g(S a,T b){
   if(b.state==1)a.sum=a.ocnt;
   else if(b.state==2){
      swap(a.ocnt,a.ecnt);
      a.sum=a.ocnt;
   }
   a.sum+=b.x*(a.ecnt+a.ocnt);
   if(b.x&1)swap(a.ecnt,a.ocnt);
   return a;
}

T h(T a,T b){
   if(b.state){
      if((a.state==2) xor (a.x&1)){
         b.state^=3;
      }
      return b;
   }
   else{
      a.x+=b.x;
      return a;
   }
}

int main(){
   int n,q; cin>>n>>q;
   LazySegmentTree<S,T,f,g,h> seg(n,S{0,1,0},T{0,0});
   vector<S> a(n);
   rep(i,0,n){
      int x; cin>>x;
      a[i]=S{x,!(x&1),(x&1)};
   }
   seg.build(a);

   rep(i,0,q){
      int t; cin>>t;
      if(t==1){
         int lb,rb; cin>>lb>>rb; lb--;
         seg.update(lb,rb,T{1,0});
      }
      if(t==2){
         int lb,rb,x; cin>>lb>>rb>>x; lb--;
         seg.update(lb,rb,T{0,x});
      }
      if(t==3){
         int lb,rb; cin>>lb>>rb; lb--;
         ll res=seg.query(lb,rb).sum;
         cout<<res<<endl;
      }
   }
   return 0;
}
0