4487. Can you answer these queries VI

Problem code: GSS6

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   第二次写splay居然遇上了考splay的常数优化。弄了很久。首先是读入优化,貌似SPOJ上面数据有'\r'所以要特殊判断。然后inline所有函数,最后把splay移到一个struct里面会加速很多。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<string>
#include<queue>
using namespace std;
#ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif
#define MAXN 110000
#define MAXV MAXN*2
#define MAXE MAXV*2
#define MAXT MAXN*2
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define lch splay[now].ch[0]
#define rch splay[now].ch[1]
typedef long long qword;
inline int nextInt()
{
char ch;
int x=;
bool flag=false;
do
ch=(char)getchar(),flag=(ch=='-')?true:flag;
while(ch<''||ch>'');
do x=x*+ch-'';
while (ch=(char)getchar(),ch<='' && ch>='');
return x*(flag?-:);
} int n,m;
int root=,topt=;;
struct sss
{
int lx,rx,mx,val,sum,siz,pnt;
int ch[];
}splay[MAXT];
inline void up(int now)
{
splay[now].lx=splay[now].rx=splay[now].mx=-INF;
if (lch)splay[now].lx=max(splay[now].lx,splay[lch].lx);
splay[now].lx=max(splay[now].lx,max(splay[lch].sum+splay[now].val,splay[lch].sum+splay[now].val+splay[rch].lx)); if (rch)splay[now].rx=max(splay[now].rx,splay[rch].rx);
splay[now].rx=max(splay[now].rx,max(splay[rch].sum+splay[now].val,splay[rch].sum+splay[now].val+splay[lch].rx)); splay[now].mx=splay[now].val;
if (lch)splay[now].mx=max(splay[now].mx,splay[lch].mx);
if (rch)splay[now].mx=max(splay[now].mx,splay[rch].mx);
splay[now].mx=max(splay[now].mx,splay[lch].rx+splay[rch].lx+splay[now].val);
splay[now].mx=max(splay[now].mx,splay[rch].lx+splay[now].val);
splay[now].mx=max(splay[now].mx,splay[lch].rx+splay[now].val); splay[now].siz=splay[lch].siz+splay[rch].siz+;
splay[now].sum=splay[lch].sum+splay[rch].sum+splay[now].val;
} inline void Rotate(int now)
{
int p=splay[now].pnt,anc=splay[p].pnt;
int dir=splay[p].ch[]==now;
if (anc)
splay[anc].ch[splay[anc].ch[]==p]=now;
splay[now].pnt=anc; splay[splay[now].ch[dir]].pnt=p;
splay[p].ch[-dir]=splay[now].ch[dir]; splay[p].pnt=now;
splay[now].ch[dir]=p;
up(p);
up(now);
}
/*
inline int Get_kth(int now,int rk)
{
if (rk==siz[splay[now].ch[0]]+1)
return now;
if (rk<siz[splay[now].ch[0]]+1)
return Get_kth(splay[now].ch[0],rk);
else
return Get_kth(splay[now].ch[1],rk-siz[splay[now].ch[0]]-1);
}*/
inline int Get_kth(int rk)
{
int now=root;
while (rk!=splay[splay[now].ch[]].siz+)
{
if (rk>splay[splay[now].ch[]].siz+)
{
rk-=splay[splay[now].ch[]].siz+;
now=splay[now].ch[];
}else
{
now=splay[now].ch[];
}
}
return now;
} inline void Splay(int now,int tp=)
{
if (now==tp)return ;
while (splay[now].pnt!=tp)
{
int p=splay[now].pnt,anc=splay[p].pnt;
if (anc==tp)
Rotate(now);
else if( (splay[anc].ch[]==p) == (splay[p].ch[]==now))
{
Rotate(p);
Rotate(now);
}else
{
Rotate(now);
Rotate(now);
}
}
if (tp==)root=now;
}
inline void Insert(int pos,int v)
{
int now=++topt;
splay[now].ch[]=splay[now].ch[]=;
splay[now].pnt=;
splay[now].val=v;
splay[now].siz=;
splay[now].lx=splay[now].rx=splay[now].mx=splay[now].sum=v;
if (!pos)
{
Splay(Get_kth());
splay[now].ch[]=root;
splay[root].pnt=now;
root=now;
up(now);
return ;
}
Splay(Get_kth(pos));
splay[now].pnt=root;
splay[now].ch[]=splay[root].ch[];
splay[splay[root].ch[]].pnt=now;
splay[root].ch[]=now;
up(now);
up(root);
return ;
}
inline void Delete(int pos)
{
Splay(Get_kth(pos));
if (pos!=splay[root].siz)
{
Splay(Get_kth(pos+),root);/**/
splay[splay[root].ch[]].ch[]=splay[root].ch[];
splay[splay[root].ch[]].pnt=splay[root].ch[];;
splay[splay[root].ch[]].pnt=splay[root].pnt;
root=splay[root].ch[];
}else
{
root=splay[root].ch[];
splay[root].pnt=;
}
up(root);
}
inline int Qry_mxs(int l,int r)
{
if (l== && r==splay[root].siz)
{
return splay[root].mx;
}else if (l==)
{
Splay(Get_kth(r+));
return splay[splay[root].ch[]].mx;
}else if (r==splay[root].siz)
{
Splay(Get_kth(l-));
return splay[splay[root].ch[]].mx;
}else
{
Splay(Get_kth(l-));
Splay(Get_kth(r+),root);
return splay[splay[splay[root].ch[]].ch[]].mx;
}
}
inline void Chg_val(int pos,int v)
{
Splay(Get_kth(pos));
splay[root].val=v;
up(root);
}
void Scan(int now)
{
if (!now)return ;
if (splay[now].siz!=splay[lch].siz+splay[rch].siz+)throw ;
if (splay[now].ch[])
{
if (splay[splay[now].ch[]].pnt!=now)throw ;
Scan(splay[now].ch[]);
}
printf("%d ",splay[now].val);
if (splay[now].ch[])
{
if (splay[splay[now].ch[]].pnt!=now)throw ;
Scan(splay[now].ch[]);
}
}
void Build_tree(int &now,int *num,int l,int r)
{
now=++topt;
int mid=(l+r)>>;
splay[now].siz=;
splay[now].sum=splay[now].val=splay[now].mx=splay[now].lx=splay[now].rx=num[mid];
if (l<=mid-)Build_tree(lch,num,l,mid-);
if (mid+<=r)Build_tree(rch,num,mid+,r);
splay[lch].pnt=now;
splay[rch].pnt=now;
up(now);
}
int num[MAXN];
int main()
{
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int i,j,k;
int x,y,z;
scanf("%d",&n);
for (i=;i<n;i++)
{
x=nextInt();
num[i]=x;
}
Build_tree(root,num,,n-);
scanf("%d\n",&m);
char opt;
for (i=;i<m;i++)
{
opt=getchar();
//scanf("%c",&opt);
if (opt=='I')
{
// scanf("%d%d",&x,&y);
x=nextInt();y=nextInt();
x--;
Insert(x,y);
}else if (opt=='Q')
{
// scanf("%d%d",&x,&y);
x=nextInt();y=nextInt();
printf("%d\n",Qry_mxs(x,y));
}else if (opt=='R')
{
// scanf("%d%d",&x,&y);
x=nextInt();y=nextInt();
Chg_val(x,y);
}else if (opt=='D')
{
// scanf("%d",&x);
x=nextInt();
Delete(x);
}
getchar();
}
return ;
}
												

spoj 4487. Can you answer these queries VI (gss6) splay 常数优化的更多相关文章

  1. SPOJ 4487. Can you answer these queries VI splay

    题目链接:点击打开链接 题意比較明显,不赘述. 删除时能够把i-1转到根,把i+1转到根下 则i点就在 根右子树 的左子树,且仅仅有i这一个 点 #include<stdio.h> #in ...

  2. 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取替 ...

  3. 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 ...

  4. 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 ...

  5. GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树

    GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...

  6. [题解] SPOJ GSS1 - Can you answer these queries I

    [题解] SPOJ GSS1 - Can you answer these queries I · 题目大意 要求维护一段长度为 \(n\) 的静态序列的区间最大子段和. 有 \(m\) 次询问,每次 ...

  7. 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 ...

  8. bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树

    2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 145 ...

  9. spoj gss2 : Can you answer these queries II 离线&&线段树

    1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...

随机推荐

  1. 【Android XMPP】 学习资料收集贴(持续更新)

    系列一: 基于xmpp openfire smack开发之openfire介绍和部署[1] 基于xmpp openfire smack开发之smack类库介绍和使用[2] 基于xmpp openfir ...

  2. Redis实战之Redis + Jedis[转]

    http://blog.csdn.net/it_man/article/details/9730605 2013-08-03 11:01 1786人阅读 评论(0) 收藏 举报   目录(?)[-] ...

  3. jsp-javabean-setproperty介绍

    李兴华<java web开发实战经典>第7章关于javabean的讲解中说道:<jsp:setProperty>标签一共有4种使用方法·下面列出了4种操作语法的格式:   设置 ...

  4. Android开发之设定Dialog的位置

    今天自定义了一个对话框,但是弹出时默认是显示在屏幕中间.主要代码:menuDialog = new AlertDialog.Builder(this).create();                ...

  5. HTTP基础:URL格式、 HTTP请求、响应、消息

    HTTP URL 格式: http://host[:port][abs_path] 其中http表示要通过HTTP协议来定位网络资源. host表示合法的Internet主机域名或IP地址(以点分十进 ...

  6. CentOS7上GitHub/GitLab多帐号管理SSH Key

    由于公司团队使用 GitLab 来托管代码,同时,个人在 Github 上还有一些代码仓库,可公司邮箱与个人邮箱是不同的,由此产生的 SSH key 也是不同的,这就造成了冲突 ,文章提供此类问题的解 ...

  7. Linux删除文件Argument list too long问题的解决方案

    方法一:使用find find . -name 文件 | xargs rm -f 但文件数量过多,find命令也会出现问题: -bash: /bin/find: Argument list too l ...

  8. (转)网站隔几天打不开,多次重启Apache解决办法

    网站打不开了重启一下apache又好了,但过二天又打不开了,只有重启一下才好,基本上二天重起一次,本文主要解决这个问题 工具/原料 linux服务器 网站 方法/步骤   注意观察cpu占用情况{:s ...

  9. orm fluentdata使用相关文章

    微型orm fluentdata使用:http://www.360doc.com/content/12/1228/23/9200790_256885743.shtml

  10. 将decimal类型的数值后面的0和.号去掉

    今天在群里面看到有朋友在问如下的需求,想到以前在写项目时也遇到这种处理数值的需求,所以写一个例子贴在博客里. 需求:在许多显示货币值时,可能需要截取掉后面的0,显示小数值或者整型值. 举例:(1)数据 ...