結果
| 問題 |
No.876 Range Compress Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-09-06 21:50:09 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 237 ms / 2,000 ms |
| コード長 | 2,362 bytes |
| コンパイル時間 | 1,876 ms |
| コンパイル使用メモリ | 178,720 KB |
| 実行使用メモリ | 11,648 KB |
| 最終ジャッジ日時 | 2024-06-24 17:35:48 |
| 合計ジャッジ時間 | 4,994 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 18 |
ソースコード
#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
const int MOD=1000000007;
const int INF=0x3f3f3f3f;
const ll INFL=0x3f3f3f3f3f3f3f3f;
struct st{
int s,l,r;
};
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<int>lazy;
T INIT;
T E0;
int E1;
function<T(T,int,int)>F0;
function<int(int,int)>F1;
function<T(T,T)>F2;
public:
LazySegtree(int n_,T INIT,T E0,int E1
,function<T(T,int,int)>F0,function<int(int,int)>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<int>(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,int 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,int x){
update(a,b,x,0,0,n);
}
inline void update(int a,int 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,{1,0,0},{0,-1,-1},0,
[](st a,int b,int s){
return (st){a.s,a.l+b,a.r+b};
},
[](int a,int b){
return a+b;
},
[](st a,st b){
return (st){a.s+b.s-(a.r!=-1&&a.r==b.l),a.l,b.r};
}
);
rep(i,n){
int a;scanf("%lld",&a);
seg.update(i,a);
}
rep(i,q){
int t;scanf("%lld",&t);
if(t==1){
int l,r,x;scanf("%lld%lld%lld",&l,&r,&x);l--;
seg.update(l,r,x);
}
else{
int l,r;scanf("%lld%lld",&l,&r);l--;
printf("%lld\n",seg.query(l,r).s);
}
}
}