# Bzoj1251 序列终结者

Posted by yjjr's blog on December 20, 2017

# 题目

Description

1 1 3 2

1 2 4 -1

2 1 3

3 2 4

Sample Output 2

【数据范围】

N<=50000，M<=100000。

# code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define inf 0x7fffffff
using namespace std;
{
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int maxn=5e4+6;
int n,m,sz,root,fa[maxn],c[maxn][2],id[maxn],tag[maxn],v[maxn],mx[maxn],size[maxn];
bool rev[maxn];

void pushup(int x)
{
int l=c[x][0],r=c[x][1];
size[x]=size[l]+size[r]+1;
mx[x]=max(v[x],max(mx[l],mx[r]));
}

void pushdown(int x)
{
int l=c[x][0],r=c[x][1],t=tag[x];
if(t){
tag[x]=0;
if(l){tag[l]+=t;mx[l]+=t;v[l]+=t;}
if(r){tag[r]+=t;mx[r]+=t;v[r]+=t;}
}
if(rev[x]){
rev[x]^=1;rev[l]^=1;rev[r]^=1;
swap(c[x][0],c[x][1]);
}
}

void rotate(int x,int &k)
{
int y=fa[x],z=fa[y],l,r;
if(c[y][0]==x)l=0;else l=1;r=l^1;
if(y==k)k=x;
else{if(c[z][0]==y)c[z][0]=x;else c[z][1]=x;}
fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
c[y][l]=c[x][r];c[x][r]=y;
pushup(y);pushup(x);
}

void splay(int x,int &k)
{
while(x!=k){
int y=fa[x],z=fa[y];
if(y!=k){
if(c[y][0]==x^c[z][0]==y)rotate(x,k);
else rotate(y,k);
}
rotate(x,k);
}
}

int find(int x,int rank)
{
if(rev[x]||tag[x])pushdown(x);
int l=c[x][0],r=c[x][1];
if(size[l]+1==rank)return x;
else if(size[l]>=rank)return find(l,rank);
else return find(r,rank-size[l]-1);
}

void update(int l,int r,int val)
{
int x=find(root,l),y=find(root,r+2);
splay(x,root);splay(y,c[x][1]);
int z=c[y][0];
tag[z]+=val;v[z]+=val;mx[z]+=val;
}

void rever(int l,int r)
{
int x=find(root,l),y=find(root,r+2);
splay(x,root);splay(y,c[x][1]);
int z=c[y][0];rev[z]^=1;
}
int querymx(int l,int r)
{
int x=find(root,l),y=find(root,r+2);
splay(x,root);splay(y,c[x][1]);
return mx[c[y][0]];
}

void build(int l,int r,int f)
{
if(l>r)return;
int now=id[l],last=id[f];
if(l==r){
fa[now]=last,size[now]=1;
if(l<f)c[last][0]=now;else c[last][1]=now;
return;
}
int mid=(l+r)>>1;now=id[mid];
build(l,mid-1,mid);build(mid+1,r,mid);
fa[now]=last;pushup(now);
if(mid<f)c[last][0]=now;
else c[last][1]=now;
}

int main()
{
mx[0]=-inf;root=(n+3)>>1;
rep(i,1,n+2)id[i]=++sz;
build(1,n+2,0);
rep(i,1,m){