結果
| 問題 |
No.879 Range Mod 2 Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-09-06 23:19:24 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 367 ms / 3,000 ms |
| コード長 | 2,782 bytes |
| コンパイル時間 | 2,017 ms |
| コンパイル使用メモリ | 179,256 KB |
| 実行使用メモリ | 13,708 KB |
| 最終ジャッジ日時 | 2024-06-24 22:24:04 |
| 合計ジャッジ時間 | 6,437 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 21 |
ソースコード
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
#define int long long
struct st{
ll sum,cnt;
};
struct lazynode{
ll add;
bool mod;
ll add2;
};
enum SegtreeType{
CUSTOM,
RAQ_RMQ,
RAQ_RSQ,
RUQ_RMQ,
RUQ_RSQ,
RMQ_RMQ
};
template<SegtreeType ty,class T>
class LazySegtree{
int n;
vector<T>dat;
vector<lazynode>lazy;
T INIT;
T E0;
lazynode E1;
function<T(T,lazynode,ll)>F0;
function<lazynode(lazynode,lazynode)>F1;
function<T(T,T)>F2;
public:
LazySegtree(int n_,T INIT,T E0,lazynode E1
,function<T(T,lazynode,ll)>F0,function<lazynode(lazynode,lazynode)>F1,function<T(T,T)>F2):
INIT(INIT),E0(E0),E1(E1),F0(F0),F1(F1),F2(F2){
n=1;while(n<n_)n<<=1;
dat=vector<T>(2*n);
lazy=vector<lazynode>(2*n);
for(int i=0;i<2*n-1;i++){
dat[i]=INIT;
lazy[i]=E1;
}
}
inline void push(int k,int s){
if(!lazy[k].add&&!lazy[k].mod&&!lazy[k].add2)return;
dat[k]=F0(dat[k],lazy[k],s);
if(k<n-1){
lazy[k*2+1]=F1(lazy[k*2+1],lazy[k]);
lazy[k*2+2]=F1(lazy[k*2+2],lazy[k]);
}
lazy[k]=E1;
}
inline void upnode(int k){
dat[k]=F2(dat[k*2+1],dat[k*2+2]);
}
inline void update(int a,int b,lazynode x,int k,int l,int r){
push(k,r-l);
if(r<=a||b<=l)return;
if(a<=l&&r<=b){
lazy[k]=F1(lazy[k],x);
push(k,r-l);
return;
}
update(a,b,x,k*2+1,l,(l+r)/2);
update(a,b,x,k*2+2,(l+r)/2,r);
upnode(k);
}
inline T query(int a,int b,int k,int l,int r){
push(k,r-l);
if(b<=l||r<=a)return E0;
if(a<=l&&r<=b)return dat[k];
T lb=query(a,b,k*2+1,l,(l+r)/2);
T rb=query(a,b,k*2+2,(l+r)/2,r);
upnode(k);
return F2(lb,rb);
}
inline void update(int a,int b,lazynode x){
update(a,b,x,0,0,n);
}
inline void update(int a,lazynode x){
update(a,a+1,x);
}
inline T query(int a,int b){
return query(a,b,0,0,n);
}
inline T query(int a){
return query(a,a+1);
}
};
signed main(){
int n,q;cin>>n>>q;
LazySegtree<CUSTOM,st>seg(n,{0,0},{0,0},{0,0,0},
[](st a,lazynode b,ll s){
a.sum+=b.add*s;
if(b.add%2==1)a.cnt=s-a.cnt;
if(b.mod)a.sum=a.cnt;
a.sum+=b.add2*s;
if(b.add2%2==1)a.cnt=s-a.cnt;
return a;
},
[](lazynode a,lazynode b){
if(b.mod){
(a.add+=a.add2+b.add)%=2;
a.mod=true;
a.add2=b.add2;
}
else{
if(a.mod)(a.add2+=b.add);
else (a.add+=b.add);
}
return a;
},
[](st a,st b){
return (st){a.sum+b.sum,a.cnt+b.cnt};
}
);
rep(i,n){
int a;scanf("%lld",&a);
seg.update(i,{a,0,0});
}
rep(i,q){
int t;scanf("%lld",&t);
if(t==1){
int l,r;scanf("%lld%lld",&l,&r);l--;
seg.update(l,r,{0,1,0});
}
else if(t==2){
int l,r,x;scanf("%lld%lld%lld",&l,&r,&x);l--;
seg.update(l,r,{x,0,0});
}
else{
int l,r;scanf("%lld%lld",&l,&r);l--;
printf("%lld\n",seg.query(l,r).sum);
}
}
}