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. 在storyboard中的静态UITableView中拖入 UISearchBar and Search Display Controller出现的奇怪问题

         近期学习过程中想模拟一下新浪微博"发现"界面.      我在storyboard中拖入一个UITableViewController,设置这个UITableViewCo ...

  2. php的ob_flush和flush(转)

    php.ini中 output_buffering = off 关闭php的缓存 implicit_flush = Off php不会立即输出到浏览器.如果是ON,相当于每次ECHO 立刻执行一个FL ...

  3. android79 Fragment生命周期

    切换成01时依次调用onCreate,onStart,onResume方法,切换到03的时候01依次onPause,onStop,onDestroy,03依次onCreate,onStart,onRe ...

  4. hibernate入门之person表

    下面的hibernate入门person表指的是:根据mysql数据库中的test表和其中的元素-->建立映射表==>进而创建持久化类的顺序来操作了,下面为步骤 1.配置MySQL驱动程序 ...

  5. lua function

    This is the main reference for the World of Warcraft Lua Runtime. Note that these are mostly standar ...

  6. iOS创建界面方法的讨论

    以前在入门的时候,找的入门书籍上编写的 demo 都是基于 Storyboards 拖界面的.后来接触公司项目,发现界面都是用纯代码去写复杂的 autoLayout 的.再然后,领导给我发了个 Mas ...

  7. NDK开发之数组操作

    JNI把Java数组当作引用类型来处理,JNI提供了必要的函数来访问和处理Java数组. 下面一个一个来看. 1.创建数组 我们可以使用NewArray函数在原生代码中创建数组实例,其中可以是Int. ...

  8. CentOS7上Nginx的使用

    Nginx 的启动 指定配置文件的方式启动nginx # nginx -c /etc/nginx/nginx.conf 对于yum安装的nginx,使用systemctl命令启动 # systemct ...

  9. Frequent Pattern 挖掘之一(Aprior算法)(转)

    数据挖掘中有一个很重要的应用,就是Frequent Pattern挖掘,翻译成中文就是频繁模式挖掘.这篇博客就想谈谈频繁模式挖掘相关的一些算法. 定义 何谓频繁模式挖掘呢?所谓频繁模式指的是在样本数据 ...

  10. 如何写robots.txt?

    robin 发表在 八月 2, 2006 在国内,网站管理者似乎对robots.txt并没有引起多大重视,应一些朋友之请求,今天想通过这篇文章来简单谈一下robots.txt的写作. robots.t ...