可持久化线段树——主席树

来源:互联网 发布:淘宝店主图设计 编辑:程序博客网 时间:2024/05/21 07:58

支持访问任意历史版本以及在历史版本上修改的数据结构 每次修改不是在原有线段树上直接修改,而是在旁边新建点。
时空复杂度都是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 Description
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[N]. 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;            }        }    }}
原创粉丝点击