首页 > 代码库 > 【BZOJ】3223: Tyvj 1729 文艺平衡树(splay)
【BZOJ】3223: Tyvj 1729 文艺平衡树(splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=3223
默默的。。
#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>#include <queue>#include <set>#include <map>using namespace std;typedef long long ll;#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << (#x) << " = " << (x) << endl#define error(x) (!(x)?puts("error"):0)#define rdm(x, i) for(int i=ihead[x]; i; i=e[i].next)inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }struct node *null;struct node { node *f, *c[2]; int s, k; bool rev; node(int _k=0) { f=c[0]=c[1]=null; k=_k; s=1; rev=0; } bool d() { return f->c[1]==this; } void setc(node *x, bool d) { c[d]=x; x->f=this; } void pushup() { s=1+c[0]->s+c[1]->s; } void upd() { if(this==null) return; rev=!rev; swap(c[0], c[1]); } void pushdown() { if(rev) rev=0, c[0]->upd(), c[1]->upd(); }}*root;void rot(node *x) { node *f=x->f; f->pushdown(); x->pushdown(); bool d=x->d(); f->f->setc(x, f->d()); f->setc(x->c[!d], d); x->setc(f, !d); f->pushup(); if(f==root) root=x;}void splay(node *x, node *f=null) { x->pushdown(); while(x->f!=f) if(x->f->f==f) rot(x); else x->d()==x->f->d()?(rot(x->f), rot(x)):(rot(x), rot(x)); x->pushup();}node *sel(node *x, int k) { if(x==null) return null; x->pushdown(); int s=x->c[0]->s; if(s==k) return x; if(s<k) return sel(x->c[1], k-s-1); return sel(x->c[0], k);}node *getrange(int l, int r) { splay(sel(root, l-1)); splay(sel(root, r+1), root); return root->c[1]->c[0];}void build(int l, int r, node *&x) { if(l>r) return; int mid=(l+r)>>1; x=new node(mid); if(l==r) return; build(l, mid-1, x->c[0]); build(mid+1, r, x->c[1]); if(l<=mid-1) x->c[0]->f=x; if(mid+1<=r) x->c[1]->f=x; x->pushup();}int n, m;void init() { null=new node; null->s=0; null->f=null->c[0]=null->c[1]=null; root=new node; root->setc(new node, 1); node *x; build(1, n, x); root->c[1]->setc(x, 0); root->c[1]->pushup(); root->pushup();}void rev() { int l=getint(), r=getint(); node *x=getrange(l, r); x->upd();}void Pr(node *x) { if(x==null) return; x->pushdown(); Pr(x->c[0]); printf("%d ", x->k); Pr(x->c[1]); }void P() { node *x=getrange(1, n); Pr(x);}int main() { read(n); int m=getint(); init(); while(m--) rev(); P(); return 0;}
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
1 3
1 3
1 4
Sample Output
4 3 2 1 5
HINT
N,M<=100000
Source
平衡树
【BZOJ】3223: Tyvj 1729 文艺平衡树(splay)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。