QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#168404 | #6856. Easy problem I | C1924huangjiaxu | AC ✓ | 1700ms | 71988kb | C++14 | 2.1kb | 2023-09-08 14:10:57 | 2023-09-08 14:10:58 |
Judging History
answer
#include<stdio.h>
#include<algorithm>
using namespace std;
const int N=5e5+5;
typedef long long ll;
int n,q,a[N],T;
#define L k<<1
#define R k<<1|1
struct seg{
ll sum[N<<2],tg[N<<2],ln[N<<2],mn[N<<2];
bool rv[N<<2];
void pushup(int k){
ln[k]=ln[L]+ln[R],sum[k]=sum[L]+sum[R],mn[k]=min(mn[L],mn[R]);
}
void pushrv(int k){
rv[k]^=1,tg[k]=-tg[k],sum[k]=-sum[k];
}
void pushtg(int k,ll v){
sum[k]+=v*ln[k],tg[k]+=v,mn[k]+=v;
}
void pushdown(int k){
if(rv[k]){
pushrv(L);
pushrv(R);
rv[k]=false;
}
if(tg[k]){
pushtg(L,tg[k]);
pushtg(R,tg[k]);
tg[k]=0;
}
}
void build(int k,int l,int r){
if(l==r){
ln[k]=1,sum[k]=mn[k]=a[l];
return;
}
int mid=l+r>>1;
build(L,l,mid),build(R,mid+1,r);
pushup(k);
}
void modify(int k,int l,int r,int x,int y,int v,bool ty){
if(x<=l&&r<=y){
if(ty)pushrv(k),pushtg(k,v);
else pushtg(k,-v);
return;
}
int mid=l+r>>1;
pushdown(k);
if(x<=mid)modify(L,l,mid,x,y,v,ty);
if(y>mid)modify(R,mid+1,r,x,y,v,ty);
pushup(k);
}
ll query(int k,int l,int r,int x,int y){
if(x<=l&&r<=y)return sum[k];
int mid=l+r>>1;
pushdown(k);
ll res=0;
if(x<=mid)res+=query(L,l,mid,x,y);
if(y>mid)res+=query(R,mid+1,r,x,y);
return res;
}
void clear(){
for(int i=1;i<n<<2;++i)sum[i]=ln[i]=mn[i]=tg[i]=rv[i]=0;
}
}T1,T2;
void clear(int k,int l,int r,int x,int y,int v){
if(T1.mn[k]>2*v)return;
if(l==r){
T2.sum[k]=abs(T1.sum[k]-v),T2.ln[k]=1;
T1.sum[k]=T1.ln[k]=0,T1.mn[k]=1e18;
return;
}
T1.pushdown(k),T2.pushdown(k);
int mid=l+r>>1;
if(x<=mid)clear(L,l,mid,x,y,v);
if(y>mid)clear(R,mid+1,r,x,y,v);
T1.pushup(k),T2.pushup(k);
}
void solve(){
scanf("%d%d",&n,&q);
T1.clear(),T2.clear();
for(int i=1;i<=n;++i)scanf("%d",&a[i]);
T1.build(1,1,n);
for(int i=1,op,l,r,x;i<=q;++i){
scanf("%d%d%d",&op,&l,&r);
if(op==1){
scanf("%d",&x);
T2.modify(1,1,n,l,r,x,true);
clear(1,1,n,l,r,x);
T1.modify(1,1,n,l,r,x,false);
}else printf("%lld\n",T2.query(1,1,n,l,r)+T1.query(1,1,n,l,r));
}
}
int main(){
scanf("%d",&T);
while(T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1700ms
memory: 71988kb
input:
5 200000 200000 3709648 4995492 5495456 501056 4601940 1825635 6147030 7689408 143360 9147335 2417120 2793752 9916480 8197760 7882880 7267650 2321280 1451104 8753832 7068854 6171460 1619388 5223842 4450688 2700644 5887820 4425750 6152896 6689900 5465982 9139756 5472040 6425220 9986528 4223363 938070...
output:
367859591959 341355592681 180239954362 125106478765 799473701501 12540914494 113658177169 439855267098 593351571892 206765988098 198000313371 528837895899 159199669236 16199163797 485076055527 171497193223 298594266318 775543704410 113775003661 262345595573 473858669554 686338586089 239945373429 130...
result:
ok 399842 lines