激!GSS系列
1 #include <cstdio> 2 const int sizeOfNumber=50005; 3 const int sizeOfSeg=200002; 4 5 inline int max(int, int); 6 inline int getint(); 7 inline void putint(int); 8 9 struct node 10 { 11 int lmax, rmax, smax, ssum; 12 inline node(int=0); 13 }; 14 inline node merge(node, node); 15 16 struct seg 17 { 18 node data; 19 seg * l, * r; 20 inline void maintain(); 21 }; 22 seg memory[sizeOfSeg], * port=memory; 23 inline seg * newseg(); 24 seg * build(int, int); 25 node query(seg * , int, int, int, int); 26 27 int n, m; 28 int a[sizeOfNumber]; 29 seg * t; 30 31 int main() 32 { 33 n=getint(); 34 for (int i=1;i<=n;i++) 35 a[i]=getint(); 36 t=build(1, n); 37 38 m=getint(); 39 for (int i=1;i<=m;i++) 40 { 41 int x=getint(), y=getint(); 42 putint(query(t, 1, n, x, y).smax); 43 } 44 45 return 0; 46 } 47 48 inline int max(int x, int y) 49 { 50 return x>y?x:y; 51 } 52 inline int getint() 53 { 54 register int num=0; 55 register char ch=0, last; 56 do last=ch, ch=getchar(); while (ch<'0' || ch>'9'); 57 do num=num*10+ch-'0', ch=getchar(); while (ch>='0' && ch<='9'); 58 if (last=='-') num=-num; 59 return num; 60 } 61 inline void putint(int num) 62 { 63 char stack[11]; 64 register int top=0; 65 if (num==0) stack[top=1]='0'; 66 if (num<0) putchar('-'), num=-num; 67 for ( ;num;num/=10) stack[++top]=num%10+'0'; 68 for ( ;top;top--) putchar(stack[top]); 69 putchar('\n'); 70 } 71 72 inline node::node(int x) 73 { 74 lmax=rmax=smax=ssum=x; 75 } 76 inline node merge(node a, node b) 77 { 78 node c; 79 c.ssum=a.ssum+b.ssum; 80 c.lmax=max(a.lmax, a.ssum+b.lmax); 81 c.rmax=max(a.rmax+b.ssum, b.rmax); 82 c.smax=max(a.smax, b.smax); 83 c.smax=max(c.smax, a.rmax+b.lmax); 84 return c; 85 } 86 87 inline seg * newseg() 88 { 89 seg * ret=port++; 90 return ret; 91 } 92 inline void seg::maintain() 93 { 94 this->data=merge(this->l->data, this->r->data); 95 } 96 inline seg * build(int l, int r) 97 { 98 seg * t=newseg(); 99 int m=(l+r)>>1; 100 101 if (l==r) 102 t->data=node(a[m]); 103 else 104 { 105 t->l=build(l, m); 106 t->r=build(m+1, r); 107 t->maintain(); 108 } 109 110 return t; 111 } 112 node query(seg * t, int l, int r, int ql, int qr) 113 { 114 node ret; 115 int m=(l+r)>>1; 116 117 if (l==ql && r==qr) ret=t->data; 118 else 119 { 120 if (qr<=m) ret=query(t->l, l, m, ql, qr); 121 else if (ql>m) ret=query(t->r, m+1, r, ql, qr); 122 else ret=merge(query(t->l, l, m, ql, m), query(t->r, m+1, r, m+1, qr)); 123 } 124 125 return ret; 126 }
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 const int sizeOfNumber=100001; 5 const int sizeOfQuestion=100001; 6 const int sizeOfSeg=400004; 7 8 inline int max(int, int); 9 inline int getint(); 10 inline void putint(int); 11 12 struct seg 13 { 14 int ssum, lmax; 15 int flag, maxflag; 16 seg * l, * r; 17 inline void pushdown(); 18 inline void maintain(); 19 }; 20 seg memory[sizeOfSeg], * port=memory; 21 inline seg * newseg(); 22 seg * build(int, int); 23 void update(seg * , int, int, int, int, int); 24 int query(seg * , int, int, int, int); 25 26 int n, q; 27 int a[sizeOfNumber], p[sizeOfNumber]; 28 int d[sizeOfQuestion], l[sizeOfQuestion], r[sizeOfQuestion]; 29 int ans[sizeOfQuestion]; 30 seg * t; 31 inline bool cmpForQuestion(int, int); 32 inline bool cmpForDiscrete(int, int); 33 inline void prepare(); 34 35 int main() 36 { 37 n=getint(); 38 for (int i=1;i<=n;i++) 39 a[i]=getint(); 40 prepare(); 41 t=build(1, n); 42 q=getint(); 43 for (int i=1;i<=q;i++) 44 l[i]=getint(), r[i]=getint(); 45 for (int i=1;i<=q;i++) 46 d[i]=i; 47 std::sort(d+1, d+q+1, cmpForQuestion); 48 49 int j=1; 50 for (int i=1;i<=n;i++) 51 { 52 update(t, 1, n, p[i]+1, i, a[i]); 53 for ( ;j<=q && r[d[j]]==i;j++) 54 ans[d[j]]=query(t, 1, n, l[d[j]], r[d[j]]); 55 } 56 57 for (int i=1;i<=q;i++) 58 putint(ans[i]); 59 60 return 0; 61 } 62 63 inline int max(int x, int y) 64 { 65 return x>y?x:y; 66 } 67 inline int getint() 68 { 69 register int num=0; 70 register char ch=0, last; 71 do last=ch, ch=getchar(); while (ch<'0' || ch>'9'); 72 do num=num*10+ch-'0', ch=getchar(); while (ch>='0' && ch<='9'); 73 if (last=='-') num=-num; 74 return num; 75 } 76 inline void putint(int num) 77 { 78 char stack[11]; 79 register int top=0; 80 if (num==0) stack[top=1]='0'; 81 if (num<0) putchar('-'), num=-num; 82 for ( ;num;num/=10) stack[++top]=num%10+'0'; 83 for ( ;top;top--) putchar(stack[top]); 84 putchar('\n'); 85 } 86 87 inline void seg::pushdown() 88 { 89 this->l->maxflag=max(this->l->maxflag, this->l->flag+this->maxflag); 90 this->r->maxflag=max(this->r->maxflag, this->r->flag+this->maxflag); 91 this->l->flag+=this->flag; 92 this->r->flag+=this->flag; 93 this->l->lmax=max(this->l->lmax, this->l->ssum+this->maxflag); 94 this->r->lmax=max(this->r->lmax, this->r->ssum+this->maxflag); 95 this->l->ssum+=this->flag; 96 this->r->ssum+=this->flag; 97 this->flag=0; 98 this->maxflag=0; 99 } 100 inline void seg::maintain() 101 { 102 this->ssum=max(this->l->ssum, this->r->ssum); 103 this->lmax=max(this->l->lmax, this->r->lmax); 104 } 105 inline seg * newseg() 106 { 107 seg * ret=port++; 108 return ret; 109 } 110 seg * build(int l, int r) 111 { 112 seg * t=newseg(); 113 int m=(l+r)>>1; 114 if (l==r) return t; 115 t->l=build(l, m); 116 t->r=build(m+1, r); 117 return t; 118 } 119 void update(seg * t, int l, int r, int ql, int qr, int v) 120 { 121 if (l==ql && r==qr) 122 { 123 t->ssum+=v; 124 t->lmax=max(t->lmax, t->ssum); 125 t->flag+=v; 126 t->maxflag=max(t->maxflag, t->flag); 127 } 128 else 129 { 130 int m=(l+r)>>1; 131 t->pushdown(); 132 if (qr<=m) update(t->l, l, m, ql, qr, v); 133 else if (ql>m) update(t->r, m+1, r, ql, qr, v); 134 else update(t->l, l, m, ql, m, v), update(t->r, m+1, r, m+1, qr, v); 135 t->maintain(); 136 } 137 } 138 int query(seg * t, int l, int r, int ql, int qr) 139 { 140 int ret=0; 141 142 if (l==ql && r==qr) 143 ret=t->lmax; 144 else 145 { 146 int m=(l+r)>>1; 147 t->pushdown(); 148 if (qr<=m) ret=query(t->l, l, m, ql, qr); 149 else if (ql>m) ret=query(t->r, m+1, r, ql, qr); 150 else ret=max(query(t->l, l, m, ql, m), query(t->r, m+1, r, m+1, qr)); 151 t->maintain(); 152 } 153 154 return ret; 155 } 156 157 inline bool cmpForQuestion(int i, int j) 158 { 159 return r[i]<r[j]; 160 } 161 inline bool cmpForDiscrete(int i, int j) 162 { 163 return a[i]<a[j]; 164 } 165 inline void prepare() 166 { 167 static int d[sizeOfNumber], l[sizeOfNumber]; 168 int m, t; 169 170 for (int i=1;i<=n;i++) 171 l[i]=i; 172 std::sort(l+1, l+n+1, cmpForDiscrete); 173 174 m=1, t=a[l[m]]; 175 d[l[1]]=1; 176 for (int i=2;i<=n;i++) 177 { 178 if (a[l[i]]>t) 179 ++m, t=a[l[i]]; 180 d[l[i]]=m; 181 } 182 183 memset(l, 0, sizeof(l)); 184 for (int i=1;i<=n;i++) 185 { 186 p[i]=l[d[i]]; 187 l[d[i]]=i; 188 } 189 }
1 #include <cstdio> 2 const int sizeOfNumber=50005; 3 const int sizeOfSeg=200002; 4 5 inline int max(int, int); 6 inline int getint(); 7 inline void putint(int); 8 9 struct node 10 { 11 int lmax, rmax, smax, ssum; 12 inline node(int=0); 13 }; 14 inline node merge(node, node); 15 16 struct seg 17 { 18 node data; 19 seg * l, * r; 20 inline void maintain(); 21 }; 22 seg memory[sizeOfSeg], * port=memory; 23 inline seg * newseg(); 24 seg * build(int, int); 25 void update(seg * , int, int, int); 26 node query(seg * , int, int, int, int); 27 28 int n, m; 29 int a[sizeOfNumber]; 30 seg * t; 31 32 int main() 33 { 34 n=getint(); 35 for (int i=1;i<=n;i++) 36 a[i]=getint(); 37 t=build(1, n); 38 39 m=getint(); 40 for (int i=1;i<=m;i++) 41 { 42 int k=getint(), x=getint(), y=getint(); 43 if (k==1) 44 putint(query(t, 1, n, x, y).smax); 45 else 46 { 47 a[x]=y; 48 update(t, 1, n, x); 49 } 50 } 51 52 return 0; 53 } 54 55 inline int max(int x, int y) 56 { 57 return x>y?x:y; 58 } 59 inline int getint() 60 { 61 register int num=0; 62 register char ch=0, last; 63 do last=ch, ch=getchar(); while (ch<'0' || ch>'9'); 64 do num=num*10+ch-'0', ch=getchar(); while (ch>='0' && ch<='9'); 65 if (last=='-') num=-num; 66 return num; 67 } 68 inline void putint(int num) 69 { 70 char stack[11]; 71 register int top=0; 72 if (num==0) stack[top=1]='0'; 73 if (num<0) putchar('-'), num=-num; 74 for ( ;num;num/=10) stack[++top]=num%10+'0'; 75 for ( ;top;top--) putchar(stack[top]); 76 putchar('\n'); 77 } 78 79 inline node::node(int x) 80 { 81 lmax=rmax=smax=ssum=x; 82 } 83 inline node merge(node a, node b) 84 { 85 node c; 86 c.ssum=a.ssum+b.ssum; 87 c.lmax=max(a.lmax, a.ssum+b.lmax); 88 c.rmax=max(a.rmax+b.ssum, b.rmax); 89 c.smax=max(a.smax, b.smax); 90 c.smax=max(c.smax, a.rmax+b.lmax); 91 return c; 92 } 93 94 inline seg * newseg() 95 { 96 seg * ret=port++; 97 return ret; 98 } 99 inline void seg::maintain() 100 { 101 this->data=merge(this->l->data, this->r->data); 102 } 103 inline seg * build(int l, int r) 104 { 105 seg * t=newseg(); 106 int m=(l+r)>>1; 107 108 if (l==r) 109 t->data=node(a[m]); 110 else 111 { 112 t->l=build(l, m); 113 t->r=build(m+1, r); 114 t->maintain(); 115 } 116 117 return t; 118 } 119 void update(seg * t, int l, int r, int k) 120 { 121 int m=(l+r)>>1; 122 123 if (l==r) 124 t->data=node(a[m]); 125 else 126 { 127 if (k<=m) update(t->l, l, m, k); 128 else update(t->r, m+1, r, k); 129 t->maintain(); 130 } 131 } 132 node query(seg * t, int l, int r, int ql, int qr) 133 { 134 node ret; 135 int m=(l+r)>>1; 136 137 if (l==ql && r==qr) ret=t->data; 138 else 139 { 140 if (qr<=m) ret=query(t->l, l, m, ql, qr); 141 else if (ql>m) ret=query(t->r, m+1, r, ql, qr); 142 else ret=merge(query(t->l, l, m, ql, m), query(t->r, m+1, r, m+1, qr)); 143 } 144 145 return ret; 146 }
1 #include <cstdio> 2 #include <cmath> 3 typedef long long LL; 4 const int sizeOfNumber=100001; 5 const int sizeOfSeg=400004; 6 7 inline void swap(int & , int & ); 8 inline LL getint(); 9 inline void putint(LL); 10 11 struct seg 12 { 13 LL sum; 14 bool flag; 15 seg * l, * r; 16 inline void maintain(); 17 }; 18 seg memory[sizeOfSeg], * port=memory; 19 inline seg * newseg(); 20 seg * build(int, int); 21 void update(seg * , int, int, int, int); 22 LL query(seg * , int, int, int, int); 23 24 int n, m; 25 LL a[sizeOfNumber]; 26 seg * t; 27 28 int main() 29 { 30 int c=0; 31 32 while (scanf("%d", &n)!=EOF) 33 { 34 for (int i=1;i<=n;i++) 35 a[i]=getint(); 36 port=memory; 37 t=build(1, n); 38 39 printf("Case #%d:\n", ++c); 40 41 m=getint(); 42 for (int i=1;i<=m;i++) 43 { 44 int k=getint(), x=getint(), y=getint(); 45 if (x>y) swap(x, y); 46 if (k==0) update(t, 1, n, x, y); 47 else putint(query(t, 1, n, x, y)); 48 } 49 } 50 51 return 0; 52 } 53 54 inline void swap(int & x, int & y) 55 { 56 int t=x; x=y; y=t; 57 } 58 inline LL getint() 59 { 60 register LL num=0; 61 register char ch; 62 do ch=getchar(); while (ch<'0' || ch>'9'); 63 do num=num*10+ch-'0', ch=getchar(); while (ch>='0' && ch<='9'); 64 return num; 65 } 66 inline void putint(LL num) 67 { 68 char stack[22]; 69 register int top=0; 70 if (num==0) stack[top=1]='0'; 71 for ( ;num;num/=10) stack[++top]=num%10+'0'; 72 for ( ;top;top--) putchar(stack[top]); 73 putchar('\n'); 74 } 75 76 inline void seg::maintain() 77 { 78 sum=l->sum+r->sum; 79 flag=l->flag&r->flag; 80 } 81 inline seg * newseg() 82 { 83 return port++; 84 } 85 seg * build(int l, int r) 86 { 87 seg * t=newseg(); 88 int m=(l+r)>>1; 89 90 if (l==r) 91 { 92 t->sum=a[m]; 93 t->flag=t->sum<=1; 94 } 95 else 96 { 97 t->l=build(l, m); 98 t->r=build(m+1, r); 99 t->maintain(); 100 } 101 102 return t; 103 } 104 void update(seg * t, int l, int r, int ql, int qr) 105 { 106 if (t->flag) 107 return ; 108 if (l==r && ql==qr) 109 { 110 t->sum=static_cast<int>(sqrt(t->sum)); 111 t->flag=t->sum<=1; 112 return ; 113 } 114 115 int m=(l+r)>>1; 116 if (qr<=m) update(t->l, l, m, ql, qr); 117 else if (ql>m) update(t->r, m+1, r, ql, qr); 118 else update(t->l, l, m, ql, m), update(t->r, m+1, r, m+1, qr); 119 t->maintain(); 120 } 121 LL query(seg * t, int l, int r, int ql, int qr) 122 { 123 if (l==ql && r==qr) 124 return t->sum; 125 int m=(l+r)>>1; 126 if (qr<=m) return query(t->l, l, m, ql, qr); 127 else if (ql>m) return query(t->r, m+1, r, ql, qr); 128 else return query(t->l, l, m, ql, m)+query(t->r, m+1, r, m+1, qr); 129 }
1 #include <cstdio> 2 const int sizeOfNumber=10001; 3 const int sizeOfSeg=40004; 4 const int inf=0x3F3F3F3F; 5 6 inline int max(int, int); 7 inline int getint(); 8 inline void putint(int); 9 10 struct node 11 { 12 int lmax, rmax, smax, ssum; 13 inline node(int=0); 14 }; 15 inline node merge(node, node); 16 17 struct seg 18 { 19 node data; 20 seg * l, * r; 21 inline void maintain(); 22 }; 23 seg memory[sizeOfSeg], * port=memory; 24 inline seg * newseg(); 25 seg * build(int, int); 26 node query(seg * , int, int, int, int); 27 28 int c, n, m; 29 int a[sizeOfNumber]; 30 seg * t; 31 32 int main() 33 { 34 int ans; 35 36 for (c=getint();c;c--) 37 { 38 n=getint(); 39 for (int i=1;i<=n;i++) 40 a[i]=getint(); 41 port=memory; 42 t=build(1, n); 43 44 m=getint(); 45 for (int i=1;i<=m;i++) 46 { 47 int x1=getint(), y1=getint(), x2=getint(), y2=getint(); 48 49 if (y1<x2) 50 ans=query(t, 1, n, x1, y1).rmax+query(t, 1, n, x2, y2).lmax+query(t, 1, n, y1+1, x2-1).ssum; 51 else 52 { 53 node l=query(t, 1, n, x1, x2-1), m=query(t, 1, n, x2, y1), r=query(t, 1, n, y1+1, y2); 54 ans=max(merge(l, m).rmax+r.lmax, l.rmax+merge(m, r).lmax); 55 ans=max(ans, l.rmax+m.ssum+r.lmax); 56 ans=max(ans, m.smax); 57 } 58 59 putint(ans); 60 } 61 } 62 63 return 0; 64 } 65 66 inline int max(int x, int y) 67 { 68 return x>y?x:y; 69 } 70 inline int getint() 71 { 72 register int num=0; 73 register char ch=0, last; 74 do last=ch, ch=getchar(); while (ch<'0' || ch>'9'); 75 do num=num*10+ch-'0', ch=getchar(); while (ch>='0' && ch<='9'); 76 if (last=='-') num=-num; 77 return num; 78 } 79 inline void putint(int num) 80 { 81 char stack[11]; 82 register int top=0; 83 if (num==0) stack[top=1]='0'; 84 if (num<0) putchar('-'), num=-num; 85 for ( ;num;num/=10) stack[++top]=num%10+'0'; 86 for ( ;top;top--) putchar(stack[top]); 87 putchar('\n'); 88 } 89 90 inline node::node(int x) 91 { 92 lmax=rmax=smax=ssum=x; 93 } 94 inline node merge(node a, node b) 95 { 96 node c; 97 c.ssum=a.ssum+b.ssum; 98 c.lmax=max(a.lmax, a.ssum+b.lmax); 99 c.rmax=max(a.rmax+b.ssum, b.rmax); 100 c.smax=max(a.smax, b.smax); 101 c.smax=max(c.smax, a.rmax+b.lmax); 102 return c; 103 } 104 105 inline seg * newseg() 106 { 107 seg * ret=port++; 108 return ret; 109 } 110 inline void seg::maintain() 111 { 112 this->data=merge(this->l->data, this->r->data); 113 } 114 inline seg * build(int l, int r) 115 { 116 seg * t=newseg(); 117 int m=(l+r)>>1; 118 119 if (l==r) 120 t->data=node(a[m]); 121 else 122 { 123 t->l=build(l, m); 124 t->r=build(m+1, r); 125 t->maintain(); 126 } 127 128 return t; 129 } 130 node query(seg * t, int l, int r, int ql, int qr) 131 { 132 node ret(0); 133 int m=(l+r)>>1; 134 135 if (ql>qr) return ret; 136 if (l==ql && r==qr) ret=t->data; 137 else 138 { 139 if (qr<=m) ret=query(t->l, l, m, ql, qr); 140 else if (ql>m) ret=query(t->r, m+1, r, ql, qr); 141 else ret=merge(query(t->l, l, m, ql, m), query(t->r, m+1, r, m+1, qr)); 142 } 143 144 return ret; 145 }
1 #include <cstdio> 2 #include <cstdlib> 3 const int sizeOfNumber=100001; 4 const int sizeOfTreap=200002; 5 const int inf=0x3F3F3F3F; 6 7 inline int max(int, int); 8 inline char getch(); 9 inline int getint(); 10 inline void putint(int); 11 12 struct treap 13 { 14 int key, sum; 15 int lmax, rmax, smax; 16 int size, weight; 17 treap * l, * r; 18 inline treap(); 19 inline void maintain(); 20 }; 21 treap * null=new treap(); 22 treap memory[sizeOfTreap], * port=memory; 23 inline treap * newtreap(int); 24 void split(treap * , int, treap *& , treap *& ); 25 treap * merge(treap * , treap * ); 26 27 int N, Q; 28 treap * t; 29 30 int main() 31 { 32 treap * l, * m, * r; 33 char c; 34 int x, y; 35 36 N=getint(); 37 t=null; 38 for (int i=1;i<=N;i++) 39 { 40 x=getint(); 41 t=merge(t, newtreap(x)); 42 } 43 44 Q=getint(); 45 for (int i=1;i<=Q;i++) 46 { 47 c=getch(); x=getint(); 48 49 if (c=='I') 50 { 51 y=getint(); 52 split(t, x-1, l, r); 53 m=newtreap(y); 54 l=merge(l, m); 55 t=merge(l, r); 56 } 57 else if (c=='D') 58 { 59 split(t, x-1, l, r); 60 split(r, 1, m, r); 61 t=merge(l, r); 62 } 63 else if (c=='R') 64 { 65 y=getint(); 66 split(t, x-1, l, r); 67 split(r, 1, m, r); 68 m=newtreap(y); 69 l=merge(l, m); 70 t=merge(l, r); 71 } 72 else 73 { 74 y=getint(); 75 split(t, x-1, l, r); 76 split(r, y-x+1, m, r); 77 putint(m->smax); 78 l=merge(l, m); 79 t=merge(l, r); 80 } 81 } 82 83 return 0; 84 } 85 86 inline int max(int x, int y) 87 { 88 return x>y?x:y; 89 } 90 inline char getch() 91 { 92 register char ch; 93 do ch=getchar(); while (ch!='I' && ch!='D' && ch!='R' && ch!='Q'); 94 return ch; 95 } 96 inline int getint() 97 { 98 register int num=0; 99 register char ch=0, last; 100 do last=ch, ch=getchar(); while (ch<'0' || ch>'9'); 101 do num=num*10+ch-'0', ch=getchar(); while (ch>='0' && ch<='9'); 102 if (last=='-') num=-num; 103 return num; 104 } 105 inline void putint(int num) 106 { 107 char stack[11]; 108 register int top=0; 109 if (num==0) stack[top=1]='0'; 110 if (num<0) putchar('-'), num=-num; 111 for ( ;num;num/=10) stack[++top]=num%10+'0'; 112 for ( ;top;top--) putchar(stack[top]); 113 putchar('\n'); 114 } 115 116 inline treap::treap() 117 { 118 this->key=0; this->sum=0; 119 this->lmax=-inf; this->rmax=-inf; this->smax=-inf; 120 this->size=0; this->weight=0; 121 this->l=this->r=this; 122 } 123 inline void treap::maintain() 124 { 125 this->sum=this->l->sum+this->key+this->r->sum; 126 127 this->lmax=max(this->l->lmax, this->l->sum+this->key); this->lmax=max(this->lmax, this->l->sum+this->key+this->r->lmax); 128 this->rmax=max(this->r->rmax, this->key+this->r->sum); this->rmax=max(this->rmax, this->l->rmax+this->key+this->r->sum); 129 130 this->smax=max(this->l->smax, this->r->smax); this->smax=max(this->smax, this->l->rmax+this->key+this->r->lmax); 131 this->smax=max(this->smax, this->l->rmax+this->key); this->smax=max(this->smax, this->key+this->r->lmax); 132 this->smax=max(this->smax, this->key); 133 134 this->size=this->l->size+1+this->r->size; 135 } 136 inline treap * newtreap(int _key) 137 { 138 treap * ret=port++; 139 ret->key=_key; ret->sum=_key; 140 ret->lmax=ret->rmax=ret->smax=_key; 141 ret->size=1; ret->weight=rand(); 142 ret->l=ret->r=null; 143 return ret; 144 } 145 void split(treap * t, int k, treap *& l, treap *& r) 146 { 147 if (t==null) 148 { 149 l=r=null; 150 return ; 151 } 152 153 if (t->l->size+1<=k) 154 { 155 l=t; 156 split(t->r, k-t->l->size-1, t->r, r); 157 l->maintain(); 158 } 159 else 160 { 161 r=t; 162 split(t->l, k, l, t->l); 163 r->maintain(); 164 } 165 } 166 treap * merge(treap * l, treap * r) 167 { 168 if (l==null) return r; 169 if (r==null) return l; 170 171 if (l->weight>r->weight) 172 { 173 l->r=merge(l->r, r); 174 l->maintain(); 175 return l; 176 } 177 else 178 { 179 r->l=merge(l, r->l); 180 r->maintain(); 181 return r; 182 } 183 }
1 #include <cstdio> 2 #include <cstring> 3 const int sizeOfNumber=100001; 4 const int sizeOfEdge=200002; 5 const int sizeOfSeg=400004; 6 const int inf=0x7FFFFFFF; 7 8 inline int lg(int); 9 inline void swap(int & , int & ); 10 inline int max(int, int); 11 inline int getint(); 12 inline void putint(int); 13 14 struct edge 15 { 16 int point; 17 edge * next; 18 }; 19 edge memoryOfEdge[sizeOfEdge], * portOfEdge=memoryOfEdge; 20 inline edge * newedge(int, edge * ); 21 inline void link(int, int); 22 23 struct node 24 { 25 int lmax, rmax, smax, ssum; 26 inline node(int=0); 27 }; 28 inline node merge(node, node); 29 30 struct seg 31 { 32 node data; 33 int flag, size; 34 seg * l, * r; 35 inline void pushdown(); 36 inline void maintain(); 37 }; 38 seg memoryOfSeg[sizeOfSeg], * portOfSeg=memoryOfSeg; 39 inline seg * newseg(); 40 seg * build(int, int); 41 void update(seg * , int, int, int, int, int); 42 node query(seg * , int, int, int, int); 43 44 int n, q; 45 int x[sizeOfNumber]; 46 edge * e[sizeOfNumber]; 47 int a[20][sizeOfNumber]; 48 int s[sizeOfNumber], d[sizeOfNumber], f[sizeOfNumber]; 49 int tmp, idx[sizeOfNumber], r_idx[sizeOfNumber], son[sizeOfNumber], top[sizeOfNumber]; 50 seg * t; 51 inline void dfsTree(); 52 inline void dfsChain(); 53 inline int lca(int, int); 54 inline int anc(int, int); 55 inline void update(int, int, int); 56 inline node query(int, int); 57 58 int main() 59 { 60 n=getint(); 61 for (int i=1;i<=n;i++) 62 x[i]=getint(); 63 for (int i=1;i<n;i++) 64 { 65 int u=getint(), v=getint(); 66 link(u, v); 67 } 68 dfsTree(); 69 dfsChain(); 70 71 t=build(1, n); 72 q=getint(); 73 for (int i=1;i<=q;i++) 74 { 75 int k=getint(), x=getint(), y=getint(); 76 77 if (k==1) 78 { 79 if (d[x]>d[y]) 80 swap(x, y); 81 if (x==y) 82 { 83 putint(query(t, 1, n, idx[x], idx[x]).smax); 84 continue; 85 } 86 87 int a=lca(x, y), z=anc(y, d[y]-d[a]-1); 88 node l=query(y, z), r=query(x, a); 89 swap(l.lmax, l.rmax); 90 putint(merge(l, r).smax); 91 } 92 else 93 { 94 int z=getint(); 95 update(x, y, z); 96 } 97 } 98 99 return 0; 100 } 101 102 inline int lg(int x) 103 { 104 return x>1?31-__builtin_clz(x):0; 105 } 106 inline void swap(int & x, int & y) 107 { 108 int z=x; x=y; y=z; 109 } 110 inline int max(int x, int y) 111 { 112 return x>y?x:y; 113 } 114 inline int getint() 115 { 116 register int num=0; 117 register char ch=0, last; 118 do last=ch, ch=getchar(); while (ch<'0' || ch>'9'); 119 do num=num*10+ch-'0', ch=getchar(); while (ch>='0' && ch<='9'); 120 if (last=='-') num=-num; 121 return num; 122 } 123 inline void putint(int num) 124 { 125 char stack[11]; 126 register int top=0; 127 if (num==0) stack[top=1]='0'; 128 if (num<0) putchar('-'), num=-num; 129 for ( ;num;num/=10) stack[++top]=num%10+'0'; 130 for ( ;top;top--) putchar(stack[top]); 131 putchar('\n'); 132 } 133 134 inline edge * newedge(int point, edge * next) 135 { 136 edge * ret=portOfEdge++; 137 ret->point=point; ret->next=next; 138 return ret; 139 } 140 inline void link(int u, int v) 141 { 142 e[u]=newedge(v, e[u]); 143 e[v]=newedge(u, e[v]); 144 } 145 146 inline node::node(int x) 147 { 148 ssum=x; 149 lmax=rmax=smax=max(x, 0); 150 } 151 inline node merge(node a, node b) 152 { 153 node c; 154 c.ssum=a.ssum+b.ssum; 155 c.lmax=max(a.lmax, a.ssum+b.lmax); 156 c.rmax=max(a.rmax+b.ssum, b.rmax); 157 c.smax=max(a.smax, b.smax); 158 c.smax=max(c.smax, a.rmax+b.lmax); 159 return c; 160 } 161 162 inline void seg::pushdown() 163 { 164 if (this->flag<inf) 165 { 166 this->l->data.ssum=this->l->size*this->flag; 167 this->r->data.ssum=this->r->size*this->flag; 168 this->l->data.lmax=this->l->data.rmax=this->l->data.smax=max(this->l->data.ssum, 0); 169 this->r->data.lmax=this->r->data.rmax=this->r->data.smax=max(this->r->data.ssum, 0); 170 this->l->flag=this->r->flag=this->flag; 171 this->flag=inf; 172 } 173 } 174 inline void seg::maintain() 175 { 176 this->data=merge(this->l->data, this->r->data); 177 this->size=this->l->size+this->r->size; 178 } 179 inline seg * newseg() 180 { 181 seg * ret=portOfSeg++; 182 ret->flag=inf; 183 return ret; 184 } 185 seg * build(int l, int r) 186 { 187 seg * t=newseg(); 188 int m=(l+r)>>1; 189 190 if (l==r) 191 { 192 t->data=node(x[r_idx[m]]); 193 t->size=1; 194 } 195 else 196 { 197 t->l=build(l, m); 198 t->r=build(m+1, r); 199 t->maintain(); 200 } 201 202 return t; 203 } 204 void update (seg * t, int l, int r, int ql, int qr, int v) 205 { 206 if (l==ql && r==qr) 207 { 208 t->data.ssum=t->size*v; 209 t->data.lmax=t->data.rmax=t->data.smax=max(t->data.ssum, 0); 210 t->flag=v; 211 } 212 else 213 { 214 int m=(l+r)>>1; 215 t->pushdown(); 216 if (qr<=m) update(t->l, l, m, ql, qr, v); 217 else if (ql>m) update(t->r, m+1, r, ql, qr, v); 218 else update(t->l, l, m, ql, m, v), update(t->r, m+1, r, m+1, qr, v); 219 t->maintain(); 220 } 221 } 222 node query(seg * t, int l, int r, int ql, int qr) 223 { 224 node ret; 225 226 if (l==ql && r==qr) 227 ret=t->data; 228 else 229 { 230 int m=(l+r)>>1; 231 t->pushdown(); 232 if (qr<=m) ret=query(t->l, l, m, ql, qr); 233 else if (ql>m) ret=query(t->r, m+1, r, ql, qr); 234 else ret=merge(query(t->l, l, m, ql, m), query(t->r, m+1, r, m+1, qr)); 235 t->maintain(); 236 } 237 238 return ret; 239 } 240 241 inline void dfsTree() 242 { 243 static edge * t[sizeOfNumber]; 244 memset(f, 0xFF, sizeof(f)); f[1]=0; 245 memmove(t, e, sizeof(e)); 246 int lim; 247 248 for (int u=1;true; ) 249 { 250 if (!s[u]) 251 { 252 s[u]=1; lim=lg(d[u]); 253 a[0][u]=f[u]; 254 for (int i=1;i<=lim;i++) 255 a[i][u]=a[i-1][a[i-1][u]]; 256 } 257 258 edge *& i=t[u]; 259 for ( ;i && f[i->point]>=0;i=i->next); 260 if (i) 261 { 262 f[i->point]=u; 263 d[i->point]=d[u]+1; 264 u=i->point; 265 } 266 else 267 { 268 if (u==1) break; 269 s[f[u]]+=s[u]; 270 if (s[u]>s[son[f[u]]]) 271 son[f[u]]=u; 272 u=f[u]; 273 } 274 } 275 } 276 inline void dfsChain() 277 { 278 static edge * t[sizeOfNumber]; 279 memmove(t, e, sizeof(e)); 280 281 top[1]=1; 282 for (int u=1;true; ) 283 { 284 if (!idx[u]) 285 { 286 idx[u]=++tmp; 287 r_idx[tmp]=u; 288 } 289 if (son[u] && !idx[son[u]]) 290 { 291 top[son[u]]=top[u]; 292 u=son[u]; 293 continue; 294 } 295 296 edge *& i=t[u]; 297 for ( ;i && idx[i->point];i=i->next); 298 if (i) 299 { 300 top[i->point]=i->point; 301 u=i->point; 302 } 303 else 304 { 305 if (u==1) 306 break; 307 u=f[u]; 308 } 309 } 310 } 311 inline int lca(int u, int v) 312 { 313 if (d[u]<d[v]) swap(u, v); 314 while (int dist=d[u]-d[v]) 315 u=a[__builtin_ctz(dist)][u]; 316 if (u==v) return u; 317 for (int i=19;i>=0;i--) 318 if (a[i][u]!=a[i][v]) 319 u=a[i][u], 320 v=a[i][v]; 321 return a[0][u]; 322 } 323 inline int anc(int u, int d) 324 { 325 for (int i=19;i>=0;i--) 326 if ((d>>i)&1) 327 u=a[i][u]; 328 return u; 329 } 330 inline void update(int u, int v, int c) 331 { 332 while (top[u]!=top[v]) 333 { 334 if (d[top[u]]<d[top[v]]) swap(u, v); 335 update(t, 1, n, idx[top[u]], idx[u], c); 336 u=f[top[u]]; 337 } 338 if (d[u]>d[v]) swap(u, v); 339 update(t, 1, n, idx[u], idx[v], c); 340 } 341 inline node query(int u, int a) 342 { 343 node ret; 344 345 while (top[u]!=top[a]) 346 { 347 ret=merge(query(t, 1, n, idx[top[u]], idx[u]), ret); 348 u=f[top[u]]; 349 } 350 ret=merge(query(t, 1, n, idx[a], idx[u]), ret); 351 352 return ret; 353 }
1 #include <cstdio> 2 #include <cstring> 3 typedef long long llint; 4 typedef unsigned int uint; 5 6 namespace IO 7 { 8 const int sizeOfInput=33554432; 9 char inputBuffer[sizeOfInput], * port=inputBuffer; 10 11 inline void assign(); 12 inline void close(); 13 inline char getch(); 14 inline uint getint(); 15 inline void putint(uint); 16 }; 17 18 namespace random 19 { 20 llint num, seed, mod; 21 inline void srand(); 22 inline int getrand(); 23 }; 24 25 namespace treap 26 { 27 const int sizeOfMemory=262144; 28 uint C[11][11]; 29 uint P[sizeOfMemory][11]; 30 31 struct node 32 { 33 uint c[11]; 34 uint key, size; 35 int weight; 36 node * left, * right; 37 38 inline node(); 39 inline void maintain(); 40 }; 41 node * null=new node(); 42 node memory[sizeOfMemory], * port=memory; 43 44 inline void prepare(); 45 inline node * newnode(uint); 46 inline void update(node * , uint); 47 void split(node * , uint, node *& , node *& ); 48 node * merge(node * , node * ); 49 }; 50 51 int main() 52 { 53 using namespace treap; 54 using namespace IO; 55 56 node * root=null; 57 node * L, * M, * R; 58 int N, Q; 59 int pos, val, l, r, k; 60 char ch; 61 62 assign(); 63 random::srand(); 64 prepare(); 65 66 N=getint(); 67 for (int i=0;i<N;i++) 68 root=merge(root, newnode(getint())); 69 70 for (Q=getint();Q;Q--) 71 { 72 ch=getch(); 73 if (ch=='I') 74 { 75 pos=getint(), val=getint(); 76 M=treap::newnode(val); 77 split(root, pos, L, R); 78 L=merge(L, M); 79 root=merge(L, R); 80 } 81 else if (ch=='D') 82 { 83 pos=getint(); 84 split(root, pos, L, R); 85 split(R, 1, M, R); 86 root=merge(L, R); 87 } 88 else if (ch=='R') 89 { 90 pos=getint(), val=getint(); 91 split(root, pos, L, R); 92 split(R, 1, M, R); 93 update(M, val); 94 L=merge(L, M); 95 root=merge(L, R); 96 } 97 else 98 { 99 l=getint(), r=getint(), k=getint(); 100 split(root, r+1, L, R); 101 split(L, l, L, M); 102 putint(M->c[k]); 103 L=merge(L, M); 104 root=merge(L, R); 105 } 106 } 107 108 return 0; 109 } 110 111 namespace IO 112 { 113 inline void assign() 114 { 115 freopen("GSS8.in", "r", stdin); 116 freopen("GSS8.out", "w", stdout); 117 fread(inputBuffer, 1, 33554432, stdin); 118 } 119 inline void close() 120 { 121 fclose(stdin); 122 fclose(stdout); 123 } 124 inline char getch() 125 { 126 register char ch; 127 do ch=*(port++); while (ch<'A' || ch>'Z'); 128 return ch; 129 } 130 inline uint getint() 131 { 132 register uint num=0; 133 register char ch; 134 do ch=*(port++); while (ch<'0' || ch>'9'); 135 do num=num*10+ch-'0', ch=*(port++); while (ch>='0' && ch<='9'); 136 return num; 137 } 138 inline void putint(uint num) 139 { 140 char stack[16]; 141 register int top=0; 142 if (num==0) stack[top=1]='0'; 143 for ( ;num;num/=10) stack[++top]=num%10+'0'; 144 for ( ;top;top--) putchar(stack[top]); 145 putchar('\n'); 146 } 147 } 148 149 namespace random 150 { 151 inline void srand() 152 { 153 num=1, seed=48271, mod=2147483647; 154 for (int i=1;i<=233;i++) 155 getrand(); 156 } 157 inline int getrand() 158 { 159 num=num*seed%mod; 160 return num; 161 } 162 } 163 164 namespace treap 165 { 166 inline node::node() 167 { 168 memset(c, 0, sizeof(c)); 169 key=size=0; 170 weight=0; 171 left=right=this; 172 } 173 inline void node::maintain() 174 { 175 int tmp=left->size+1; 176 size=left->size+1+right->size; 177 178 for (int i=0;i<=10;i++) c[i]=left->c[i]; 179 for (int i=0;i<=10;i++) c[i]+=key*P[tmp][i]; 180 for (int i=0;i<=10;i++) for (int j=0;j<=i;j++) 181 c[i]+=C[i][j]*P[tmp][i-j]*right->c[j]; 182 } 183 inline void prepare() 184 { 185 C[0][0]=1; 186 for (int i=1;i<=10;i++) 187 { 188 C[i][0]=1; 189 for (int j=1;j<i;j++) 190 C[i][j]=C[i-1][j-1]+C[i-1][j]; 191 C[i][i]=1; 192 } 193 194 for (int i=0;i<sizeOfMemory;i++) 195 { 196 P[i][0]=1; 197 for (int j=1;j<=10;j++) 198 P[i][j]=P[i][j-1]*i; 199 } 200 } 201 inline node * newnode(uint _key) 202 { 203 node * ret=port++; 204 for (int i=0;i<=10;i++) ret->c[i]=_key; 205 ret->key=_key, ret->size=1; 206 ret->weight=random::getrand(); 207 ret->left=ret->right=null; 208 return ret; 209 } 210 inline void update(node * t, uint _key) 211 { 212 for (int i=0;i<=10;i++) t->c[i]=_key; 213 t->key=_key; 214 } 215 void split(node * t, uint k, node *& l, node *& r) 216 { 217 if (t==null) l=r=null; 218 else 219 { 220 if (t->left->size<k) 221 { 222 l=t; 223 split(t->right, k-t->left->size-1, t->right, r); 224 l->maintain(); 225 } 226 else 227 { 228 r=t; 229 split(t->left, k, l, t->left); 230 r->maintain(); 231 } 232 } 233 } 234 node * merge(node * l, node * r) 235 { 236 if (l==null) return r->maintain(), r; 237 if (r==null) return l->maintain(), l; 238 if (l->weight>r->weight) 239 { 240 l->right=merge(l->right, r); 241 l->maintain(); 242 return l; 243 } 244 else 245 { 246 r->left=merge(l, r->left); 247 r->maintain(); 248 return r; 249 } 250 } 251 }