uva 11922 Permutation Transformer 排列变换 Splay 维护序列 翻转操作
来源:互联网 发布:c语言二分法求方程例题 编辑:程序博客网 时间:2024/03/29 00:41
参考:
大白书
http://blog.csdn.net/zstu_zlj/article/details/10242605
Splay 维护的序列操作,注意lelft不能为null,所以插入一个虚拟节点
此处第k大是指序列左数第k个数
细节见注释:
//#pragma comment(linker, "/STACK:1024000000,1024000000")#include <cstdio>#include <ctime>#include <cstdlib>#include <cstring>#include <queue>#include <string>#include <set>#include <stack>#include <map>#include <cmath>#include <vector>#include <iostream>#include <algorithm>using namespace std;#define FF(i, a, b) for(int i = (a); i < (b); ++i)#define FE(i, a, b) for(int i = (a); i <= (b); ++i)#define FD(i, b, a) for(int i = (b); i >= (a); --i)#define REP(i, N) for(int i = 0; i < (N); ++i)#define CLR(a, v) memset(a, v, sizeof(a))#define PB push_back#define MP make_pairtypedef long long LL;const int INF = 0x3f3f3f3f;const int MAXN = 100010;struct Node{ Node* ch[2]; int v; int s; int flip; Node(){} Node(int v, Node* null) : v(v){ ch[0] = ch[1] = null; s = 1; flip = 0; } ///比较的是位序 int cmp(int x)/// { int ss = ch[0]->s; if (x == ss + 1) return -1; else return x < ss + 1 ? 0 : 1; } void maintain() { s = ch[0]->s + ch[1]->s + 1; } void pushdown() { if (flip) { flip = 0; swap(ch[0], ch[1]); ch[0]->flip ^= 1; ch[1]->flip ^= 1; } }};///Splay维护一个序列struct Splay{ Node *root, *null; void init() { null = new Node; null->ch[0] = null->ch[1] = null; null->v = null->s = null->flip = 0; root = null; } void build(Node* &o, int n) { if (n >= 0)///增设虚拟节点0,split 限制 left != null ! { o = new Node(n, null); build(o->ch[0], n - 1); o->maintain();//maintain } } void rotate(Node* &rt, int d) { Node* k = rt->ch[d ^ 1]; rt->ch[d ^ 1] = k->ch[d]; k->ch[d] = rt; rt->maintain(); k->maintain(); rt = k;//maintain } ///找到rt序列左数第k个元素并伸展到根 void splay(Node* &rt, int k) { rt->pushdown();///pushdown int d = rt->cmp(k); if (d != -1) { if (d == 1) k -= rt->ch[0]->s + 1; Node* p = rt->ch[d]; p->pushdown();///pushdown int d2 = p->cmp(k); if (d2 != -1) { int k2 = k; if (d2 == 1) k2 -= p->ch[0]->s + 1; splay(p->ch[d2], k2);///!!!!!!!!!!! if (d == d2) rotate(rt, d ^ 1); else rotate(rt->ch[d], d); } rotate(rt, d ^ 1); } } ///合并left和right。假设left的所有元素比right小。注意:right可以为null,left不可以 Node* merge(Node* left, Node* right) { splay(left, left->s); left->ch[1] = right; left->maintain();//maintain return left; } ///把rt的前k小的节点放在left里,其它放在right里。1<= k <= rt->s.当k = o->s时,right = null void split(Node* rt, int k, Node* &left, Node* &right) { splay(rt, k); left = rt; right = rt->ch[1]; left->ch[1] = null;/// left->maintain();//maintain } void solve(int a, int b) { Node *left, *right, *mid, *tmp_right; split(root, a, left, tmp_right);///!!! split(tmp_right, b - a + 1, mid, right); mid->flip ^= 1; root = merge(merge(left, right), mid); } void out(Node* &rt) { if (rt == null) return ; rt->pushdown();///pushdown out(rt->ch[0]); if (rt->v > 0) printf("%d\n", rt->v); out(rt->ch[1]);// delete rt;// rt = null; } void debug(Node* &o) { if(o == null) return ; printf("%d(",o->v); if(o->ch[0]!=null) printf("%d,",o->ch[0]->v); else printf("null,"); if(o->ch[1]!=null) printf("%d",o->ch[1]->v); else printf("null"); puts(")"); if(o->ch[0]!=null) debug(o->ch[0]); if(o->ch[1]!=null) debug(o->ch[1]); }}sp;int main (){ int n, m; int x, y; while (~scanf("%d%d", &n, &m)) { sp.init(); sp.build(sp.root, n); for (int i = 1; i <= m; i++) { scanf("%d%d", &x, &y); sp.solve(x, y); } sp.out(sp.root); } return 0;}
- uva 11922 Permutation Transformer 排列变换 Splay 维护序列 翻转操作
- UVa 11922 Permutation Transformer splay 把序列翻转后放到结尾
- UVA 11922 Permutation Transformer Splay 区间翻转 + 区间合并
- UVA 11922 Permutation Transformer (Splay 区间翻转 + 复制粘贴)
- UVA 11922 Permutation Transformer Splay
- UVA 11922 Permutation Transformer(splay)
- UVa 11922 Permutation Transformer(splay)
- 11922 - Permutation Transformer (Splay区间翻转)
- 【splay tree】 UVA 11922 Permutation Transformer
- UVa 11922 - Permutation Transformer (Splay)
- UVA 11922 Permutation Transformer(Splay Tree)
- UVA 11922 Permutation Transformer(splay树)
- Permutation Transformer+uva+splay树
- UVA 11922 Permutation Transformer(Splay【伸展树】的一些操作)
- UVA 11922 Permutation Transformer(伸展树 Splay Tree)
- uva 11922 Permutation Transformer(Splay tree,懒标记传递)
- uva 11922 - Permutation Transformer
- uva 11922 Permutation Transformer
- 教你几种方法最大限度减轻酒精对你身体的伤害
- linux命令大全 带案例
- dos不能显示中文
- rsync同步服务器配置指南
- C语言学习(四)
- uva 11922 Permutation Transformer 排列变换 Splay 维护序列 翻转操作
- Java通过JNI调用C语言的方法
- C# 装箱和拆箱[整理]
- Uva 11019 Matrix Matcher
- CentOS/使用rsync同步
- Leetcode Sum Root to Leaf Numbers
- 最佳开源应用程序:开发工具
- 云计算(四)- Hadoop2.2.0命令大全
- Spring Security学习总结二