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
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<climits> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std;
typedef long long LL;
inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; }
const int maxn=100005,maxb=325;
int L[maxb],R[maxb],Belong[maxn]; LL a[maxn];
struct Block { int left,right,pos; LL limit,lazy1,lazy2; Block(int l=0,int r=0) { left=l,right=r; pos=lazy1=lazy2=0; limit=LLONG_MAX; reset(); } void reset() { LL Max=0,delta=lazy1; pos=0; for(int i=left; i<=right; i++) { a[i]+=delta; if(a[i]>=Max) { Max=a[i]; pos=i; limit=LLONG_MAX; } else limit=min(limit,(Max-a[i])/(i-pos)); delta+=lazy2; } lazy1=lazy2=0; } LL get_val(int x) { return a[x]+lazy1+lazy2*(x-left); } LL get_max() { return get_val(pos); } };
vector<Block> B;
LL Query(int Left,int Right) { int Lb=Belong[Left],Rb=Belong[Right]; LL ans=0; if(Rb-Lb<=1) { for(int i=Left; i<=Right; i++)ans=max(ans,B[Belong[i]].get_val(i)); return ans; } for(int i=Left; i<=R[Lb]; i++)ans=max(ans,B[Belong[i]].get_val(i)); for(int i=L[Rb]; i<=Right; i++)ans=max(ans,B[Belong[i]].get_val(i)); for(int i=Lb+1; i<=Rb-1; i++)ans=max(ans,B[i].get_max()); return ans; }
void modify(int Left,int Right,int st,int val) { int b=Belong[Left]; LL delta=1ll*(Left-st+1)*val; for(int i=Left; i<=Right; i++) { a[i]+=delta; delta+=val; } B[b].reset(); }
void Modify(int Left,int Right,int val) { int Lb=Belong[Left],Rb=Belong[Right]; if(Lb==Rb) { modify(Left,Right,Left,val); return; } modify(Left,R[Lb],Left,val); modify(L[Rb],Right,Left,val); for(int i=Lb+1; i<=Rb-1; i++) { if(val<=B[i].limit) { B[i].limit-=val; B[i].lazy1+=1ll*(L[i]-Left+1)*val; B[i].lazy2+=val; } else { B[i].lazy1+=1ll*(L[i]-Left+1)*val; B[i].lazy2+=val; B[i].reset(); } } }
int n,m,BCC=0;
int main() { n=Get_Int(); m=Get_Int(); int size=sqrt(n); for(int i=1; i<=n; i++) { a[i]=Get_Int(); Belong[i]=(i-1)/size; if(!L[Belong[i]])L[Belong[i]]=i,BCC++; R[Belong[i]]=i; } for(int i=0; i<BCC; i++)B.push_back(Block(L[i],R[i])); for(int i=1; i<=m; i++) { int opt=Get_Int(),l=Get_Int(),r=Get_Int(); if(opt==1) { LL ans=Query(l,r); if(ans<B[0].get_val(1))puts("0"); else printf("%lld\n",ans-B[0].get_val(1)); } else if(opt==2) { int Lb=Belong[l],Rb=Belong[r]; B[Lb].reset(); if(Lb!=Rb)B[Rb].reset(); swap(a[l],a[r]); B[Lb].reset(); if(Lb!=Rb)B[Rb].reset(); } else Modify(l,r,Get_Int()); } return 0; }
|