3223: Tyvj 1729 文艺平衡树
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 3313 Solved: 1883
[Submit][Status][Discuss]
Description
您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1
Input
第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n) m表示翻转操作次数
接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=n
Output
输出一行n个数字,表示原始序列经过m次变换后的结果
Sample Input
5 3
1 3
1 3
1 4
Sample Output
4 3 2 1 5
HINT
N,M<=100000
Source
RunID
User | Problem | Result | Memory | Time | Language | Code_Length | Submit_Time | |
1538282 | ksq2013 | 3223 | Accepted | 5396 kb | 2576 ms | C++/Edit | 2878 B | 2016-07-08 20:57:17 |
区间翻转裸题,废话不多说,直接放代码:
#include<cstdio>
#include<iostream>
#define INF 0x3f3f3f3f
#define Key_value ch[ch[root][1]][0]
using namespace std;
bool rev[];
int n,m,root,tot,size[],key[],pre[],ch[][];
void NewNode(int &x,int father,int val)
{
x=++tot;
size[x]=;
key[x]=val;
pre[x]=father;
}
void Update_Rev(int x)
{
rev[x]^=;
swap(ch[x][],ch[x][]);
}
void Push_Up(int x)
{
size[x]=size[ch[x][]]+size[ch[x][]]+;
}
void Push_Down(int x)
{
if(rev[x]){
Update_Rev(ch[x][]);
Update_Rev(ch[x][]);
rev[x]=;
}
}
void build(int &x,int father,int l,int r)
{
if(l>r)return;
int mid=(l+r)>>;
NewNode(x,father,mid);
build(ch[x][],x,l,mid-);
build(ch[x][],x,mid+,r);
Push_Up(x);
}
void Init()
{
NewNode(root,,INF);
NewNode(ch[root][],root,INF);
build(Key_value,ch[root][],,n);
}
int Get_Kth(int x,int k)
{
Push_Down(x);//mistaken codes;
int t=size[ch[x][]]+;
if(t==k)return x;
if(t>k)return Get_Kth(ch[x][],k);
return Get_Kth(ch[x][],k-t);
}
void Rotate(int x,int kind)
{
int y=pre[x];
Push_Down(y);Push_Down(x);
ch[y][!kind]=ch[x][kind];
pre[ch[x][kind]]=y;
if(pre[y])ch[pre[y]][ch[pre[y]][]==y]=x;//unfixed;
pre[x]=pre[y];
ch[x][kind]=y;
pre[y]=x;
Push_Up(y);
}
void Splay(int x,int goal)
{
Push_Down(x);
while(pre[x]!=goal){
if(pre[pre[x]]==goal){
Push_Down(pre[x]);
Push_Down(x);
Rotate(x,ch[pre[x]][]==x);
}
else{
int y=pre[x];
int kind=ch[pre[y]][]==y;
Push_Down(pre[y]);//mistaken codes&&一遇到向下的操作就Push_Down;
Push_Down(y);//mistaken codes
Push_Down(x);//mistaken codes
if(ch[y][kind]==x){
Rotate(x,!kind);
Rotate(x,kind);
}
else{
Rotate(y,kind);
Rotate(x,kind);
}
}
}
Push_Up(x);
if(goal==)root=x;
}
void Rev(int l,int r)
{
Splay(Get_Kth(root,l),);
Splay(Get_Kth(root,r+),root);
Update_Rev(Key_value);
Push_Up(ch[root][]);
Push_Up(root);
}
void dfs(int x)
{
if(!x)return;
Push_Down(x);
dfs(ch[x][]);
if(<=key[x]&&key[x]<=n)printf("%d ",key[x]);
dfs(ch[x][]);
}
int main()
{
scanf("%d%d",&n,&m);
Init();
for(int x,y;m;m--){
scanf("%d%d",&x,&y);
Rev(x,y);
}dfs(root);
return ;
}
新splay模板上线,更简洁
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
inline int Rin(){
int x=,c=getchar(),f=;
for(;c<||c>;c=getchar())
if(!(c^))f=-;
for(;c>&&c<;c=getchar())
x=(x<<)+(x<<)+c-;
return x*f;
}
struct node{
node*ch[];
int r,s,v;bool b;
node (int v,node*k):v(v){
r=rand();
b=;s=;
ch[]=ch[]=k;
}
void exc(node*&x,node*&y)
{node*k=y;y=x;x=k;}
void pu(){s=ch[]->s+ch[]->s+;}
void pd(){
if(b){
b=;
exc(ch[],ch[]);
ch[]->b^=;
ch[]->b^=;
}
}
}*rt,*bf;
inline void rot(node*&o,int d){
node*k=o->ch[d^];o->ch[d^]=k->ch[d];k->ch[d]=o;
o->pu();k->pu();o=k;
}
inline int cmk(node*o,int k){
if(o->ch[]->s+==k)return -;
if(o->ch[]->s>=k)return ;
return ;
}
inline void ins(node*&o,int x){
if(o==bf){o=new node(x,bf);return;}
ins(o->ch[],x);
o->ch[]->r>o->r?rot(o,):o->pu();
}
inline void splay(node*&o,int k){
if(o==bf)return;
o->pd();int d=cmk(o,k);
if(!(d^))k-=o->ch[]->s+;
if(d!=-&&o->ch[d]!=bf){
node*p=o->ch[d];p->pd();
int dd=cmk(p,k);
if(dd!=-&&p->ch[dd]!=bf){
int kk=(dd?k-p->ch[]->s-:k);
splay(p->ch[dd],kk);
d^dd?rot(o->ch[d],d):rot(o,d^);
}
rot(o,d^);
}
}
inline node*mrg(node*l,node*r){
splay(l,l->s);l->ch[]=r;
l->pu();return l;
}
inline void slt(node*o,int k,node*&l,node*&r){
if(!k){l=bf;r=o;return;}
if(!(k^o->s)){l=o;r=bf;return;}
splay(o,k);l=o;r=o->ch[];
o->ch[]=bf;l->pu();
}
inline void ini(){
bf=new node(,);
bf->r=INT_MAX;bf->s=bf->v=;
bf->ch[]=bf->ch[]=bf;rt=bf;
ins(rt,);rt->r=-;
}
void opt(node*o){
if(o==bf)return;
o->pd();
opt(o->ch[]);
if(o->r!=-)printf("%d ",o->v);
opt(o->ch[]);
}
int n,m;
int main(){
n=Rin(),m=Rin();ini();
for(int i=;i<=n;i++)ins(rt,i);
while(m--){
int l=Rin(),r=Rin();
node *ll,*rr,*mm;
slt(rt,l,ll,rr);
slt(rr,r-l+,mm,rr);
mm->b^=;
rt=mrg(mrg(ll,mm),rr);
}
opt(rt);
return ;
}