結果
| 問題 | No.879 Range Mod 2 Query |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-09-07 20:55:23 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 391 ms / 3,000 ms |
| コード長 | 3,944 bytes |
| 記録 | |
| コンパイル時間 | 2,051 ms |
| コンパイル使用メモリ | 181,040 KB |
| 実行使用メモリ | 13,732 KB |
| 最終ジャッジ日時 | 2024-06-27 04:24:28 |
| 合計ジャッジ時間 | 6,785 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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;
enum SegtreeType{
CUSTOM,
RAQ_RMQ,
RAQ_RSQ,
RUQ_RMQ,
RUQ_RSQ,
RMQ_RMQ
};
template<SegtreeType ty,class T,class U>
class LazySegtree{
int n;
vector<T>dat;
vector<U>lazy;
T INIT;
T E0;
U E1;
function<T(T,U,int)>F0;
function<U(U,U)>F1;
function<T(T,T)>F2;
public:
LazySegtree(int n_,T INIT,T E0,U E1
,function<T(T,U,int)>F0,function<U(U,U)>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<U>(2*n);
for(int i=0;i<2*n-1;i++){
dat[i]=INIT;
lazy[i]=E1;
}
}
LazySegtree(int n_,T INIT_=-2){
switch(ty){
case RAQ_RMQ:
INIT=(INIT_==-2?0:INIT_);
E0=numeric_limits<T>::max();
E1=0;
F0=[](T a,T b,int s){return a+b;};
F1=[](T a,T b){return a+b;};
F2=[](T a,T b){return min(a,b);};
break;
case RAQ_RSQ:
INIT=(INIT_==-2?0:INIT_);
E0=0;
E1=0;
F0=[](T a,T b,int s){return a+b*s;};
F1=[](T a,T b){return a+b;};
F2=[](T a,T b){return a+b;};
break;
case RUQ_RMQ:
INIT=(INIT_==-2?numeric_limits<T>::max():INIT_);
E0=numeric_limits<T>::max();
E1=numeric_limits<T>::min();
F0=[](T a,T b,int s){return b;};
F1=[](T a,T b){return b;};
F2=[](T a,T b){return min(a,b);};
break;
case RUQ_RSQ:
INIT=(INIT_==-2?0:INIT_);
E0=0;
E1=numeric_limits<T>::min();
F0=[](T a,T b,int s){return b*s;};
F1=[](T a,T b){return b;};
F2=[](T a,T b){return a+b;};
break;
case RMQ_RMQ:
INIT=(INIT_==-2?numeric_limits<T>::max():INIT_);
E0=numeric_limits<T>::max();
E1=numeric_limits<T>::max();
F0=[](T a,T b,int s){return min(a,b);};
F1=[](T a,T b){return min(a,b);};
F2=[](T a,T b){return min(a,b);};
break;
}
n=1;while(n<n_)n<<=1;
dat=lazy=vector<T>(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]==E1)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,U 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,U x){
update(a,b,x,0,0,n);
}
inline void update(int a,U 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);
}
};
struct st{
ll sum,cnt;
};
struct lazynode{
ll add;
bool mod;
ll add2;
};
bool operator==(lazynode&a,lazynode&b){
return a.add==b.add&&a.mod==b.mod&&a.add2==b.add2;
}
signed main(){
int n,q;cin>>n>>q;
LazySegtree<CUSTOM,st,lazynode>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("%d",&a);
seg.update(i,{a,0,0});
}
rep(i,q){
int t;scanf("%d",&t);
if(t==1){
int l,r;scanf("%d%d",&l,&r);l--;
seg.update(l,r,{0,1,0});
}
else if(t==2){
int l,r,x;scanf("%d%d%d",&l,&r,&x);l--;
seg.update(l,r,{x,0,0});
}
else{
int l,r;scanf("%d%d",&l,&r);l--;
printf("%lld\n",seg.query(l,r).sum);
}
}
}