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
|
#include<bits/stdc++.h> #define re register typedef long long ll; template<class T> inline void read(T &x) { x=0; char ch=getchar(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); if(t) x=-x; } template<class T,class ...T1> inline void read(T &x,T1 &...x1){ read(x),read(x1...); } template<class T> inline void write(T x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } template<> inline void write(bool x){ std::cout<<x; } template<> inline void write(char c){ putchar(c); } template<> inline void write(char *s){ while(*s!='\0') putchar(*s++); } template<> inline void write(const char *s){ while(*s!='\0') putchar(*s++); } template<class T,class ...T1> inline void write(T x,T1 ...x1){ write(x),write(x1...); } template<class T> inline bool checkMax(T &x,T y){ return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T y){ return x>y?x=y,1:0; } const int MAXN=1e5+10; const int INF=0x7f7f7f7f; int N,Q,a[MAXN]; #define ls (p<<1) #define rs (p<<1|1) struct Segment { int l,r; int val,cval,tag,ctag,cov,ccov; }Tr[MAXN<<2]; inline void Cover(int p,int cov,int ccov) { Tr[p].val=cov,Tr[p].tag=0; Tr[p].cov=cov,checkMax(Tr[p].ccov,ccov); checkMax(Tr[p].cval,Tr[p].ccov); } inline void Add(int p,int tag,int ctag) { if(Tr[p].cov!=-INF) return Cover(p,Tr[p].cov+tag,Tr[p].cov+ctag); checkMax(Tr[p].ctag,Tr[p].tag+ctag); checkMax(Tr[p].cval,Tr[p].val+ctag); Tr[p].val+=tag,Tr[p].tag+=tag; } inline void pushUp(int p) { Tr[p].val=std::max(Tr[ls].val,Tr[rs].val); Tr[p].cval=std::max(Tr[ls].cval,Tr[rs].cval); checkMax(Tr[p].cval,Tr[p].val); } inline void pushDown(int p) { Add(ls,Tr[p].tag,Tr[p].ctag),Add(rs,Tr[p].tag,Tr[p].ctag); if(Tr[p].cov!=-INF) { Cover(ls,Tr[p].cov,Tr[p].ccov),Cover(rs,Tr[p].cov,Tr[p].ccov); Tr[p].cov=-INF; } Tr[p].tag=Tr[p].ctag=0; } void build(int p,int l,int r) { Tr[p].l=l,Tr[p].r=r,Tr[p].tag=Tr[p].ctag=0,Tr[p].cov=-INF; if(l==r) return Tr[p].val=Tr[p].cval=a[l],void(); int mid=(l+r)>>1; build(ls,l,mid),build(rs,mid+1,r); pushUp(p); } void modifyAdd(int p,int l,int r,int v) { if(l<=Tr[p].l&&Tr[p].r<=r) return Add(p,v,v); pushDown(p); int mid=(Tr[p].l+Tr[p].r)>>1; if(l<=mid) modifyAdd(ls,l,r,v); if(mid<r) modifyAdd(rs,l,r,v); pushUp(p); } void modifyCov(int p,int l,int r,int v) { if(l<=Tr[p].l&&Tr[p].r<=r) return Cover(p,v,v); pushDown(p); int mid=(Tr[p].l+Tr[p].r)>>1; if(l<=mid) modifyCov(ls,l,r,v); if(mid<r) modifyCov(rs,l,r,v); pushUp(p); } int queryMax(int p,int l,int r) { if(l<=Tr[p].l&&Tr[p].r<=r) return Tr[p].val; pushDown(p); int mid=(Tr[p].l+Tr[p].r)>>1,res=-INF; if(l<=mid) checkMax(res,queryMax(ls,l,r)); if(mid<r) checkMax(res,queryMax(rs,l,r)); return res; } int queryCmax(int p,int l,int r) { if(l<=Tr[p].l&&Tr[p].r<=r) return Tr[p].cval; pushDown(p); int mid=(Tr[p].l+Tr[p].r)>>1,res=-INF; if(l<=mid) checkMax(res,queryCmax(ls,l,r)); if(mid<r) checkMax(res,queryCmax(rs,l,r)); return res; } char Opt[3]; int main() {
read(N); for(int i=1;i<=N;++i) read(a[i]); build(1,1,N); read(Q); for(int ql,qr,qv;Q--;) { scanf("%s",Opt+1);read(ql,qr); if(Opt[1]=='Q') write(queryMax(1,ql,qr),'\n'); if(Opt[1]=='A') write(queryCmax(1,ql,qr),'\n'); if(Opt[1]=='P') read(qv),modifyAdd(1,ql,qr,qv); if(Opt[1]=='C') read(qv),modifyCov(1,ql,qr,qv); } return 0; }
|