Description
网上有许多题,就是给定一个序列,要你支持几种操作:A、B、C、D。一看另一道题,又是一个序列 要支持几种操作:D、C、B、A。尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术含量……这样 我也出一道题,我出这一道的目的是为了让大家以后做这种题目有一个“库”可以依靠,没有什么其他的意思。这道题目 就叫序列终结者吧。 【问题描述】 给定一个长度为N的序列,每个序列的元素是一个整数(废话)。要支持以下三种操作: 1. 将[L,R]这个区间内的所有数加上V。 2. 将[L,R]这个区间翻转,比如1 2 3 4变成4 3 2 1。 3. 求[L,R]这个区间中的最大值。 最开始所有元素都是0。
Input
第一行两个整数N,M。M为操作个数。 以下M行,每行最多四个整数,依次为K,L,R,V。K表示是第几种操作,如果不是第1种操作则K后面只有两个数。
Output
对于每个第3种操作,给出正确的回答。
Sample Input
4 4
1 1 3 2
1 2 4 -1
2 1 3
3 2 4
1 1 3 2
1 2 4 -1
2 1 3
3 2 4
Sample Output
2
【数据范围】
N<=50000,M<=100000。
【数据范围】
N<=50000,M<=100000。
// 竟然颓废了一周..学校复习小四门各种作业堆上来结果完全不想写程序了OTL果然还是要逼自己去写。。只要一旦写起来就想写了
因为这题涉及了区间翻转,所以是个splay。每次对于要操作的区间[s,t],把s-1旋转到root,t+1旋转到root的子树然后t+1这个节点的所有左节点就是要操作的区间了。然后打打tag就好了。
// 这算第一次写splay?才怪!还不是照着人家标程改的!给我好好去学splay啦!
#include<cstdio>
#include<algorithm>
using namespace std;
struct tree
{
int ll,rr;
int l,r;
int fa;
int x,k;
int max;
int tag,turn;
}tr[100001];
int root,tot;
inline void up(int p)
{
int p1=tr[p].l,p2=tr[p].r;
tr[p].max=max(tr[p].l?tr[p1].max:-2100000000,tr[p].r?tr[p2].max:-2100000000);
tr[p].max=max(tr[p].max,tr[p].x);
}
inline void down(int p)
{
int p1=tr[p].l,p2=tr[p].r;
if(tr[p].tag)
{
tr[p1].tag+=tr[p].tag;
tr[p2].tag+=tr[p].tag;
tr[p1].x+=tr[p].tag;
tr[p2].x+=tr[p].tag;
tr[p1].max+=tr[p].tag;
tr[p2].max+=tr[p].tag;
tr[p].tag=0;
}
if(tr[p].turn)
{
tr[p1].turn^=1;
tr[p2].turn^=1;
swap(tr[p1].l,tr[p1].r);
swap(tr[p2].l,tr[p2].r);
swap(tr[p1].ll,tr[p1].rr);
swap(tr[p2].ll,tr[p2].rr);
tr[p].turn=0;
}
}
inline void build(int &p,int l,int r)
{
int mid=(l+r)/2;
tot++;
p=tot;
if(l==r)
return ;
if(mid-l>0)
{
tr[p].ll=mid-l;
build(tr[p].l,l,mid-1);
tr[tr[p].l].fa=p;
}
if(r-mid>0)
{
tr[p].rr=r-mid;
build(tr[p].r,mid+1,r);
tr[tr[p].r].fa=p;
}
up(p);
}
inline int find(int p,int rank)
{
down(p);
if(tr[p].ll+1==rank)
return p;
if(tr[p].ll+1<rank)
return find(tr[p].r,rank-tr[p].ll-1);
else
return find(tr[p].l,rank);
}
inline void zig(int p)
{
int t=tr[p].l;
tr[p].l=tr[t].r;
tr[p].ll=tr[t].rr;
tr[t].r=p;
tr[t].rr+=tr[p].rr+1;
tr[t].fa=tr[p].fa;
tr[p].fa=t;
tr[tr[p].l].fa=p;
if (p==tr[tr[t].fa].l) tr[tr[t].fa].l=t;
else tr[tr[t].fa].r=t;
up(p);
up(t);
}
inline void zag(int p)
{
int t=tr[p].r;
tr[p].r=tr[t].l;
tr[p].rr=tr[t].ll;
tr[t].l=p;
tr[t].ll+=tr[p].ll+1;
tr[t].fa=tr[p].fa;
tr[p].fa=t;
tr[tr[p].r].fa=p;
if (p==tr[tr[t].fa].l) tr[tr[t].fa].l=t;
else tr[tr[t].fa].r=t;
up(p);
up(t);
}
int q[100001];
void splay(int x,int &p)
{
int pt=0;
int i;
for(i=x;i;i=tr[i].fa)
{
pt++;
q[pt]=i;
}
while(pt>0)
{
down(q[pt]);
pt--;
}
int fa=tr[p].fa;
while (tr[x].fa!=fa){
int y=tr[x].fa,z=tr[y].fa;
if (z==fa){
if (x==tr[y].l) zig(y);
else zag(y);
}
else if (y==tr[z].l){
if (x==tr[y].l){zig(z);zig(y);}
else{zag(y);zig(z);}
}
else{
if (x==tr[y].r){zag(z);zag(y);}
else{zig(y);zag(z);}
}
}
p=x;
}
inline void add(int s,int t,int x)
{
int u=find(root,s),v=find(root,t+2);
splay(u,root);
splay(v,tr[root].r);
up(root);
int p=tr[tr[root].r].l;
tr[p].max+=x;
tr[p].tag+=x;
tr[p].x+=x;
}
inline void turn(int s,int t)
{
int u=find(root,s),v=find(root,t+2);
splay(u,root);
splay(v,tr[root].r);
up(root);
int p=tr[tr[root].r].l;
tr[p].turn^=1;
swap(tr[p].l,tr[p].r);
swap(tr[p].ll,tr[p].rr);
}
inline int ask(int s,int t)
{
int u=find(root,s),v=find(root,t+2);
splay(u,root);
splay(v,tr[root].r);
up(root);
int p=tr[tr[root].r].l;
return tr[p].max;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
build(root,0,n+1);
int x,s,t;
int i;
for(i=1;i<=m;i++)
{
scanf("%d",&x);
if(x==1)
{
scanf("%d%d%d",&s,&t,&x);
add(s,t,x);
}
else if(x==2)
{
scanf("%d%d",&s,&t);
turn(s,t);
}
else if(x==3)
{
scanf("%d%d",&s,&t);
printf("%d\n",ask(s,t));
}
}
return 0;
}