可持久化线段树——主席树
2017-08-14 18:32
295 查看
支持访问任意历史版本以及在历史版本上修改的数据结构 每次修改不是在原有线段树上直接修改,而是在旁边新建点。
时空复杂度都是O(mlogn)
![](https://img-blog.csdn.net/20170814175626123?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamlubWluZ3lpMTk5OA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
Background
To The Moon is a independent game released in November 2011, it is a role-playing adventure game powered by RPG Maker.
The premise of To The Moon is based around a technology that allows us to permanently reconstruct the memory on dying man. In this problem, we’ll give you a chance, to implement the logic behind the scene.
You‘ve been given N integers A[1], A[2],…, A
. On these integers, you need to implement the following operations:
1. C l r d: Adding a constant d for every {Ai | l <= i <= r}, and increase the time stamp by 1, this is the only operation that will cause the time stamp increase.
2. Q l r: Querying the current sum of {Ai | l <= i <= r}.
3. H l r t: Querying a history sum of {Ai | l <= i <= r} in time t.
4. B t: Back to time t. And once you decide return to a past, you can never be access to a forward edition anymore.
.. N, M ≤ 105, |A[i]| ≤ 109, 1 ≤ l ≤ r ≤ N, |d| ≤ 104 .. the system start from time 0, and the first modification is in time 1, t ≥ 0, and won’t introduce you to a future state.
Input
n m
A1 A2 … An
… (here following the m operations. )
Output
… (for each query, simply print the result. )
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
2 4
0 0
C 1 1 1
C 2 2 -1
Q 1 2
H 1 2 1
Sample Output
4
55
9
15
0
1
Author
HIT
Source
2012 Multi-University Training Contest 5
时空复杂度都是O(mlogn)
const int N = 100000 + 5;//点数 long long tree[N * 25], add_num[N * 25]; int total, left_child[N * 25], right_child[N * 25], root[N * 25]; /**************************************************************************** 逻辑上仍然是完全二叉树,动态加点,所以左右儿子不能再通过*2或*2+1得到 tree[]为存放树的,left_child[t]\right_child[t]存放t的左右儿子在tree中的下标 total用于加点时分配下标,root用于更新时创建新的根,不同的root为不同的时间 add_num[]存放每个节点区间更新的标记 *****************************************************************************/ void build(int t, int l, int r)//t为在tree中的下标,l为当前递归状态表示的l,r为当前r { if (l == r) { //tree[t]=a[l]; //cin >> tree[t]; //向叶子节点添加值 return; } int mid = l + r >> 1; build(left_child[t] = ++total, l, mid);//向左递归时加一个点 build(right_child[t] = ++total, mid + 1, r);//向右递归同时加一个点为右儿子 tree[t] = tree[left_child[t]] + tree[right_child[t]]; //tree[t] = max(tree[left_child[t]] , tree[right_child[t]]); //回溯的时候更新父亲,以求区间和为例 } /**************************参数**************************** l,r同上,因为是新建的一条链,所以last和cur与上面的t意义一样,只是 在这里分成两个,一个新的一个旧的。x,y为查询区间,k为区间要加的东西 ***********************************************************/ void interval_update(int last, int cur, int l, int r, int x, int y, int k) { tree[cur] = tree[last] + (y-x + 1)*k;//更新当前区间 right_child[cur] = right_child[last];//初始化这个新建的点,新点指向原先的左右儿子 left_child[cur] = left_child[last]; add_num[cur] = add_num[last];//原先这个点的标记拿到新建的这个点 if (x <= l&&r <= y) { add_num[cur] += k;//更新,当前区间打上标记 return; } int mid = l + r >> 1; if (y <= mid) { interval_update(left_child[last], left_child[cur] = ++total, l, mid, x, y, k); } else if (x>mid) { interval_update(right_child[last], right_child[cur] = ++total, mid + 1, r, x, y, k); } else { //往两边递归的时候查询的区间也要跟着分开 interval_update(left_child[last], left_child[cur] = ++total, l, mid, x, mid, k); interval_update(right_child[last], right_child[cur] = ++total, mid + 1, r, mid+1, y, k); } } long long interval_query(int t, int l, int r, int x, int y, int sum)//t,l,r,x,y同上,sum为一路传下来的标记 { if (x <= l&&r <= y) { //返回当前点的值和所有传下来的标记 return tree[t] + (y - x + 1)*sum; } int mid = l + r >> 1; if (y <= mid)//向左递归 { return interval_query(left_child[t], l, mid, x, y, sum + add_num[t]); } else if (x>mid)//向右递归 { return interval_query(right_child[t], mid + 1, r, x, y, sum + add_num[t]); } else//区间分两半 { //往两边递归的时候查询的区间也要跟着分开 return interval_query(left_child[t], l, mid, x, mid, sum + add_num[t]) + interval_query(right_child[t], mid + 1, r, mid+1, y, sum + add_num[t]); } }
例题
HDU 4348
Problem DescriptionBackground
To The Moon is a independent game released in November 2011, it is a role-playing adventure game powered by RPG Maker.
The premise of To The Moon is based around a technology that allows us to permanently reconstruct the memory on dying man. In this problem, we’ll give you a chance, to implement the logic behind the scene.
You‘ve been given N integers A[1], A[2],…, A
. On these integers, you need to implement the following operations:
1. C l r d: Adding a constant d for every {Ai | l <= i <= r}, and increase the time stamp by 1, this is the only operation that will cause the time stamp increase.
2. Q l r: Querying the current sum of {Ai | l <= i <= r}.
3. H l r t: Querying a history sum of {Ai | l <= i <= r} in time t.
4. B t: Back to time t. And once you decide return to a past, you can never be access to a forward edition anymore.
.. N, M ≤ 105, |A[i]| ≤ 109, 1 ≤ l ≤ r ≤ N, |d| ≤ 104 .. the system start from time 0, and the first modification is in time 1, t ≥ 0, and won’t introduce you to a future state.
Input
n m
A1 A2 … An
… (here following the m operations. )
Output
… (for each query, simply print the result. )
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
2 4
0 0
C 1 1 1
C 2 2 -1
Q 1 2
H 1 2 1
Sample Output
4
55
9
15
0
1
Author
HIT
Source
2012 Multi-University Training Contest 5
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> using namespace std; const int N = 100000 + 5; long long tre[N * 25], add[N * 25]; int tot, lc[N * 25], rc[N * 25], root[N * 25]; void build(int t, int l, int r) { if (l == r) { // tre[t]=a[l]; cin >> tre[t]; return; } int mid = l + r >> 1; build(lc[t] = ++tot, l, mid); build(rc[t] = ++tot, mid + 1, r); tre[t] = tre[lc[t]] + tre[rc[t]]; } void update(int last, int cur, int l, int r, int x, int y, int k) { tre[cur] = tre[last] + (y-x + 1)*k; rc[cur] = rc[last]; lc[cur] = lc[last]; add[cur] = add[last]; if (x <= l&&r <= y) { add[cur] += k; return; } int mid = l + r >> 1; if (y <= mid) { update(lc[last], lc[cur] = ++tot, l, mid, x, y, k); } else if (x>mid) { update(rc[last], rc[cur] = ++tot, mid + 1, r, x, y, k); } else { update(lc[last], lc[cur] = ++tot, l, mid, x, mid, k); update(rc[last], rc[cur] = ++tot, mid + 1, r, mid+1, y, k); } } long long query(int t, int l, int r, int x, int y, int sum) { if (x <= l&&r <= y) { return tre[t] + (y - x + 1)*sum; } int mid = l + r >> 1; if (y <= mid) { return query(lc[t], l, mid, x, y, sum + add[t]); } else if (x>mid) { return query(rc[t], mid + 1, r, x, y, sum + add[t]); } else { return query(lc[t], l, mid, x, mid, sum + add[t]) + query(rc[t], mid + 1, r, mid+1, y, sum + add[t]); } } int main() { cout << log2(100000); int n, m; while (~scanf("%d%d", &n, &m)) { memset(add, 0, sizeof(add)); memset(lc, 0, sizeof(lc)); memchr(rc, 0, sizeof(rc)); tot = 0; build(root[1] = ++tot, 1, n); int time = 1; while (m--) { char s[10]; scanf("%s", s); if (s[0] == 'C') { int l, r, k; scanf("%d%d%d", &l, &r, &k); time++; update(root[time - 1], root[time] = ++tot, 1, n, l, r, k); } else if (s[0] == 'Q') { int l, r; scanf("%d%d", &l, &r); cout << query(root[time], 1, n, l, r, 0) << endl; } else if (s[0] == 'H') { int l, r, t; cin >> l >> r >> t; cout << query(root[t + 1], 1, n, l, r, 0) << endl; } else if (s[0] == 'B') { int t; cin >> t; time = t + 1; } } } }
相关文章推荐
- 主席树(可持久化线段树)
- 入坑 可持久化线段树——主席树
- 主席树(可持久化线段树)学习笔记
- 主席树——可持久化值域线段树
- 主席树(可持久化线段树)讲解 [POJ 2104] K-th Number
- 可持久化线段树(主席树)【数组】
- P3834 【模板】可持久化线段树 1(主席树)
- HDU 2665 Kth number [可持久化线段树 主席树]
- 主席树/函数式线段树/可持久化线段树 学习指南
- 主席树练习1——P3834 【模板】可持久化线段树 1(主席树)
- HDU 4417 Super Mario [可持久化线段树 主席树]
- 关于 (主席树) (可持久化线段树) (动态开点线段树) 的问题
- hdu 2665 可持久化线段树求区间第K大值(函数式线段树||主席树)
- 主席树——可持久化权值线段树
- HDU 2665(主席树/可持久化线段树)
- 【POJ2104】K-th Number 主席树?函数式线段树?可持久化线段树?……反正是其中一个
- hdu2665 主席树(可持久化线段树)
- 主席树/函数式线段树/可持久化线段树
- 【模板】可持久化线段树 1(主席树)
- 【模板】可持久化线段树(主席树)