NOI2007项链工厂——sbTreap代码

时间:2023-03-09 18:57:31
NOI2007项链工厂——sbTreap代码
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib> using namespace std;
struct node
{
int data;
int left;
int right;
int key;
int size;
bool turn;
int ans;
int mark;
node* ls,*rs;
node(int dat=,int ke=)
{
data=dat;
key=ke;
left=data;
right=data;
size=;
turn=false;
mark=;
ans=;
ls=NULL;
rs=NULL;
}
}no[]; void pushdown(node* now)
{
if (now->mark)
{
if (now->ls)
{
now->ls->data=now->mark;
now->ls->mark=now->mark;
now->ls->left=now->mark;
now->ls->right=now->mark;
now->ls->ans=;
}
if (now->rs)
{
now->rs->data=now->mark;
now->rs->mark=now->mark;
now->rs->left=now->mark;
now->rs->right=now->mark;
now->rs->ans=;
}
now->mark=;
}
if (now->turn)
{
node* l=now->ls;
node* r=now->rs;
if (now->ls)
now->ls->left^=now->ls->right^=now->ls->left^=now->ls->right;
if (now->rs)
now->rs->left^=now->rs->right^=now->rs->left^=now->rs->right;
now->ls=r;
now->rs=l;
if (now->ls) now->ls->turn=!now->ls->turn;
if (now->rs) now->rs->turn=!now->rs->turn;
now->turn=false;
}
} void update(node* now)
{
now->size=;
now->ans=;
now->left=now->data;
now->right=now->data;
if (now->ls)
{
now->size+=now->ls->size;
now->ans+=now->ls->ans;
now->left=now->ls->left;
}
if (now->rs)
{
now->size+=now->rs->size;
now->ans+=now->rs->ans;
now->right=now->rs->right;
}
if (now->ls)
if (now->ls->right==now->data) now->ans--;
if (now->rs)
if (now->rs->left==now->data) now->ans--;
} node* merge(node* a,node* b)
{
if (!b) return a;
if (!a) return b;
pushdown(a);
pushdown(b);
if (a->key<=b->key)
{
a->rs=merge(a->rs,b);
update(a);
return a;
}
else
{
b->ls=merge(a,b->ls);
update(b);
return b;
}
} struct npair
{
node* l,*r;
npair(node* a,node* b)
{
l=a;
r=b;
}
}; npair split(node* a,int k)
{
if (!a) return npair(NULL,NULL);
if (k==) return npair(NULL,a);
pushdown(a);
if (a->ls)
{
if (a->ls->size>=k)
{
npair km=split(a->ls,k);
a->ls=km.r;
update(a);
return npair(km.l,a);
}
else
{
npair km=split(a->rs,k-a->ls->size-);
a->rs=km.l;
update(a);
return npair(a,km.r);
}
}
else
{
npair km=split(a->rs,k-);
a->rs=km.l;
update(a);
return npair(a,km.r);
}
}
node* insert(node* root,node* newnode)
{
return merge(root,newnode);
} void turn(node* now)
{
now->turn=!now->turn;
now->left^=now->right^=now->left^=now->right;
} node* rotate(node* root,int num)
{
int n=root->size;
num=num%n;
int k=n-num;
npair km = split(root,k);
return merge(km.r,km.l);
} node* flip(node* root)
{
int n=root->size;
int r;
if (n%==)
{
r=n/+;
}
else
{
r=n/;
}
npair km=split(root,r);
npair km2=split(km.l,);
if (n%==)
{
turn(km2.r);
turn(km.r);
return merge(merge(km2.l,km.r),km2.r);
}
else
{
npair km3=split(km.r,);
turn(km2.r);
turn(km3.r);
return merge(merge(km2.l,km3.r),merge(km3.l,km2.r));
}
} node* swap(node* root,int i,int j)
{
if (i>j) i^=j^=i^=j;
if (i==j) return root;
npair km=split(root,i);
npair km2=split(km.l,i-);
npair km3=split(km.r,j-i);
npair km4=split(km3.l,j-i-);
return merge(merge(merge(km2.l,km4.r),km4.l),merge(km2.r,km3.r));
} node* paint(node* root,int i,int j,int x)
{
int n=root->size;
if (i<=j)
{
npair km=split(root,i-);
npair km2=split(km.r,j-i+);
km2.l->mark=x;
km2.l->data=x;
km2.l->ans=;
km2.l->left=x;
km2.l->right=x;
return merge(km.l,merge(km2.l,km2.r));
}
else
{
npair km=split(root,j);
int nn=km.r->size;
npair km2=split(km.r,nn-n+i-);
km.l->mark=x;
km.l->data=x;
km.l->ans=;
km.l->left=x;
km.l->right=x;
km2.r->mark=x;
km2.r->data=x;
km2.r->ans=;
km2.r->left=x;
km2.r->right=x;
return merge(km.l,merge(km2.l,km2.r));
}
} node* root;
int countS(int i,int j)
{
int n=root->size;
if (i<=j)
{
npair km=split(root,i-);
npair km2=split(km.r,j-i+);
int ret=km2.l->ans;
root=merge(km.l,merge(km2.l,km2.r));
return ret;
}
else
{
npair km=split(root,j);
int nn=km.r->size;
npair km2=split(km.r,nn-n+i-);
int ret=km.l->ans+km2.r->ans;
if (km.l->left==km2.r->right) ret--;
root=merge(km.l,merge(km2.l,km2.r));
return ret;
}
} int count()
{
int ret=root->ans;
if (root->left==root->right&&ret!=) ret--;
return ret;
} void print(node* now,bool b)
{
if (!now) return;
b=b^now->turn;
// if (!b)
print(now->ls,b);
// else
// print(now->rs,b);
printf("data: %d size: %d mark: %d turn: %d ans: %d left: %d right: %d\n",now->data,now->size,now->mark,now->turn,now->ans,now->left,now->right);
// if (!b)
print(now->rs,b);
// else
// print(now->ls,b);
} void print(node* now)
{
if (!now) return;
pushdown(now);
print(now->ls);
printf("%d\n",now->data);
print(now->rs);
} int cnt=-;
int main()
{
int n,c;
scanf("%d%d",&n,&c);
int j;
for (int i=;i<=n;++i)
{
scanf("%d",&j);
node* q=&no[++cnt];
*q=node(j,rand());
root=insert(root,q);
}
int m;
scanf("%d",&m);
char cmd[];
int l,r,k;
for (int i=;i<=m;++i)
{
scanf("%s",cmd);
if (cmd[]=='R'){scanf("%d",&k);root=rotate(root,k);}
if (cmd[]=='F'){root=flip(root);}
if (cmd[]=='S'){scanf("%d%d",&l,&r);root=swap(root,l,r);}
if (cmd[]=='P'){scanf("%d%d%d",&l,&r,&k);root=paint(root,l,r,k);}
if (cmd[]=='C'&&cmd[]!='S'){printf("%d\n",count());}
if (cmd[]=='C'&&cmd[]=='S'){scanf("%d%d",&l,&r);printf("%d\n",countS(l,r));}
// printf("%d\n",i);
// printf("--------------------\n");
// print(root,false);
// printf("--------------------\n");
}
return ;
}