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. nginx lua 开发笔记

    获取 在lua代码中获取 location 正则的参数对应的变量 // location location ~/lua_http_2/(\w*.+) { } // lua local vars=ngx ...

  2. [Angular 2] Component relative paths

    Oingial aritial --> Link Take away: import { Component, OnInit } from '@angular/core'; @Component ...

  3. ccrendertexture

    int bgHeight=150; CCSprite *sp=CCSprite::create("HelloWorld.png"); sp->setAnchorPoint(c ...

  4. C#泛型的性能优势

    我写东西一向追求短小精悍,就不放代码去验证的,只说结论,并会与Java泛型做对比.有不对之处还望指出. 泛型作为一个在C#2.0中就引入的特性,也是C#的重要特性之一,我经常看到有人讨论泛型带来的便捷 ...

  5. android开发之重写Application类

    在android应用开发中,重写Application也算是比较常见的,以前开发的一些程序太过于简单,都不要重写这个类,但是在真正的商业开发中,重写Application类几乎是必做的. 为什么要重写 ...

  6. yii2 验证码的使用

    @see  http://www.yiiframework.com/doc-2.0/yii-captcha-captcha.html 以下根据 MVC 模型的顺序来添加代码 1. model 层, 或 ...

  7. Bootstrap-全局css样式之按钮

    这里所说的按钮只是Bootstrap设计的能使标签或元素呈现按钮样式的属性,所以为 <a>.<button> 或 <input> 元素添加按钮类(button cl ...

  8. ManualResetEvent的使用与介绍

    它可以通知一个或多个正在等待的线程已发生事件,允许线程通过发信号互相通信,来控制线程是否可心访问资源 当一个线程开始一个活动(此活动必须完成后,其他线程才能开始)时,它调用 Reset 以将 Manu ...

  9. 模版引擎(NVelocity)开发

    在net中用模版开发,在handler中用到了大量的html代码.为解决这个问题,我可以采用模版引擎(NVelocity)进行开发.1.首先需要将NVelocity.dll文件放入项目,其次引用.2. ...

  10. 研究WCF并发及处理能力的控制

    WCF 跟并发 性能相关的几个配置:1.系统控制的客户端网络连接并发(如果服务端也需要并发请求的话这个参数也是需要的):          <configuration>          ...