思路分析:
ADD , REVERSE ,INSERT ,DELETE ,MIN 这些都是基本操作。
只是REVOLVE 操作不好想,但是你会发现规律。就是把一段取出,放在新序列的后面。
而且有一个循环关系。所以我们可以用取模操作。而且discuss里说的这个操作的T可能为负数。
所以取模的时候就%mod+mod%mod吧。
调试的时候犯了两个超级2的错误。
样例给的m是2.然后我就一直把m输入成2,然后在下面不停的添加操作,结果debug一直不对。每两个就变成初始状态了。。。
然后就是拿出来的时候没有pushup 代码上注释了。。。WTF。。。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define inf 0x3f3f3f3f
#define maxn 222222
#define keyTree (ch[ch[root][1]][0])
using namespace std;
typedef long long LL;
int S[maxn],que[maxn],ch[maxn][2],pre[maxn],siz[maxn];
int root,top1,top2;
int val[maxn],a[maxn];
int rev[maxn],add[maxn],mot[maxn];
void New(int &x,int PRE,int v)
{
x=++top1;
ch[x][0]=ch[x][1]=0;
siz[x]=1;
pre[x]=PRE;
/*special*/
rev[x]=0;
val[x]=v;
mot[x]=v;
add[x]=0;
}
void pushup(int x)/*special*/
{
siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
mot[x]=min(val[x],min(mot[ch[x][0]],mot[ch[x][1]]));
}
void pushdown(int x)/*special*/
{
if(add[x])
{
if(ch[x][0])
{
add[ch[x][0]]+=add[x];
val[ch[x][0]]+=add[x];
mot[ch[x][0]]+=add[x];
}
if(ch[x][1])
{
add[ch[x][1]]+=add[x];
val[ch[x][1]]+=add[x];
mot[ch[x][1]]+=add[x];
}
add[x]=0;
}
if(rev[x])
{
if(ch[x][0])
rev[ch[x][0]]^=1;
if(ch[x][1])
rev[ch[x][1]]^=1;
swap(ch[x][0],ch[x][1]);
rev[x]=0;
}
}
void build(int &x,int s,int e,int f)
{
if(s>e)return;
int mid=(s+e)>>1;
New(x,f,a[mid]);
if(s<mid)build(ch[x][0],s,mid-1,x);
if(e>mid)build(ch[x][1],mid+1,e,x);
pushup(x);
}
void Rotate(int x,int kind)
{
int y=pre[x];
pushdown(x);
pushdown(y);
ch[y][!kind]=ch[x][kind];
pre[ch[x][kind]]=y;
if(pre[y])ch[pre[y]][ch[pre[y]][1]==y]=x;
pre[x]=pre[y];
ch[x][kind]=y;
pre[y]=x;
pushup(y);
}
void Splay(int x,int goal)
{
pushdown(x);
while(pre[x]!=goal)
{
if(pre[pre[x]]==goal)
Rotate(x,ch[pre[x]][0]==x);
else
{
int y=pre[x];
int kind=ch[pre[y]][0]==y;
if(ch[y][kind]==x){
Rotate(x,!kind);
Rotate(x,kind);
}
else {
Rotate(y,kind);
Rotate(x,kind);
}
}
}
pushup(x);
if(goal==0)root=x;
}
void RotateTo(int k,int goal)
{
int r=root;
pushdown(r);
while(siz[ch[r][0]]!=k)
{
if(k<siz[ch[r][0]])
{
r=ch[r][0];
}
else
{
k-=siz[ch[r][0]]+1;
r=ch[r][1];
}
pushdown(r);
}
Splay(r,goal);
}
void init(int n)/*special*/
{
root=top1=top2=0;
ch[0][0]=ch[0][1]=siz[0]=pre[0]=0;
rev[0]=add[0]=0;
mot[0]=val[0]=inf;
New(root,0,inf);
New(ch[root][1],root,inf);
pushup(root);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
build(keyTree,1,n,ch[root][1]);
pushup(ch[root][1]);
pushup(root);
}
void print(int r)
{
if(r)
{
pushdown(r);
print(ch[r][0]);
if(val[r]!=inf)
{
printf("%d ",val[r]);
}
print(ch[r][1]);
}
}
void debug()
{
print(root);
puts("");
}
void solve(int m)
{
char str[10];
int a,b,c;
while(m--)
{
scanf("%s",str);
if(strcmp(str,"ADD")==0)
{
scanf("%d%d%d",&a,&b,&c);
RotateTo(a-1,0);
RotateTo(b+1,root);
add[keyTree]+=c;
mot[keyTree]+=c;
val[keyTree]+=c;
pushup(ch[root][1]);
pushup(root);
}
else if(strcmp(str,"REVERSE")==0)
{
scanf("%d%d",&a,&b);
RotateTo(a-1,0);
RotateTo(b+1,root);
rev[keyTree]^=1;
}
else if(strcmp(str,"REVOLVE")==0)
{
scanf("%d%d%d",&a,&b,&c);
if(!c)continue;
c=-c;
int mod=b-a+1;
c=((c%mod)+mod)%mod;
RotateTo(a-1,0);
RotateTo(a+c,root);
int tmp=keyTree;
pre[tmp]=0;
keyTree=0;
pushup(ch[root][1]);/*居然漏了这两句 WTF*/
pushup(root);
RotateTo(b-c,0);
RotateTo(b-c+1,root);
keyTree=tmp;
pre[tmp]=ch[root][1];
pushup(ch[root][1]);
pushup(root);
}
else if(strcmp(str,"INSERT")==0)
{
scanf("%d%d",&a,&b);
RotateTo(a,0);
RotateTo(a+1,root);
New(keyTree,ch[root][1],b);
pushup(ch[root][1]);
pushup(root);
}
else if(strcmp(str,"DELETE")==0)
{
scanf("%d",&a);
RotateTo(a-1,0);
RotateTo(a+1,root);
pre[keyTree]=0;
keyTree=0;
pushup(ch[root][1]);
pushup(root);
}
else if(strcmp(str,"MIN")==0)
{
scanf("%d%d",&a,&b);
RotateTo(a-1,0);
RotateTo(b+1,root);
printf("%d\n",mot[keyTree]);
}
}
}
int main()
{
int n,m;
while(scanf("%d",&n)!=EOF)
{
init(n);
scanf("%d",&m);
solve(m);
}
return 0;
}
/*
5
1 2 3 4 5
10
ADD 2 4 1
REVERSE 2 4
REVOLVE 2 4 2
INSERT 2 9
DELETE 3
MIN 1 4
*/
更多推荐
POJ 3580 SuperMemo(Splay)
发布评论