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
| #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; template<typename T> inline void read(T &x) { char ch=getchar(); T f=1; x=0; while(!('0'<=ch&&ch<='9')) { if(ch=='-') f=-1; ch=getchar(); } while('0'<=ch&&ch<='9') { x=(x<<3)+(x<<1)+ch-48; ch=getchar(); } x*=f; } const int N=50010; struct sgttree { int left,right,sum,ans; }t[N<<2]; int n,m; int a[N]; #define ls(p) p<<1 #define rs(p) p<<1|1 inline sgttree pushup(sgttree ls,sgttree rs) { sgttree tmp; tmp.sum=ls.sum+rs.sum; tmp.left=max(ls.left,ls.sum+rs.left); tmp.right=max(rs.right,rs.sum+ls.right); tmp.ans=max(max(ls.ans,rs.ans),ls.right+rs.left); return tmp; } void build(int p,int l,int r) { if(l==r) { t[p].ans=t[p].sum=t[p].left=t[p].right=a[l]; return; } int mid=l+r>>1; build(ls(p),l,mid); build(rs(p),mid+1,r); t[p]=pushup(t[ls(p)],t[rs(p)]); } sgttree query(int ql,int qr,int l,int r,int p) { if(ql>qr) return (sgttree){0,0,0,0}; if(ql<=l&&r<=qr) { return t[p]; } int mid=l+r>>1; if(qr<=mid) return query(ql,qr,l,mid,ls(p)); if(mid<ql) return query(ql,qr,mid+1,r,rs(p)); sgttree ls,rs; if(ql<=mid) ls=query(ql,qr,l,mid,ls(p)); if(mid<qr) rs=query(ql,qr,mid+1,r,rs(p)); return pushup(ls,rs); } int main() { int _; read(_); ++_; while(--_) { memset(t,0,sizeof(t)); read(n); for(int i=1;i<=n;++i) read(a[i]); build(1,1,n); read(m); ++m; int ll,lr,rl,rr; while(--m) { read(ll); read(lr); read(rl); read(rr); if(lr<rl) printf("%d\n",query(lr+1,rl-1,1,n,1).sum+query(ll,lr,1,n,1).right+query(rl,rr,1,n,1).left); else { int res=query(rl,lr,1,n,1).ans; if(ll<rl) res=max(res,query(ll,rl,1,n,1).right+query(rl,rr,1,n,1).left-a[rl]); if(rr>lr) res=max(res,query(ll,lr,1,n,1).right+query(lr,rr,1,n,1).left-a[lr]); printf("%d\n",res); } } } return 0; }
|