GSS

 库存

GSS1

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 
 5 inline int read() {
 6     int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
 7     for (;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f;
 8 }
 9 
10 const int N = 100100;
11 struct Seg{
12     int S,LS,RS,sum;
13 }T[N<<2];
14 
15 #define Root 1,n,1
16 #define lson l,mid,rt<<1
17 #define rson mid+1,r,rt<<1|1
18 
19 void pushup(int rt) {
20     T[rt].sum = T[rt<<1].sum + T[rt<<1|1].sum;
21     T[rt].LS = max(T[rt<<1].LS,T[rt<<1].sum+T[rt<<1|1].LS);
22     T[rt].RS = max(T[rt<<1|1].RS,T[rt<<1|1].sum+T[rt<<1].RS);
23     T[rt].S = max(T[rt<<1].RS+T[rt<<1|1].LS,max(T[rt<<1].S,T[rt<<1|1].S));
24 }
25 void build(int l,int r,int rt) {
26     if (l == r) {
27         T[rt].S = T[rt].LS = T[rt].RS = T[rt].sum = read();
28         return ;
29     }
30     int mid = (l + r) >> 1;
31     build(lson);
32     build(rson);
33     pushup(rt);
34 }
35 Seg query(int l,int r,int rt,int L,int R) {
36     if (L <= l && r <= R) {
37         return T[rt];
38     }
39     int mid = (l + r) >> 1;
40     if (L <= mid && R > mid) {
41         Seg res,ll,rr;
42         ll = query(lson,L,R);rr = query(rson,L,R);
43         res.sum = ll.sum + rr.sum;
44         res.LS = max(ll.LS,ll.sum+rr.LS);
45         res.RS = max(rr.RS,rr.sum+ll.RS);
46         res.S = max(ll.RS+rr.LS,max(ll.S,rr.S));
47         return res;
48     }
49     else if (L <= mid) return query(lson,L,R);
50     else if (R > mid) return query(rson,L,R);    
51 }
52 
53 int main() {
54     int n = read();
55     build(Root);
56     int m = read();
57     while (m--) {
58         int l = read(),r = read();
59         Seg ans = query(Root,l,r);
60         printf("%d
",ans.S);
61     }
62     return 0;
63 }
View Code

GSS2

  1 /*
  2 离线维护。
  3 记录每个点到当前节点的和 和 以每个点作为起点的最大子段和。
  4 s[i] = a[i] + a[i+1] + ... + a[now] ,MS[i] = max(s[i],s[i+1],...,s[r])
  5 将所有询问按右端点排序。当前扫到某一右端点为now,那么询问在l~now(now即r)之间的MS最大值。
  6 MS即维护每个节点的历史最大值。相应的维护历史最大Tag 
  7 
  8 如何维护:
  9 考虑S和Tag都是累加的,所以S和Tag都不是递增的。
 10 而MS要在所有历史S中取一个最大的,MS是递增的。
 11 即Mtag为上次下放标记后,到现在节点的最大的tag。Mtag也是递增。 
 12 
 13 假设一个节点在x-1时,下放了标记,现在在y,那么更新了a[x+1]~a[y]。
 14 由于tag累加,tag依次为a[x],a[x]+a[x+1],a[x]+a[x+1]+a[x+2]...
 15 Mtag在这些tag取出最大的。假设为a[x]+a[x+1]+...+a[z] (z<=y)
 16 
 17 此时下放标记:因为在上次下放标记时它的子节点的sum保留了i到x-1的和。
 18 leftson.sum = a[i]+a[i+1]+...+a[x-1]; (leftson的坐标为i), 
 19 那么leftson.MS = leftson.sum + Father.MTag (= a[i]+a[i+1]+...a[x-1]+a[x]+..a[z]);此处取max 
 20 然后更新leftson.sum = leftson.sum + Father.Tag;
 21 
 22 每次这样更新。 
 23 */
 24 #include<bits/stdc++.h>
 25 using namespace std;
 26 typedef long long LL;
 27 
 28 inline int read() {
 29     int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
 30     for (;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f;
 31 }
 32 
 33 const int N = 100100;
 34 int pos[N<<1];
 35 LL ans[N],a[N],S[N<<2],MS[N<<2],Tag[N<<2],MTag[N<<2];
 36 
 37 struct Que{
 38     int l,r,id;
 39     bool operator < (const Que &A) const {
 40         return r < A.r;
 41     }
 42 }q[N];
 43 
 44 #define Root 1,n,1
 45 #define lson l,mid,rt<<1
 46 #define rson mid+1,r,rt<<1|1
 47 void pushup(int rt) {
 48     S[rt] = max(S[rt<<1],S[rt<<1|1]);
 49     MS[rt] = max(MS[rt<<1],MS[rt<<1|1]);
 50 }
 51 void pushdown(int rt) {
 52     if (!Tag[rt] && !MTag[rt]) return ; 
 53     MS[rt<<1] = max(MS[rt<<1],S[rt<<1]+MTag[rt]);//-
 54     MTag[rt<<1] = max(MTag[rt<<1],Tag[rt<<1]+MTag[rt]);
 55     S[rt<<1] += Tag[rt];
 56     Tag[rt<<1] += Tag[rt];
 57     
 58     MS[rt<<1|1] = max(MS[rt<<1|1],S[rt<<1|1]+MTag[rt]);
 59     MTag[rt<<1|1] = max(MTag[rt<<1|1],Tag[rt<<1|1]+MTag[rt]);
 60     S[rt<<1|1] += Tag[rt];
 61     Tag[rt<<1|1] += Tag[rt];
 62     
 63     MTag[rt] = Tag[rt] = 0;
 64 }
 65 void update(int l,int r,int rt,int L,int R,LL val) {
 66     if (L <= l && r <= R) {
 67         S[rt] += val;MS[rt] = max(MS[rt],S[rt]);
 68         Tag[rt] += val;MTag[rt] = max(MTag[rt],Tag[rt]);
 69         return ;
 70     }
 71     int mid = (l + r) >> 1;
 72     pushdown(rt);
 73     if (L <= mid) update(lson,L,R,val);
 74     if (R > mid)  update(rson,L,R,val);
 75     pushup(rt);    
 76 }
 77 LL query(int l,int r,int rt,int L,int R) {
 78     if (L <= l && r <= R) {
 79         return MS[rt];
 80     }
 81     pushdown(rt);
 82     LL res = 0;    
 83     int mid = (l + r) >> 1;
 84     if (L <= mid) res = max(res,query(lson,L,R));
 85     if (R > mid)  res = max(res,query(rson,L,R));
 86     return res;
 87 }
 88 int main() {
 89     int n = read();
 90     for (int i=1; i<=n; ++i) a[i] = read();
 91     int m = read();
 92     for (int i=1; i<=m; ++i) {
 93         q[i].l = read(),q[i].r = read(),q[i].id = i;
 94     }
 95     int T = 100000; // --
 96     sort(q+1,q+m+1);
 97     int cur = 1;
 98     for (int i=1; i<=n; ++i) {
 99         update(Root,pos[a[i]+T]+1,i,a[i]);
100         pos[a[i]+T] = i;
101         while (cur <= m && q[cur].r == i) {
102             ans[q[cur].id] = query(Root,q[cur].l,q[cur].r);
103             ++cur;
104         }
105     }
106     for (int i=1; i<=m; ++i) 
107         printf("%lld
",ans[i]);
108     return 0;
109 }
View Code

GSS3

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 
 5 inline int read() {
 6     int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
 7     for (;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f;
 8 }
 9 
10 const int N = 50100;
11 struct Seg{
12     int S,LS,RS,sum;
13 }T[N<<2];
14 
15 #define Root 1,n,1
16 #define lson l,mid,rt<<1
17 #define rson mid+1,r,rt<<1|1
18 
19 void pushup(int rt) {
20     T[rt].sum = T[rt<<1].sum + T[rt<<1|1].sum;
21     T[rt].LS = max(T[rt<<1].LS,T[rt<<1].sum+T[rt<<1|1].LS);
22     T[rt].RS = max(T[rt<<1|1].RS,T[rt<<1|1].sum+T[rt<<1].RS);
23     T[rt].S = max(T[rt<<1].RS+T[rt<<1|1].LS,max(T[rt<<1].S,T[rt<<1|1].S));
24 }
25 void build(int l,int r,int rt) {
26     if (l == r) {
27         T[rt].S = T[rt].LS = T[rt].RS = T[rt].sum = read();
28         return ;
29     }
30     int mid = (l + r) >> 1;
31     build(lson);
32     build(rson);
33     pushup(rt);
34 }
35 void update(int l,int r,int rt,int p,int val) {
36     if (l == r) {
37         T[rt].S = T[rt].LS = T[rt].RS = T[rt].sum = val;
38         return ;
39     }
40     int mid = (l + r) >> 1;
41     if (p <= mid) update(lson,p,val);
42     else update(rson,p,val);
43     pushup(rt);
44 }
45 Seg query(int l,int r,int rt,int L,int R) {
46     if (L <= l && r <= R) {
47         return T[rt];
48     }
49     int mid = (l + r) >> 1;
50     if (L <= mid && R > mid) {
51         Seg res,ll,rr;
52         ll = query(lson,L,R);rr = query(rson,L,R);
53         res.sum = ll.sum + rr.sum;
54         res.LS = max(ll.LS,ll.sum+rr.LS);
55         res.RS = max(rr.RS,rr.sum+ll.RS);
56         res.S = max(ll.RS+rr.LS,max(ll.S,rr.S));
57         return res;
58     }
59     else if (L <= mid) return query(lson,L,R);
60     else if (R > mid) return query(rson,L,R);    
61 }
62 
63 int main() {
64     int n = read();
65     build(Root);
66     int m = read();
67     while (m--) {
68         int opt = read(),a = read(),b = read();
69         if (opt==0) update(Root,a,b);
70         else {
71             Seg ans = query(Root,a,b);
72             printf("%d
",ans.S);
73         }
74     }
75     return 0;
76 }
View Code

GSS4

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4  
 5 const int N = 100100;
 6  
 7 LL sum[N<<2];
 8 int tag[N<<2];
 9  
10 #define lson l,mid,rt<<1
11 #define rson mid+1,r,rt<<1|1
12 void pushup(int rt) {
13     sum[rt] = sum[rt<<1] + sum[rt<<1|1];
14     tag[rt] = tag[rt<<1] & tag[rt<<1|1];
15 }
16 void build(int l,int r,int rt) {
17     if (l == r) {
18         scanf("%lld",&sum[rt]);
19         if (sum[rt] <= 1) tag[rt] = 1;
20         return; 
21     }
22     int mid = (l + r) >> 1;
23     build(lson);build(rson);
24     pushup(rt);
25 }
26 void update(int l,int r,int rt,int L,int R) {
27     if (tag[rt]) return;
28     if (l == r) {
29         sum[rt] = sqrt(sum[rt]);
30         if (sum[rt] <= 1) tag[rt] = 1;
31         return ;
32     }
33     int mid = (l + r) >> 1;
34     if (L <= mid) update(lson,L,R);
35     if (R > mid)  update(rson,L,R);
36     pushup(rt);
37 }
38 LL query(int l,int r,int rt,int L,int R) {
39     if (L <= l && r <= R) {
40         return sum[rt];
41     }
42     int mid = (l + r) >> 1;
43     LL res = 0;
44     if (L <= mid) res += query(lson,L,R);
45     if (R > mid)  res += query(rson,L,R);
46     return res;
47 }
48 void Clear() {
49     memset(sum,0,sizeof(sum));
50     memset(tag,0,sizeof(tag));
51 }
52 int main() {
53     int n,m,Case = 0;;
54     while (scanf("%d",&n)!=EOF) {
55         Clear();
56         build(1,n,1);
57         scanf("%d",&m);
58         printf("Case #%d:
",++Case);
59         while (m--) {
60             int opt,l,r;
61             scanf("%d%d%d",&opt,&l,&r);
62             if (l > r) swap(l,r); //--此处留意 
63             if (opt==1) printf("%lld
",query(1,n,1,l,r));
64             else update(1,n,1,l,r);
65         }
66         puts("");
67     }
68     return 0;
69 }
View Code

GSS5

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 
 5 inline int read() {
 6     int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
 7     for (;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f;
 8 }
 9 
10 const int N = 10100;
11 int A[N];
12 struct Seg{
13     int S,LS,RS,sum;
14 }T[N<<2];
15 
16 #define Root 1,n,1
17 #define lson l,mid,rt<<1
18 #define rson mid+1,r,rt<<1|1
19 
20 void pushup(int rt) {
21     T[rt].sum = T[rt<<1].sum + T[rt<<1|1].sum;
22     T[rt].LS = max(T[rt<<1].LS,T[rt<<1].sum+T[rt<<1|1].LS);
23     T[rt].RS = max(T[rt<<1|1].RS,T[rt<<1|1].sum+T[rt<<1].RS);
24     T[rt].S = max(T[rt<<1].RS+T[rt<<1|1].LS,max(T[rt<<1].S,T[rt<<1|1].S));
25 }
26 void build(int l,int r,int rt) {
27     if (l == r) {
28         T[rt].S = T[rt].LS = T[rt].RS = T[rt].sum = A[l];
29         return ;
30     }
31     int mid = (l + r) >> 1;
32     build(lson);
33     build(rson);
34     pushup(rt);
35 }
36 Seg query(int l,int r,int rt,int L,int R) {
37     if (L <= l && r <= R) {
38         return T[rt];
39     }
40     int mid = (l + r) >> 1;
41     if (L <= mid && R > mid) {
42         Seg res,ll,rr;
43         ll = query(lson,L,R);rr = query(rson,L,R);
44         res.sum = ll.sum + rr.sum;
45         res.LS = max(ll.LS,ll.sum+rr.LS);
46         res.RS = max(rr.RS,rr.sum+ll.RS);
47         res.S = max(ll.RS+rr.LS,max(ll.S,rr.S));
48         return res;
49     }
50     else if (L <= mid) return query(lson,L,R);
51     else if (R > mid) return query(rson,L,R);    
52 }
53 
54 int main() {
55     int Case = read();
56     while (Case--) {
57         int n = read();
58         for (int i=1; i<=n; ++i) A[i] = read();
59         build(Root);
60         int m = read();
61         while (m--) {
62             int a = read(),b = read(),c = read(),d = read();
63             if (c > b) {
64                 int t1 = query(Root,a,b).RS;
65                 int t2 = c-b>1 ? query(Root,b+1,c-1).sum : 0;
66                 int t3 = query(Root,c,d).LS;
67                 printf("%d
",t1 + t2 + t3);
68             }
69             else {
70                 int t1 = query(Root,a,c).RS + query(Root,c,d).LS - A[c];
71                 int t2 = query(Root,a,b).RS + query(Root,b,d).LS - A[b];
72                 int t3 = query(Root,c,b).S;
73                 printf("%d
",max(t1, max(t2, t3)));// --max(t1,max(t1,t3))
74             }
75         }
76     }
77     return 0;
78 }
View Code

GSS6

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 typedef long long LL;
  4 
  5 inline int read() {
  6     int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
  7     for (;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f;
  8 }
  9 const int N = 200100;
 10 int ch[N][2],fa[N],siz[N],S[N],data[N],LS[N],RS[N],sum[N],Root,Tn;
 11 
 12 #define lc ch[p][0]
 13 #define rc ch[p][1]
 14 
 15 int son(int x) {
 16     return x==ch[fa[x]][1];
 17 }
 18 void pushup(int p) {
 19     siz[p] = siz[lc] + siz[rc] + 1;
 20     sum[p] = sum[lc] + sum[rc] + data[p];
 21     LS[p] = max(LS[lc],sum[lc]+data[p]+max(LS[rc],0));
 22     RS[p] = max(RS[rc],sum[rc]+data[p]+max(RS[lc],0));
 23     S[p] = max(S[lc],S[rc]);
 24     S[p] = max(S[p],data[p]+max(LS[rc],0)+max(RS[lc],0));
 25 }
 26 void rotate(int x) {
 27     int y = fa[x],z = fa[y],b = son(x),c = son(y),a = ch[x][!b];
 28     if (z) ch[z][c] = x;else Root = x;fa[x] = z;
 29     ch[x][!b] = y;fa[y] = x;
 30     ch[y][b] = a;if (a) fa[a] = y;
 31     pushup(y);pushup(x);
 32 }
 33 void splay(int x,int rt) {
 34     while (fa[x] != rt) {
 35         int y = fa[x],z = fa[y];
 36         if (z == rt) rotate(x);
 37         else {
 38             if (son(x) == son(y)) rotate(y),rotate(x);
 39             else rotate(x),rotate(x);
 40         }
 41     }
 42 }
 43 int getkth(int k) {
 44     int p = Root;
 45     while (true) {
 46         if (k == siz[lc] + 1) return p;
 47         if (k <= siz[lc]) p = lc;
 48         else {
 49             k -= siz[lc] + 1;
 50             p = rc;
 51         }
 52     }
 53 }
 54 void Insert() {
 55     int p = read(),x = read();
 56     int t = getkth(p); // p - 1 + 1
 57     splay(t,0);
 58     ++Tn;
 59     ch[Tn][1] = ch[Root][1];fa[ch[Root][1]] = Tn;
 60     ch[Root][1] = Tn;fa[Tn] = Root;
 61     data[Tn] = x;
 62     pushup(Tn);pushup(Root);
 63 }
 64 void Delete() {
 65     int x = read() + 1;
 66     splay(getkth(x-1),0);
 67     splay(getkth(x+1),Root);
 68     fa[ch[ch[Root][1]][0]] = 0;
 69     ch[ch[Root][1]][0] = 0;
 70     pushup(ch[Root][1]);pushup(Root);
 71 }
 72 void Replace() {
 73     int x = read() + 1,y = read();
 74     splay(getkth(x),0);
 75     data[Root] = y;
 76     pushup(Root);
 77 }
 78 void Query() {
 79     int l = read() + 1,r = read() + 1;
 80     splay(getkth(l-1),0);splay(getkth(r+1),Root);
 81     printf("%d
",S[ch[ch[Root][1]][0]]);
 82 }
 83 int build(int l,int r) {
 84     if (l > r) return 0;
 85     int mid = (l + r) >> 1;
 86     int t1 = build(l,mid-1);ch[mid][0] = t1;fa[t1] = mid;
 87     int t2 = build(mid+1,r);ch[mid][1] = t2;fa[t2] = mid;
 88     pushup(mid);
 89     return mid;
 90 }
 91 void init() {
 92     memset(S,-0x3f,sizeof(S));
 93     memset(LS,-0x3f,sizeof(LS));
 94     memset(RS,-0x3f,sizeof(RS));
 95     memset(ch,0,sizeof(ch)); // 由于新加了一些节点,所以要全清空 
 96     memset(fa,0,sizeof(fa));
 97     memset(siz,0,sizeof(siz));
 98     memset(sum,0,sizeof(sum));
 99 }
100 int main() {
101     int n,m;
102     while (scanf("%d",&n)!=EOF) {
103         Tn = n + 2;
104         init();
105         for (int i=2; i<=n+1; ++i) data[i] = read();
106         Root = build(1,n+2);
107         int m = read();
108         char opt[5];
109         while (m--) {
110             scanf("%s",opt);
111             if (opt[0] == 'I') Insert();
112             else if (opt[0] == 'D') Delete();
113             else if (opt[0] == 'R') Replace();
114             else Query();
115         }
116     }
117     return 0;
118 }
View Code

未完

原文地址:https://www.cnblogs.com/mjtcn/p/9247355.html