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取替。
Q x y: 输出区间[x,y]的最大字段和。
分析:
其实这题是BZOJ 1500 [NOI2005]维修数列这题的简化版。
使用splay来做非常简单。
我的做法增加一个虚拟节点在数列的最开始处,增加两个虚拟节点在最后,这是为了方便在数列最后插入的操作。
splay网上的资料比较多,其实splay比sbt、avl都简单。这里有我的一份总结,并且有一些资料: splay总结
囧,我写的splay太慢了,最后上网搜了一个IO外挂才过了。
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
typedef unsigned long long ull; #define debug puts("here")
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define foreach(i,vec) for(unsigned i=0;i<vec.size();i++)
#define pb push_back
#define RD(n) scanf("%d",&n)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define RD4(x,y,z,w) scanf("%d%d%d%d",&x,&y,&z,&w)
#define All(vec) vec.begin(),vec.end()
#define MP make_pair
#define PII pair<int,int>
#define PQ priority_queue
#define cmax(x,y) x = max(x,y)
#define cmin(x,y) x = min(x,y)
#define Clear(x) memset(x,0,sizeof(x))
/* #pragma comment(linker, "/STACK:1024000000,1024000000") int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p) ); */ /******** program ********************/ const int MAXN = 210005;
const int INF = 1e9; int ch[MAXN][2],fa[MAXN],sz[MAXN],root,tot;
int sum[MAXN],val[MAXN];
int lmax[MAXN],rmax[MAXN],mmax[MAXN];
int a[MAXN]; #define lx ch[x][0]
#define rx ch[x][1]
#define px fa[x] #define ly ch[y][0]
#define ry ch[y][1]
#define py fa[y] #define lz ch[z][0]
#define rz ch[z][1]
#define pz fa[z] #define rt ch[root][1]
#define lrt ch[rt][0]
#define rrt ch[rt][1] // 调试
void dfs(int x){
if(x){
int a = ch[x][0];
int b = ch[x][1];
dfs(a);
printf("x: %3d %3d %3d ",x,a,b);
printf("val: %3d\n",val[x]);
dfs(b);
}
} void gdb(){
puts("\n---------------------");
dfs(root);
puts("---------------------\n");
} inline void update(int x){
if(!x)return;
sz[x] = sz[lx]+sz[rx]+1;
sum[x] = sum[lx]+sum[rx]+val[x];
lmax[x] = max( lmax[lx] , sum[lx]+val[x]+max(0,lmax[rx]) );
rmax[x] = max( rmax[rx] , sum[rx]+val[x]+max(0,rmax[lx]) );
mmax[x] = max( mmax[lx] , mmax[rx] );
mmax[x] = max( mmax[x] , max(0,lmax[rx])+val[x]+max(0,rmax[lx]) );
} inline int sgn(int x){
return ch[px][1]==x;
} inline void setc(int y,int d,int x){
ch[y][d] = x;
px = y;
} inline void rot(int x,int d){
int y = px;
int z = py;
setc(y,!d,ch[x][d]);
if(z) setc(z,sgn(y),x);
fa[x] = z;
setc(x,d,y);
update(y);
} inline void splay(int x,int goal=0){
if(!x)return;
while(px!=goal){
int y = px;
int z = py;
if(z==goal){
rot(x,!sgn(x));
break;
}
if(lz==y){
if(ly==x)
rot(y,1),rot(x,1);
else
rot(x,0),rot(x,1);
}
else{
if(ry==x)
rot(y,0),rot(x,0);
else
rot(x,1),rot(x,0);
}
}
update(x);
if(goal==0)
root = x;
} inline void newNode(int &x,int y,int v){
x = ++ tot;
sz[x] = 1;
ch[x][0] = ch[x][1] = 0;
val[x] = sum[x] = lmax[x] = rmax[x] = mmax[x] = v;
fa[x] = y;
} inline void build(int &x,int y,int l,int r){
if(l>r) return;
int mid = (l+r)>>1;
newNode(x,y,a[mid]);
build(lx,x,l,mid-1);
build(rx,x,mid+1,r);
update(x);
} inline int getKth(int x,int k){
while( sz[lx]+1 != k ){
if(k<=sz[lx])x = lx;
else{
k -= sz[lx]+1;
x = rx;
}
}
return x;
} inline void splay(int x,int y,int ok){
splay( getKth(root,x) );
splay( getKth(root,y) , root );
} inline void Int(int &num){
char in;
bool neg=false;
while(((in=getchar()) > '9' || in<'0') && in!='-') ;
if(in=='-'){
neg=true;
while((in=getchar()) >'9' || in<'0');
}
num=in-'0';
while(in=getchar(),in>='0'&&in<='9')
num*=10,num+=in-'0';
if(neg)
num=0-num;
} char Char() {
char res;
while (res = getchar(), !isalpha(res));
return res;
} char s[MAXN<<2];
int cur; inline int Int(){
int ans = 0;
bool ok = 0 , f = 0;
for(;s[cur];cur++){
if(s[cur]==' '){
if(f)break;
continue;
}f = true;
if(s[cur]=='-')ok = true;
else ans = ans*10+s[cur]-'0';
}
if(ok)ans = -ans;
return ans;
} int main(){ #ifndef ONLINE_JUDGE
freopen("sum.in","r",stdin);
//freopen("sum.out","w",stdout);
#endif int n,m,x,y;
char op; RD(n);
rep1(i,n)
Int(a[i]);
lmax[0] = mmax[0] = rmax[0] = -INF; a[++n] = INF; // 补一个虚拟节点,方便在最后插入 newNode(root,0,-INF);
newNode(ch[root][1],root,INF); update(rt);
update(root); build(lrt,rt,1,n);
update(rt);
update(root); RD(m);
while(m--){
op = Char();
Int(x);
if(op=='I'){
Int(y);
splay(x,x+1,0);
newNode(lrt,rt,y);
update(rt);
update(root);
}else if(op=='D'){
splay(x,x+2,0);
fa[lrt] = 0;
lrt = 0;
}else if(op=='R'){
Int(y);
splay(x,x+2,0);
fa[lrt] = 0;
lrt = 0;
newNode(lrt,rt,y);
}else{
Int(y);
splay(x,y+2,0);
printf("%d\n",mmax[lrt]);
}
} return 0;
}
GSS6 4487. Can you answer these queries VI splay的更多相关文章
- SPOJ 4487. Can you answer these queries VI splay
题目链接:点击打开链接 题意比較明显,不赘述. 删除时能够把i-1转到根,把i+1转到根下 则i点就在 根右子树 的左子树,且仅仅有i这一个 点 #include<stdio.h> #in ...
- 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 ...
- SPOJ GSS6 Can you answer these queries VI ——Splay
[题目分析] 增加了插入和删除. 直接用Splay维护就好辣! 写了一个晚上,(码力不精),最后发现更新写挂了 [代码] #include <cstdio> #include <cs ...
- SPOJ GSS6 Can you answer these queries VI
Can you answer these queries VI Time Limit: 2000ms Memory Limit: 262144KB This problem will be judge ...
- 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 查询一 ...
- kuangbin专题七 HDU4027 Can you answer these queries? (线段树)
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use ...
- 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 ...
- hdu 4027 Can you answer these queries?
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4027 Can you answer these queries? Description Proble ...
- GSS4 2713. Can you answer these queries IV 线段树
GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...
随机推荐
- JS阻塞的问题
常见问题 http://www.zhihu.com/question/23101413 阻塞特性: JS 有个很无语的阻塞特性,就是当浏览器在执行JS 代码时,不能同时做其他任 ...
- NSNotificationCenter需要注意的几个问题
NSNotificationCenter是iOS中常用的消息通知机制,不过在使用过程中有几点需要注意的问题. 直接贴Apple 的官方文档吧: A notification center delive ...
- jquery 选择器中含有空格注意
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- word2013中取消句首字母自动大写
经常使用word的朋友都知道word中一行的首字母会自动大写,这给用户带来方便的同时,也产生了问题,因为有时候我们并不希望每行开头的首字母大写.要取消首字母自动大写可以取消勾选"首句字母大写 ...
- 10635 - Prince and Princess
Problem D Prince and Princess Input: Standard Input Output: Standard Output Time Limit: 3 Seconds In ...
- Hadoop on Mac with IntelliJ IDEA - 10 陆喜恒. Hadoop实战(第2版)6.4.1(Shuffle和排序)Map端 内容整理
下午对着源码看陆喜恒. Hadoop实战(第2版)6.4.1 (Shuffle和排序)Map端,发现与Hadoop 1.2.1的源码有些出入.下面作个简单的记录,方便起见,引用自书本的语句都用斜体表 ...
- hadoop备战:hadoop,hbase兼容版本号汇总
Hbase的安装须要考虑Hadoop的版本号,即兼容性.有不足的希望能指出. 下面考究官网得到的,关于hadoop版本号和hbase版本号可到下面网址中下载:http://mirror.bit.edu ...
- [NOIP 2014复习]第三章:动态规划——NOIP历届真题回想
背包型动态规划 1.Wikioi 1047 邮票面值设计 题目描写叙述 Description 给定一个信封,最多仅仅同意粘贴N张邮票,计算在给定K(N+K≤40)种邮票的情况下(假定全部的邮票数量都 ...
- [Angular 2] Order Dynamic Components Inside an Angular 2 ViewContainer
By default, when you generate components, they will simply be added to the page in order, one after ...
- Js 数组——filter()、map()、some()、every()、forEach()、lastIndexOf()、indexOf()
filter(): 语法: var filteredArray = array.filter(callback[, thisObject]); 参数说明: callback: 要对每个数组元素执行 ...