SPOJ GSS6 Can you answer these queries VI

Can you answer these queries VI

Time Limit: 2000ms

Memory Limit: 262144KB

This problem will be judged on SPOJ. Original ID: GSS6
64-bit integer IO format: %lld      Java class name: Main

Given a sequence A of N (N <= 100000) integers, you have to apply Q (Q <= 100000) operations:

Insert, delete, replace an element, find the maximum contiguous(non empty) sum in a given interval.

Input

The first line of the input contains an integer N.
The following line contains N integers, representing the starting
sequence A1..AN, (|Ai| <= 10000).

The third line contains an integer Q. The next Q lines contains the operations in following form:

I x y: insert element y at position x (between x - 1 and x).
D x  : delete the element at position x.
R x y: replace element at position x with y.
Q x y: print max{Ai + Ai+1 + .. + Aj | x <= i <= j <= y}.

All given positions are valid, and given values are between -10000 and +10000.

The sequence will never be empty.

Output

For each "Q" operation, print an integer(one per line) as described above.

Example

Input:53 -4 3 -1 610I 6 2Q 3 5R 5 -4Q 3 5D 2Q 1 5I 2 -10Q 1 6R 2 -1Q 1 6

Output:83635

Source

my own

解题:splay

  1 #include <bits/stdc++.h>
  2 #define KT ch[ch[root][1]][0]
  3 using namespace std;
  4 const int INF = numeric_limits<int>::max();
  5 const int maxn = 210010;
  6
  7 struct SplayTree {
  8     int fa[maxn],ch[maxn][2],sz[maxn],key[maxn];
  9     int lsum[maxn],rsum[maxn],ans[maxn],sum[maxn];
 10     int tot,root,seq[maxn];
 11     inline void pushup(int x) {
 12         if(!x) return;
 13         sz[x] = 1 + sz[ch[x][0]] + sz[ch[x][1]];
 14         sum[x] = key[x] + sum[ch[x][0]] + sum[ch[x][1]];
 15         lsum[x] = max(lsum[ch[x][0]],key[x] + sum[ch[x][0]] + max(0,lsum[ch[x][1]]));
 16         rsum[x] = max(rsum[ch[x][1]],key[x] + sum[ch[x][1]] + max(0,rsum[ch[x][0]]));
 17         ans[x] = max(max(ans[ch[x][0]],ans[ch[x][1]]),key[x] + max(0,rsum[ch[x][0]]) + max(0,lsum[ch[x][1]]));
 18     }
 19     void newnode(int &x,int val,int f) {
 20         x = ++tot;
 21         lsum[x] = rsum[x] = ans[x] = sum[x] = key[x] = val;
 22         fa[x] = f;
 23         ch[x][0] = ch[x][1] = 0;
 24         sz[x] = 1;
 25     }
 26     void build(int &x,int L,int R,int f) {
 27         if(L > R) return;
 28         int mid = (L + R)>>1;
 29         newnode(x,seq[mid],f);
 30         build(ch[x][0],L,mid-1,x);
 31         build(ch[x][1],mid+1,R,x);
 32         pushup(x);
 33     }
 34     void init(int n) {
 35         tot = root = 0;
 36         ch[0][0] = ch[0][1] = fa[0] = sum[0] = 0;
 37         lsum[0] = rsum[0] = ans[0] = key[0] = -INF;
 38         newnode(root,-INF,0);
 39         newnode(ch[root][1],-INF,root);
 40         build(KT,1,n,ch[root][1]);
 41         pushup(ch[root][1]);
 42         pushup(root);
 43     }
 44     void rotate(int x,int kd) {
 45         int y = fa[x];
 46         ch[y][kd^1] = ch[x][kd];
 47         fa[ch[x][kd]] = y;
 48         fa[x] = fa[y];
 49         ch[x][kd] = y;
 50         fa[y] = x;
 51         if(fa[x]) ch[fa[x]][y == ch[fa[x]][1]] = x;
 52         pushup(y);
 53     }
 54     void splay(int x,int goal = 0) {
 55         while(fa[x] != goal) {
 56             if(fa[fa[x]] == goal) rotate(x,x == ch[fa[x]][0]);
 57             else {
 58                 int y = fa[x],z = fa[y],s = (y == ch[z][0]);
 59                 if(x == ch[y][s]) {
 60                     rotate(x,s^1);
 61                     rotate(x,s);
 62                 } else {
 63                     rotate(y,s);
 64                     rotate(x,s);
 65                 }
 66             }
 67         }
 68         pushup(x);
 69         if(!goal) root = x;
 70     }
 71     int select(int k,int goal) {
 72         int x = root;
 73         while(sz[ch[x][0]] + 1 != k) {
 74             if(k < sz[ch[x][0]] + 1) x = ch[x][0];
 75             else {
 76                 k -= sz[ch[x][0]] + 1;
 77                 x = ch[x][1];
 78             }
 79         }
 80         splay(x,goal);
 81         return x;
 82     }
 83     void insert(int a,int b) {
 84         select(a - 1 + 1,0);
 85         select(a + 1,root);
 86         newnode(KT,b,ch[root][1]);
 87         pushup(ch[root][1]);
 88         pushup(root);
 89     }
 90     void remove(int a) {
 91         select(a - 1 + 1,0);
 92         select(a + 1 + 1,root);
 93         KT = 0;
 94         pushup(ch[root][1]);
 95         pushup(root);
 96     }
 97     void replace(int a,int b){
 98         int x = root;
 99         ++a;
100         while(sz[ch[x][0]] + 1 != a){
101             if(a < sz[ch[x][0]] + 1) x = ch[x][0];
102             else{
103                 a -= sz[ch[x][0]] + 1;
104                 x = ch[x][1];
105             }
106         }
107         key[x] = b;
108         splay(x,0);
109     }
110     int query(int a,int b){
111         select(a-1+1,0);
112         select(b+1+1,root);
113         return ans[KT];
114     }
115 } spt;
116 int main() {
117     int n,m,x,y;
118     char op[10];
119     while(~scanf("%d",&n)) {
120         for(int i = 1; i <= n; ++i)
121             scanf("%d",&spt.seq[i]);
122         spt.init(n);
123         scanf("%d",&m);
124         while(m--) {
125             scanf("%s",op);
126             if(op[0] == ‘I‘) {
127                 scanf("%d%d",&x,&y);
128                 spt.insert(x,y);
129             } else if(op[0] == ‘D‘) {
130                 scanf("%d",&x);
131                 spt.remove(x);
132             }else if(op[0] == ‘R‘){
133                 scanf("%d%d",&x,&y);
134                 spt.replace(x,y);
135             }else if(op[0] == ‘Q‘){
136                 scanf("%d%d",&x,&y);
137                 printf("%d\n",spt.query(x,y));
138             }
139         }
140     }
141     return 0;
142 }

时间: 2024-10-25 14:07:28

SPOJ GSS6 Can you answer these queries VI的相关文章

SPOJ GSS6 Can you answer these queries VI ——Splay

[题目分析] 增加了插入和删除. 直接用Splay维护就好辣! 写了一个晚上,(码力不精),最后发现更新写挂了 [代码] #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <map> #include <set> #include <queue> #include <string> #include

SPOJ 4487. Can you answer these queries VI splay

题目链接:点击打开链接 题意比较明显,不赘述. 删除时可以把i-1转到根,把i+1转到根下 则i点就在 根右子树 的左子树,且只有i这一个 点 #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> using namespace std; #define N 300500 #define inf 10000000 #define L(x) tree[x].ch[

spoj 4487. Can you answer these queries VI splay 常数优化

4487. Can you answer these queries VI Problem code: GSS6 Given a sequence A of N (N <= 100000) integers, you have to apply Q (Q <= 100000) operations: Insert, delete, replace an element, find the maximum contiguous(non empty) sum in a given interval

SPOJ 1043 Can you answer these queries I 求任意区间最大连续子段和 线段树

题目链接:点击打开链接 维护区间左起连续的最大和,右起连续的和.. #include <cstdio> #include <iostream> #include <algorithm> #include <string.h> #include <math.h> #include <vector> #include <map> using namespace std; #define N 50050 #define Lson

SPOJ GSS3 Can you answer these queries III (线段树)

题目大意: 求区间最大子区间的和. 思路分析: 记录左最大,右最大,区间最大. 注意Q_L  和 Q_R  就好. #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #define lson num<<1,s,mid #define rson num<<1|1,mid+1,e #define maxn 55555 using na

SPOJ GSS4 Can you answer these queries IV (线段树)

题目大意: 给出N个数 0     操作   把 l -----  r之间的数全部开平方 1     操作  输出 l -----r  之间的和 思路分析: 判断区间里的数字是否全相同.如果相同, 将cov 置为该数 查询的时候和更新的时候,如果碰到cov != -1 的  就直接返回就可以了 #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #incl

spoj gss2 : Can you answer these queries II 离线&amp;&amp;线段树

1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang Zhe cannot solve but get Wrong Answer from most of the OI problems. And he refuse to write two program of same kind at all. So he always failes in co

SPOJ GSS5 Can you answer these queries V (线段树)

原来有一两个人说我不帅的时候,我不以为意,逗我玩而已,后来几乎所有 人都说我不帅,我才真正意识到事态的严重,这社会骗子真是越来越多了... 好吧我承认,这个笑话不好笑,其实我想说的是,做人一定要坚持自己的原则, 哪怕有一天所有人都和你背道而驰,都不要放弃自己当初的梦想,如果有一天, 我们淹没在人海之中,庸碌一生,那是因为我们不够努力,不够勇敢的去面对生活. 每天积累一点点,嗯,满足简单的快乐. ---------------------------------------------------

bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树

2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 145  Solved: 76[Submit][Status][Discuss] Description 给定n个元素的序列. 给出m个询问:求l[i]~r[i]的最大子段和(可选空子段). 这个最大子段和有点特殊:一个数字在一段中出现了两次只算一次. 比如:1,2,3,2,2,2出现了3次,但只算一次,