1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int>pii; const int maxn=3e5+5; const ll mod=998244353; int n,q; ll a[maxn]; struct node{ ll pre,suf,mn,sum; }t[maxn<<2]; void push_up(int rt){ t[rt].pre=min(t[rt<<1].pre,t[rt<<1].sum+t[rt<<1|1].pre); t[rt].suf=min(t[rt<<1|1].suf,t[rt<<1].suf+t[rt<<1|1].sum); t[rt].sum=t[rt<<1].sum+t[rt<<1|1].sum; t[rt].mn=min(min(t[rt<<1].mn,t[rt<<1|1].mn),t[rt<<1].suf+t[rt<<1|1].pre); } void build(int rt,int l,int r){ int mid=l+r>>1; if(l==r){ t[rt].pre=t[rt].suf=t[rt].mn=t[rt].sum=a[l]; return ; } build(rt<<1,l,mid); build(rt<<1|1,mid+1,r); push_up(rt); } ll query_sum(int rt,int l,int r,int xl,int xr){ int mid=l+r>>1; if(xl==l&&xr==r){ return t[rt].sum; } ll res=0; if(xl<=mid)res+=query_sum(rt<<1,l,mid,xl,min(xr,mid)); if(xr>mid)res+=query_sum(rt<<1|1,mid+1,r,max(xl,mid+1),xr); return res; } ll query_suf(int rt,int l,int r,int xl,int xr){ int mid=l+r>>1; if(l==xl&&r==xr){ return t[rt].suf; } if(xr<=mid)return query_suf(rt<<1,l,mid,xl,xr); else if(xl>mid)return query_suf(rt<<1|1,mid+1,r,xl,xr); else return min(query_suf(rt<<1,l,mid,xl,mid)+query_sum(rt<<1|1,mid+1,r,mid+1,xr), query_suf(rt<<1|1,mid+1,r,mid+1,xr)); } ll query_pre(int rt,int l,int r,int xl,int xr){ int mid=l+r>>1; if(l==xl&&r==xr){ return t[rt].pre; } if(xr<=mid)return query_pre(rt<<1,l,mid,xl,xr); else if(xl>mid)return query_pre(rt<<1|1,mid+1,r,xl,xr); else return min(query_pre(rt<<1|1,mid+1,r,mid+1,xr)+query_sum(rt<<1,l,mid,xl,mid), query_pre(rt<<1,l,mid,xl,mid)); } ll query_mn(int rt,int l,int r,int xl,int xr){ int mid=l+r>>1; if(l==xl&&xr==r){ return t[rt].mn; } ll res=0; if(xl<=mid)res=min(res,query_mn(rt<<1,l,mid,xl,min(xr,mid))); if(xr>mid)res=min(res,query_mn(rt<<1|1,mid+1,r,max(xl,mid+1),xr)); if(xl<=mid&&xr>mid)res=min(res,query_suf(rt<<1,l,mid,xl,mid)+query_pre(rt<<1|1,mid+1,r,mid+1,xr)); return res; } void update(int rt,int l,int r,int xp){ int mid=l+r>>1; if(l==r){ t[rt].pre=t[rt].suf=t[rt].mn=t[rt].sum=a[l]; return; } if(xp<=mid)update(rt<<1,l,mid,xp); else update(rt<<1|1,mid+1,r,xp); push_up(rt); } int main(){ scanf("%d%d",&n,&q); for(int i=1;i<=n;i++){ scanf("%lld",&a[i]); } build(1,1,n); while(q--){ int opt; scanf("%d",&opt); if(opt==1){ int l,r;ll x; scanf("%d%d%lld",&l,&r,&x); ll dn=(x-1)/10+1; ll cur=0; if(l!=1)cur=min(cur,query_mn(1,1,n,1,l-1)); if(l!=1&&cur<=-x)puts("0"); else { ll temp=a[l]; a[l]=query_suf(1,1,n,1,l); update(1,1,n,l); cur=query_mn(1,1,n,l,r); ll mn_suf=0; if(cur<=dn-x||a[l]-temp<=dn-x){ mn_suf=dn-x; } else { mn_suf=query_suf(1,1,n,l,r); } a[l]=temp; update(1,1,n,l); if(r==n)printf("%lld\n",min(x,mn_suf+x)); else { if(mn_suf<0){ ll temp=a[r+1]; a[r+1]+=mn_suf; update(1,1,n,r+1); cur=query_mn(1,1,n,r+1,n); if(cur<=-x)puts("0"); else printf("%lld\n",min(x,x+query_suf(1,1,n,r+1,n))); a[r+1]=temp; update(1,1,n,r+1); } else { cur=query_mn(1,1,n,r+1,n); if(cur<=-x)puts("0"); else printf("%lld\n",min(x,x+query_suf(1,1,n,r+1,n))); } }
} } else { int idx,x; scanf("%d%d",&idx,&x); a[idx]=x; update(1,1,n,idx); } }
return 0; }
|