SPOJ GSS6 Can you answer these queries VI
Can you answer these queries VI
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
#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的更多相关文章
- SPOJ GSS6 Can you answer these queries VI ——Splay
[题目分析] 增加了插入和删除. 直接用Splay维护就好辣! 写了一个晚上,(码力不精),最后发现更新写挂了 [代码] #include <cstdio> #include <cs ...
- 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 ...
- 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 查询一 ...
- SPOJ 4487. Can you answer these queries VI splay
题目链接:点击打开链接 题意比較明显,不赘述. 删除时能够把i-1转到根,把i+1转到根下 则i点就在 根右子树 的左子树,且仅仅有i这一个 点 #include<stdio.h> #in ...
- 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取替 ...
- 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 ...
- GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树
GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...
- [题解] SPOJ GSS1 - Can you answer these queries I
[题解] SPOJ GSS1 - Can you answer these queries I · 题目大意 要求维护一段长度为 \(n\) 的静态序列的区间最大子段和. 有 \(m\) 次询问,每次 ...
- 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 ...
随机推荐
- _bzoj1500 [NOI2005]维修数列【真·Splay】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1500 注意MAX-SUM的时候,不可以是空串. #include <cstdio> ...
- 题解报告:hdu 1503 Advanced Fruits(LCS加强版)
Problem Description The company "21st Century Fruits" has specialized in creating new sort ...
- 官方XmlPullParser和网络解析xml示例及详述
Parsing XML Data This lesson teaches you to Choose a Parser Analyze the Feed Instantiate the Parser ...
- Java实现求二叉树的路径和
题: 解: 这道题考的是如何找出一个二叉树里所有的序列. 我的思路是先从根节点开始遍历,找出所有的子节点,因为每个子节点只有一个父节点,再根据每个子节点向上遍历找出所有的序列,再判断序列的总和. 这样 ...
- AJPFX关于modifier总结
修饰符总结 Modifiers 函数修饰符始终在返回值类型之前!!! 变量修饰符始终在变量类型之前!!!---------------------------------- ...
- VMware Workstation安装CentOS 7和开发环境
VMware Workstation新建虚拟机 此处使用的是VMware Workstation 10,其安装过程即是常规Windos系统下软件安装方式,略过. 安装完成双击图标: 打开虚拟机主界面: ...
- Android 使用EventBus进行Fragment和Activity通信
本文介绍EventBus的基本使用,以及用于Fragment和Activity之间通信. github地址: https://github.com/greenrobot/EventBus 版本是 Ev ...
- Shiro 自定义登陆、授权、拦截器
Shiro 登陆.授权.拦截 按钮权限控制 一.目标 Maven+Spring+shiro 自定义登陆.授权 自定义拦截器 加载数据库资源构建拦截链 使用总结: 1.需要设计的数据库:用户.角色.权限 ...
- BaseAdapter的优化
传统的 package cct.commonadapter.bean; import android.content.Context; import android.view.LayoutInflat ...
- EasyUI edatagrid插件使用小计
html片段 <table id="menuview" style="width:100%"> <thead> <tr> & ...