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:
5
3 -4 3 -1 6
10
I 6 2
Q 3 5
R 5 -4
Q 3 5
D 2
Q 1 5
I 2 -10
Q 1 6
R 2 -1
Q 1 6 Output:
8
3
6
3
5
 

Source

 
解题:splay
 #include <bits/stdc++.h>
#define KT ch[ch[root][1]][0]
using namespace std;
const int INF = numeric_limits<int>::max();
const int maxn = ; struct SplayTree {
int fa[maxn],ch[maxn][],sz[maxn],key[maxn];
int lsum[maxn],rsum[maxn],ans[maxn],sum[maxn];
int tot,root,seq[maxn];
inline void pushup(int x) {
if(!x) return;
sz[x] = + sz[ch[x][]] + sz[ch[x][]];
sum[x] = key[x] + sum[ch[x][]] + sum[ch[x][]];
lsum[x] = max(lsum[ch[x][]],key[x] + sum[ch[x][]] + max(,lsum[ch[x][]]));
rsum[x] = max(rsum[ch[x][]],key[x] + sum[ch[x][]] + max(,rsum[ch[x][]]));
ans[x] = max(max(ans[ch[x][]],ans[ch[x][]]),key[x] + max(,rsum[ch[x][]]) + max(,lsum[ch[x][]]));
}
void newnode(int &x,int val,int f) {
x = ++tot;
lsum[x] = rsum[x] = ans[x] = sum[x] = key[x] = val;
fa[x] = f;
ch[x][] = ch[x][] = ;
sz[x] = ;
}
void build(int &x,int L,int R,int f) {
if(L > R) return;
int mid = (L + R)>>;
newnode(x,seq[mid],f);
build(ch[x][],L,mid-,x);
build(ch[x][],mid+,R,x);
pushup(x);
}
void init(int n) {
tot = root = ;
ch[][] = ch[][] = fa[] = sum[] = ;
lsum[] = rsum[] = ans[] = key[] = -INF;
newnode(root,-INF,);
newnode(ch[root][],-INF,root);
build(KT,,n,ch[root][]);
pushup(ch[root][]);
pushup(root);
}
void rotate(int x,int kd) {
int y = fa[x];
ch[y][kd^] = ch[x][kd];
fa[ch[x][kd]] = y;
fa[x] = fa[y];
ch[x][kd] = y;
fa[y] = x;
if(fa[x]) ch[fa[x]][y == ch[fa[x]][]] = x;
pushup(y);
}
void splay(int x,int goal = ) {
while(fa[x] != goal) {
if(fa[fa[x]] == goal) rotate(x,x == ch[fa[x]][]);
else {
int y = fa[x],z = fa[y],s = (y == ch[z][]);
if(x == ch[y][s]) {
rotate(x,s^);
rotate(x,s);
} else {
rotate(y,s);
rotate(x,s);
}
}
}
pushup(x);
if(!goal) root = x;
}
int select(int k,int goal) {
int x = root;
while(sz[ch[x][]] + != k) {
if(k < sz[ch[x][]] + ) x = ch[x][];
else {
k -= sz[ch[x][]] + ;
x = ch[x][];
}
}
splay(x,goal);
return x;
}
void insert(int a,int b) {
select(a - + ,);
select(a + ,root);
newnode(KT,b,ch[root][]);
pushup(ch[root][]);
pushup(root);
}
void remove(int a) {
select(a - + ,);
select(a + + ,root);
KT = ;
pushup(ch[root][]);
pushup(root);
}
void replace(int a,int b){
int x = root;
++a;
while(sz[ch[x][]] + != a){
if(a < sz[ch[x][]] + ) x = ch[x][];
else{
a -= sz[ch[x][]] + ;
x = ch[x][];
}
}
key[x] = b;
splay(x,);
}
int query(int a,int b){
select(a-+,);
select(b++,root);
return ans[KT];
}
} spt;
int main() {
int n,m,x,y;
char op[];
while(~scanf("%d",&n)) {
for(int i = ; i <= n; ++i)
scanf("%d",&spt.seq[i]);
spt.init(n);
scanf("%d",&m);
while(m--) {
scanf("%s",op);
if(op[] == 'I') {
scanf("%d%d",&x,&y);
spt.insert(x,y);
} else if(op[] == 'D') {
scanf("%d",&x);
spt.remove(x);
}else if(op[] == 'R'){
scanf("%d%d",&x,&y);
spt.replace(x,y);
}else if(op[] == 'Q'){
scanf("%d%d",&x,&y);
printf("%d\n",spt.query(x,y));
}
}
}
return ;
}

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

  1. SPOJ GSS6 Can you answer these queries VI ——Splay

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

  2. spoj 4487. Can you answer these queries VI (gss6) splay 常数优化

    4487. Can you answer these queries VI Problem code: GSS6 Given a sequence A of N (N <= 100000) in ...

  3. SP4487 GSS6 - Can you answer these queries VI

    题目大意 给出一个由N个整数组成的序列A,你需要应用M个操作: I p x 在 p  处插入插入一个元素 x D p 删除 p 处的一个元素 R p x 修改 p 处元素的值为 x Q l r 查询一 ...

  4. SPOJ 4487. Can you answer these queries VI splay

    题目链接:点击打开链接 题意比較明显,不赘述. 删除时能够把i-1转到根,把i+1转到根下 则i点就在 根右子树 的左子树,且仅仅有i这一个 点 #include<stdio.h> #in ...

  5. GSS6 4487. Can you answer these queries VI splay

    GSS6 Can you answer these queries VI 给出一个数列,有以下四种操作: I x y: 在位置x插入y.D x  : 删除位置x上的元素.R x y: 把位置x用y取替 ...

  6. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  7. GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树

    GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...

  8. [题解] SPOJ GSS1 - Can you answer these queries I

    [题解] SPOJ GSS1 - Can you answer these queries I · 题目大意 要求维护一段长度为 \(n\) 的静态序列的区间最大子段和. 有 \(m\) 次询问,每次 ...

  9. SPOJ 1557. Can you answer these queries II 线段树

    Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...

随机推荐

  1. K - KazaQ’s Socks

    Bryce1010模板 #include <bits/stdc++.h> using namespace std; #define LL long long int main() { LL ...

  2. ACM牛人博客

    ACM牛人博客 kuangbin kuangbin(新) wuyiqi wuyiqi(新) ACM!荣耀之路! 九野的博客 传说中的ACM大牛!!! read more

  3. Python快速教程(转载)

    Python快速教程   作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 怎么能快速地掌握Python?这是和朋友闲聊时谈起的问题 ...

  4. 提交应用 Windows Phone的应用程序认证要求

    本文介绍了 Windows Phone 应用程序或游戏要通过认证并在 Windows Phone Marketplace 中发布而必须满足的策略和技术要求. 1.0 计划概述 设计认证过程的一个核心原 ...

  5. CDN概述

  6. vscode显示php函数列表

    1.安装插件支持 https://marketplace.visualstudio.com/items?itemName=linyang95.php-symbols 2.ctrt+shift+o 即可 ...

  7. Node.js——req、res对象

    requset对象类型<http.IncomingMessage>,继承stream.Readable类 requset对象: req.headers req.rawHeaders req ...

  8. ubuntu 下安装redis

    获取Redis 1.通过官网http://redis.io/获取稳定版源码包下载地址: 2.通过wget http://download.redis.io/releases/redis-3.0.2.t ...

  9. CSS3 自动旋转

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. vs code 格式化 美化 html js css 插件 Beautify

    安装 Beautify 插件 然后 F1 输入 Beautify file 回车即可