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
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<climits> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std;
#define inf INT_MAX/3
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=500005;
struct Data { int a,b; Data(int _a=0,int _b=-inf):a(_a),b(_b) {} Data operator + (const Data& y) { return Data(max(-inf,a+y.a),max(b+y.a,y.b)); } };
Data max(Data x,Data y) { return Data(max(x.a,y.a),max(x.b,y.b)); }
struct Tree { int left,right; Data now,history; int val,hisval; Tree(int l=0,int r=0,Data n=Data(),Data his=Data(),int v=-inf,int hisv=-inf):left(l),right(r),now(n),history(his),val(v),hisval(hisv) {} };
struct Segment_Tree { Tree tree[maxn*4]; #define ls index<<1 #define rs index<<1|1 void build(int index,int Left,int Right,int* a) { tree[index]=Tree(Left,Right); if(Left==Right) { tree[index].val=tree[index].hisval=a[Left]; return; } int mid=(Left+Right)>>1; build(ls,Left,mid,a); build(rs,mid+1,Right,a); push_up(index); } void push_up(int index) { tree[index].val=max(tree[ls].val,tree[rs].val); tree[index].hisval=max(tree[ls].hisval,tree[rs].hisval); } void push_down(int index) { tree[ls].history=max(tree[ls].history,tree[ls].now+tree[index].history); tree[ls].now=tree[ls].now+tree[index].now; tree[ls].hisval=max(tree[ls].hisval,max(tree[ls].val+tree[index].history.a,tree[index].history.b)); tree[ls].val=max(tree[ls].val+tree[index].now.a,tree[index].now.b); tree[rs].history=max(tree[rs].history,tree[rs].now+tree[index].history); tree[rs].now=tree[rs].now+tree[index].now; tree[rs].hisval=max(tree[rs].hisval,max(tree[rs].val+tree[index].history.a,tree[index].history.b)); tree[rs].val=max(tree[rs].val+tree[index].now.a,tree[index].now.b); tree[index].now=tree[index].history=Data(); } void modify(int index,int Left,int Right,Data v) { if(Left>tree[index].right||Right<tree[index].left)return; if(Left<=tree[index].left&&Right>=tree[index].right) { tree[index].now=tree[index].now+v; tree[index].history=max(tree[index].history,tree[index].now); tree[index].val=max(tree[index].val+v.a,v.b); tree[index].hisval=max(tree[index].hisval,tree[index].val); return; } push_down(index); modify(ls,Left,Right,v); modify(rs,Left,Right,v); push_up(index); } int query(int index,int Left,int Right,bool flag) { if(Left>tree[index].right||Right<tree[index].left)return -inf; if(Left<=tree[index].left&&Right>=tree[index].right)return flag?tree[index].val:tree[index].hisval; push_down(index); return max(query(ls,Left,Right,flag),query(rs,Left,Right,flag)); } } st;
int n,m,a[maxn];
int main() { n=Get_Int(); for(int i=1; i<=n; i++)a[i]=Get_Int(); m=Get_Int(); st.build(1,1,n,a); for(int i=1; i<=m; i++) { char opt=' '; while(!isalpha(opt))opt=getchar(); int x=Get_Int(),y=Get_Int(); if(opt=='Q')printf("%d\n",st.query(1,x,y,1)); else if(opt=='A')printf("%d\n",st.query(1,x,y,0)); else if(opt=='P')st.modify(1,x,y,Data(Get_Int(),-inf)); else st.modify(1,x,y,Data(-inf,Get_Int())); } return 0; }
|