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. Service官方教程(2)*IntentService与Service示例、onStartCommand()3个返回值的含义。

    1.Creating a Started Service A started service is one that another component starts by calling start ...

  2. 224 Basic Calculator 基本计算器

    实现一个基本的计算器来计算一个简单的字符串表达式. 字符串表达式可以包含左括号 ( ,右括号),加号+ ,减号 -,非负整数和空格 . 假定所给的表达式语句总是正确有效的. 例如: "1 + ...

  3. thinkphp3.2.3连接sqlserver 2008 R2 数据库

    环境: 操作系统——win7 64位旗舰版 PHP——thinkphp 3.23 数据库——Microsoft SQL Server 2008 R2 需要用到的软件: 步骤: 1.搜索SQLSRV30 ...

  4. [BZOJ2330][SCOI2011]糖果 差分约束系统+最短路

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2330 类似于题目中这种含有不等式关系,我们可以建立差分约束系统来跑最长路或最短路. 对于一 ...

  5. Unity笔记(4)自学第六天

    今天主要是写了demo的策划案 [关卡设计部分]: [关卡数值设计]:

  6. Android Studio中找出不再使用的资源

    顶部Analyze菜单中选择Run Inspection by Name 在弹出的输入框中输入unused resources

  7. QML中使用相对路径

    QML里有三种路径: 默认使用URL路径. "qrc:///filepath".这用来索引资源文件. "file:///绝对路径".这用来索引本地文件系统中的文 ...

  8. SOA测试之浏览器插件

    1. Chrome HTTP Rest Client 插件: 1.1 Postman: https://chrome.google.com/webstore/detail/postman-rest-c ...

  9. Farseer.net轻量级ORM开源框架 V1.x 入门篇:数据库上下文

    导航 目   录:Farseer.net轻量级ORM开源框架 目录 上一篇:Farseer.net轻量级ORM开源框架 V1.x 入门篇:数据库配置文件 下一篇:Farseer.net轻量级ORM开源 ...

  10. JAVA 学习笔记 - 反射机制

    1.   JAVA反射机制的概念 2. 怎样实例化一个 Class对象 Class.forName(包名.类名); 对象.getClass(); 类.class; ================== ...