Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define SET(x, a) memset(x, a, sizeof x )
  5. #define pll pair<ll,ll>
  6. #define INF 1001
  7. #define pii pair<int, int>
  8. // const double pi = acos(-1);
  9. #define MAXN 2850003
  10. struct splaytree{
  11. struct node{
  12. node * ch[2];
  13. node * f;
  14. ll sz, maxsum, mls, mrs, val, sum;
  15. bool same, rev;
  16. int lsize () {return ch[0]->sz;}
  17. int rsize () {return ch[1]->sz;}
  18. } * root, * null, *lb, *rb, SS[MAXN];
  19. int SC;
  20. node * newnode(int value, node * father){
  21. node * e = SS+ ++SC;
  22. e->f = father;
  23. e->maxsum = e->mls = e->mrs = e->val = e->sum = value;
  24. e->sz = 1;
  25. e->same = e->rev = false;
  26. e->ch[1] = e->ch[0] = null;
  27. return e;
  28. }
  29. void init(){
  30. SC = 0;
  31. null = newnode(-INF, 0);
  32. null->sz = 0;
  33. lb = root = newnode(-INF, null);
  34. rb = root->ch[1] = newnode(-INF, root);
  35. null->sum = lb->sum = rb->sum = 0;
  36. update(root);
  37. }
  38. void update(node * x){
  39. if(x == null) return;
  40. x->sz = x->ch[0]->sz + x->ch[1]->sz + 1;
  41. x->sum = x->val;
  42. if(x->ch[0]->val == -INF) x->sum += x->ch[0]->sum;
  43. if(x->ch[1]->val == -INF) x->sum += x->ch[1]->sum;
  44. // x->sum = x->ch[0]->sum + x->ch[1]->sum + x->val;
  45. x->mrs = x->val + x->ch[1]->sum;
  46. x->mrs = max(x->mrs, max(x->ch[1]->sum + x->val + x->ch[0]->mrs, x->ch[1]->mrs));
  47. x->mls = x->val + x->ch[0]->sum;
  48. x->mls = max(x->mls, max(x->ch[0]->sum + x->val + x->ch[1]->mls, x->ch[0]->mls));
  49. x->maxsum = x->val;
  50. x->maxsum = max(x->maxsum, max(x->ch[0]->maxsum, max(x->ch[1]->maxsum, x->ch[1]->mls + x->ch[0]->mrs + x->val)));
  51. x->maxsum = max(x->maxsum, max(x->mls + x->val, x->mrs + x->val));
  52. cout << x->lsize() << ' ' << x->rsize() << ' ' << x->sum << endl;
  53. }
  54. void pushdown(node * x){
  55. if(x == null) return;
  56. if(x->rev){
  57. x->rev = 0;
  58. node * tmp = x->ch[0];
  59. x->ch[0] = x->ch[1];
  60. x->ch[1] = tmp;
  61. x->ch[0]->rev ^= 1;
  62. x->ch[1]->rev ^= 1;
  63. swap(x->mls, x->mrs);
  64. }
  65. if(x->same){
  66. x->same=false;
  67. x->ch[0]->same=x->ch[1]->same=true;
  68. x->ch[0]->val=x->ch[1]->val=x->val;
  69. x->sum = x->maxsum = x->mls = x->mrs = x->val * x->sz;
  70. if(x->val < 0)
  71. x->maxsum = x->mls = x->mrs = x->val;
  72. }
  73. }
  74. void rotate(node *x,int o){
  75. node *y=x->f;
  76. pushdown(y->ch[!o]);
  77. pushdown(x->ch[0]);
  78. pushdown(x->ch[1]);
  79. y->ch[o] = x->ch[!o];
  80. y->ch[o]->f=y;
  81. x->f = y->f;
  82. if(y->f->ch[0]==y)
  83. y->f->ch[0]=x;
  84. else
  85. y->f->ch[1]=x;
  86. y->f=x;
  87. x->ch[!o]=y;
  88. update(y);
  89. update(x);
  90. if (root==y) root=x;
  91. }
  92. void Delete(int pos, int tot){
  93. select(pos, null);
  94. select(pos + tot + 1, root);
  95. root->ch[1]->ch[0] = null;
  96. splay(root->ch[1], null);
  97. }
  98. void splay(node * x, node * y){
  99. pushdown(x);
  100. while(x->f != y){
  101. if(x->f->f == y){
  102. if(x->f->ch[0] == x) rotate(x, 0);
  103. else rotate(x, 1);
  104. }else if(x->f->f->ch[0] == x->f){
  105. if(x->f->ch[0] == x) rotate(x->f, 0), rotate(x, 0);
  106. else rotate(x,1), rotate(x, 0);
  107. }else{
  108. if(x->f->ch[1] == x) rotate(x->f, 1), rotate(x, 1);
  109. else rotate(x, 0), rotate(x, 1);
  110. }
  111. }
  112. }
  113. void select(int k, node * f){
  114. node * x = root;
  115. pushdown(x);
  116. // cout << x->lsize() << ' ' << x->rsize() << endl;
  117. // cout << x->ch[0]->val << ' ' << x->ch[1]->val << endl;
  118. for(;k != (x->lsize() + 1);){
  119. if(k <= x->lsize()) x = x->ch[0];
  120. else{
  121. k -= x->lsize() + 1;
  122. x = x->ch[1];
  123. }
  124. pushdown(x);
  125. }
  126. splay(x, f);
  127. }
  128. void insert(int pos, int tot, int *C){
  129. node *t, *z;
  130. z = t = newnode(C[1], null);
  131. for(int i = 2; i <= tot; ++i)
  132. z = z->ch[1] = newnode(C[i], z);
  133. select(pos + 1, null);
  134. select(pos + 2, root);
  135. // cout << "hi" << endl;
  136. root->ch[1]->ch[0] = t;
  137. t->f = root->ch[1];
  138. // cout << "hi" << endl;
  139. splay(z, null);
  140. }
  141. void MakeSame(int pos,int tot,int value){
  142. select(pos,null);
  143. select(pos+tot+1,root);
  144. root->ch[1]->ch[0]->same=true;
  145. root->ch[1]->ch[0]->val=value;
  146. splay(root->ch[1]->ch[0],null);
  147. }
  148. void Reverse(int pos,int tot){
  149. select(pos,null);
  150. select(pos+tot+1,root);
  151. root->ch[1]->ch[0]->rev=!root->ch[1]->ch[0]->rev;
  152. splay(root->ch[1]->ch[0],null);
  153. }
  154. int GetSum(int pos,int tot){
  155. select(pos,null);
  156. select(pos+tot+1,root);
  157. pushdown(root->ch[1]->ch[0]);
  158. return root->ch[1]->ch[0]->sum;
  159. }
  160. int MaxSum(){
  161. pushdown(root);
  162. update(root);
  163. cout << root->maxsum << ' ' << root->mls << ' ' << root->mrs << endl;
  164. cout << root->lsize() << ' ' << root->rsize() << endl;
  165. return root->maxsum;
  166. }
  167. }Splay;
  168. int C[500001];
  169. int main(){
  170. ios::sync_with_stdio(false);
  171. cin.tie(0);
  172. int n, m; cin >> n >> m;
  173. for(int i = 1; i <= n; ++i)
  174. cin >> C[i];
  175. Splay.init();
  176. Splay.insert(0, n, C);
  177. while(m--){
  178. string cmd; cin >> cmd;
  179. if(cmd[0] == 'I'){
  180. int len, pos; cin >> pos >> len;
  181. for(int i = 1; i <= len;++i)
  182. cin >> C[i];
  183. Splay.insert(pos, len, C);
  184. }else if(cmd[0] == 'D'){
  185. int pos, tot; cin >> pos >> tot;
  186. Splay.Delete(pos, tot);
  187. }else if(cmd == "MAKE-SAME"){
  188. int pos, tot,val; cin >> pos >> tot >> val;
  189. Splay.MakeSame(pos, tot, val);
  190. }else if(cmd[0] == 'R'){
  191. int pos, tot; cin >> pos >> tot;
  192. Splay.Reverse(pos, tot);
  193. }else if(cmd[0] == 'G'){
  194. int pos, tot; cin >> pos >> tot;
  195. cout << Splay.GetSum(pos, tot) << endl;
  196. }else{
  197. cout << Splay.MaxSum() << endl;
  198. }
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement