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. 题解报告:hdu 1406 完数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1406 Problem Description 完数的定义:如果一个大于1的正整数的所有因子之和等于它的 ...

  2. 在Eclipse+ADT中开发Android系统的内置应用

    转自:  http://www.iteye.com/topic/1050439 在Eclipse+ADT中开发Android系统的内置应用 Android系统内置有:Browser(浏览器).Mms( ...

  3. 08 H5新增input元素

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  4. 在reset css后两个input之间还是出现默认间隔的问题。

    <div class="search_box fl"> <input type="text" class="search_text& ...

  5. @GetMapping和@PostMapping 和@RequestMapping区别

    @GetMapping 用于将HTTP GET请求映射到特定处理程序方法的注释. 具体来说,@GetMapping是一个作为快捷方式的组合注释@RequestMapping(method = Requ ...

  6. 使用 Java 发送邮件

    在我们的应用程序中有时需要给用户发送邮件,例如激活邮件.通知邮件等等.那么如何使用 Java 来给用户发送邮件呢? 使用 java 代码发送邮件 使用工具类发送邮件 使用Spring进行整合发送邮件 ...

  7. greendao3.2.3配置时遇到的问题

    这两天我一直在研究greendao这个框架,我在GitHub下载了 greendao3.2.2:https://github.com/greenrobot/greenDAO,照着网址里面来配置: // ...

  8. pthread Win32多线程编程的一些知识和感想

    研究遗传算法的一大诟病就是每次运行程序的结果并不是完全一样的,有时候能找到最优解有时候找不到最优解,这就是遗传算法的概率性导致的.那么怎么评价你的方法的好坏呐,这时候就要多次独立运行程序最后取结果的平 ...

  9. spring-shiro 配置

    配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...

  10. 6-Java-C(无穷分数)

    题目描述: 无穷的分数,有时会趋向于固定的数字. 请计算[图1.jpg]所示的无穷分数,要求四舍五入,精确到小数点后5位,小数位不足的补0. 请填写该浮点数,不能填写任何多余的内容. 正确算法: 此题 ...