UVA 12538 可持久化treap入门
2014-01-19 22:35
316 查看
学了能可持久化的treap
看了一下clj的《可持久化数据结构研究》论文。
做了一下UVA
12538,算是会了一点点了。
可持久化treap不用进行rotate操作,它有两个基本的操作
merge(a,b):把 treap<a>与treap<b>合并成treap<c>
split(c,k):把treap<c>的前k个变成一个treap<a>,剩下的元素变成treap<b>
这两个操作要保证a的每个元素都要小于b的每个元素
可以根据需要来选择是否实现可持久化, 如果不需要,那么就正常修改
如果需要可持久化,那么对于版本v变到版本v+1,操作所走过的路径要重新新建节点,
可以写一个copy()函数来方便新建。
像这种字符串用treap可持久化维护多个版本有专门的STL,
速度跟手写差不多,代码在最下面
code(手写):
code(STL):
看了一下clj的《可持久化数据结构研究》论文。
做了一下UVA
12538,算是会了一点点了。
可持久化treap不用进行rotate操作,它有两个基本的操作
merge(a,b):把 treap<a>与treap<b>合并成treap<c>
split(c,k):把treap<c>的前k个变成一个treap<a>,剩下的元素变成treap<b>
这两个操作要保证a的每个元素都要小于b的每个元素
可以根据需要来选择是否实现可持久化, 如果不需要,那么就正常修改
如果需要可持久化,那么对于版本v变到版本v+1,操作所走过的路径要重新新建节点,
可以写一个copy()函数来方便新建。
像这种字符串用treap可持久化维护多个版本有专门的STL,
速度跟手写差不多,代码在最下面
code(手写):
#include <cstdio> #include <cstring> #include <algorithm> #include <ctime> using namespace std; const int maxn = 50005; struct node; node *null, *root[maxn]; struct node { node* c[2]; char v; int r, sz; void up() { sz = c[0]->sz + c[1]->sz + 1; } node(char v=0): v(v) { sz = 1, r = rand(); c[0] = c[1] = null; } }; inline void copy(node* &a, node* b) { //可持久化时,修改操作经过的路径要新建节点 if(b==null) a = b; else a = new node(), *a = *b; } void merge(node* &o, node* a, node* b) { if(a == null) copy(o, b); else if(b == null) copy(o, a); else if(a->r < b->r) { copy(o, a); merge(o->c[1], a->c[1], b); o->up(); } else { copy(o, b); merge(o->c[0], a, b->c[0]); o->up(); } } void split(node* o, node* &a, node* &b, int k) { if(!k) {//最后一次往右儿子走 copy(b, o); a = null; } if(o->sz <= k) {//优化,减少加入的新节点(可以去掉) copy(a, o); b = null; } else if(o->c[0]->sz >= k) { //左移,属于treap<b> copy(b, o); split(o->c[0], a, b->c[0], k); b->up(); } else { //右移,属于treap<a> copy(a, o); split(o->c[1], a->c[1], b, k-o->c[0]->sz-1); a->up(); } } char s[203]; void build(node* &o, int l, int r) { if(l > r) return; int m = (l + r) >> 1; o = new node(s[m]); build(o->c[0], l, m-1); build(o->c[1], m+1, r); o->up(); } void ins(node* &o, node* pre, int pos) { node *a, *b, *c; int len = strlen(s); split(pre, a, b, pos); build(c, 0, len-1); merge(a, a, c); merge(o, a, b); } void del(node* &o, node* pre, int pos, int len) { node *a, *b, *c; split(pre, a, b, pos-1); split(b, b, c, len); merge(o, a, c); } int dlt; //题目强制在线的改变量 void out(node *o) { if(o == null) return; out(o->c[0]); if(o->v == 'c') dlt++; printf("%c", o->v); out(o->c[1]); } void out(node *o, int pos, int len) { node *a, *b, *c; split(o, a, b, pos-1); split(b, b, c, len); out(b); puts(""); } void init() { null = new node(); null->sz = 0; for(int i = 0; i < maxn; i++) root[i] = null; } int n; int main() { scanf("%d", &n); init(); int op, pos, len, v, nowv = 0; while(n--) { scanf("%d", &op); if(op == 1) { scanf("%d%s", &pos, s); pos -= dlt; ins(root[nowv+1], root[nowv], pos); nowv++; } else if(op == 2) { scanf("%d%d", &pos, &len); pos -= dlt, len -= dlt; del(root[nowv+1], root[nowv], pos, len); nowv++; } else { scanf("%d%d%d", &v, &pos, &len); v -= dlt, pos -= dlt, len -= dlt; out(root[v], pos, len); } } return 0; }
code(STL):
#include <iostream> #include <ext/rope> using namespace std; using namespace __gnu_cxx; crope t, trp[50005], tmp; char s[205]; int dlt, nowv, n; int main() { int pos, op, len, v; scanf("%d", &n); while (n--) { scanf("%d", &op); if(op == 1) { scanf("%d%s", &pos, s); pos -= dlt; t.insert(pos, s); trp[++nowv] = t; } else if(op == 2) { scanf("%d%d", &pos, &len); pos -= dlt, len -= dlt; t.erase(pos - 1, len); trp[++nowv] = t; } else { scanf("%d%d%d", &v, &pos, &len); v -= dlt, pos -= dlt, len -= dlt; tmp = trp[v].substr(pos - 1, len); dlt += count(tmp.begin(), tmp.end(), 'c'); cout << tmp << endl; } } }
相关文章推荐
- UVA 12538 Version Controlled IDE 可持久化平衡树 || rope
- UVA 12538 Version Controlled IDE(可持久化treap)
- UVA 12538 Version Controlled IDE 可持久化treap
- uva12538 Version Controlled IDE(可持续化Treap * 模板, STL ext/rope(块状链表))
- 算法竞赛入门经典第六章例题6-3 Matrix Chain Multiplication UVA - 442
- 【treap tree】 HDOJ 3726 && LA 5031 && UVA 1479 Graph and Queries
- Treap入门题
- treap入门
- [刷题]算法竞赛入门经典 3-1/UVa1585 3-2/UVa1586 3-3/UVa1225
- 算法之路二:刘汝佳算法竞赛入门经典 信息解码 UVA213
- Uva 5031 Graph and Queries(Treap)
- [刷题]算法竞赛入门经典 3-7/UVa1368 3-8/UVa202 3-9/UVa10340
- uva 10006 数论入门题
- UVA - 1220 Party at Hali-Bula 树形DP入门
- 算法之路二:刘汝佳算法竞赛入门经典:栈和队列 矩阵链乘UVA442
- 算法竞赛入门经典第四章习题4-4 Cube painting UVA - 253
- UVA 12538 Version Controlled IDE
- POJ 1442 (treap 求出名次入门)
- UVa1479 - Graph and Queries图询问(Treap名次树)
- 算法竞赛入门竞赛 习题3-2 分子量(Molar Mass, ACM/ICPC Seoul 2007, UVa1586)