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. 51nod1183 编辑距离

    1183 编辑距离 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 编辑距离,又称Levenshtein距离(也叫做Edit Distance),是指两个 ...

  2. 题解报告:hdu 2844 & poj 1742 Coins(多重部分和问题)

    Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. On ...

  3. FileStream和BinaryReader,BinaryWriter,StreamReader,StreamWriter的区别

    FileStream对于在文件系统上读取和写入文件非常有用,FileStream缓存输入和输出,以获得更好的性能.FileStream对象表示在磁盘或网络路径上指向文件的流.这个类提供了在文件中读写字 ...

  4. jQuery相关知识总结

    1 encodeURIComponent(city)处理js传值乱码问题 2 总体概述 以后项目如果没有特殊情况,一般采用jQuery作为最基础的公共底层库. 另外对于前端的javascript相关的 ...

  5. for循环的两种写法哪个快

    结果如下: 其实工作中,也没有这么多数据需要遍历,基本上用foreach

  6. 有关HTML版本

    先说说HTML的简史:从HTML1.0~2.0(1989~1991)>HTML3(1995)>HTML4(1998)>HTML4.01(1999)>XHTML1.0(2001) ...

  7. iOS Programming Camera 1

     iOS Programming Camera  1 1 Displaying Images and UIImageView 1.1  put an instance of UIImageView o ...

  8. IntelliJ IDEA导入JDK出现The selected directory is not a valid home for JDK问题的解决方法

    JDK版本与IDEA版本不兼容: JDK版本过高可能会造成这个问题,需与IDEA相兼容的JDK才行. 比如,用IDEA2016.3.8版本的,JDK用jdk-10.0.1_windows-x64_bi ...

  9. vscode显示php函数列表

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

  10. call和apply和bind的区别

    在 javascript 中,call 和 apply 都是为了改变某个函数运行时的上下文(context)而存在的,换句话说,就是为了改变函数体内部 this 的指向. JavaScript 的一大 ...