Data structures that can be persisted

Source: Internet
Author: User
Tags data structures versions

A sustainable data structure is primarily a case where we can query historical versions and support insertions, using previous historical versions of the data structures to reduce space consumption (the ability to modify history is functional).

Here we only talk about the more commonly used sustainable segment trees and trie.

For the segment tree we record each node's left and right son, if space permits, we can also record the interval of each number, for the label operation we need to create a new two nodes to represent the history, more commonly used is the use of a sustainable line tree to maintain the weight, and then maintain the distribution of weights in different intervals, The classical example is the interval k large value without modification, we use this problem as an example to explain the operation of the sustainable segment tree.

First of all, if we only ask the K-value of the interval [1,n], we can disperse the weights of each point first, then we set up a weight tree to represent the distribution of weights, so we just need to determine whether the node of the left son is greater than K, and then two points to find it, So for the interval query we need to establish the weighted tree of this interval, so we first build an empty tree, which means that there is no insertion of the interval of the case, and then in order to continue to insert this value in sequence, because the weight tree to meet the nature of the addition and subtraction (that is, interval [l,r] in the number of x values is the interval [1,r] The number of x in the is minus the number of x in the interval [1,l-1], so we can work out the weight tree for all the intervals [1,i], but the space will be very large. So we consider an insertion, this time the insertion change is only for this LOGN node that contains the X interval, then the remaining nodes can be inherited from the previous history version.

Building an empty tree is easier, set T[X].SON[0/1] to represent the X-node of the left and right son, T[x].left,right represents the X-node represented by the left and the the interval, t[x].cnt indicates that the node represents the number of weights for the interval. Then we can get

void build (int &x,int l,int r) {
    if (!x) x=tot++;
    T[x].left=l; T[x].right=r;
    if (t[x].left==t[x].right) return;
    int mid=t[x].left+t[x].right>>1;
    Build (T[x].son[0],l,mid); Build (T[x].son[1],mid+1,r);
}

The following is the insert operation, which means that we create a segment tree with a value of key and inherit the segment tree with the history version of the rot

void Insert (int &x,int rot,int key) {
    if (!x) x=tot++;
    T[x].left=t[rot].left; T[x].right=t[rot].right;
    if (t[x].left==t[x].right) {
        t[x].cnt=t[rot].cnt+1;
        return;
    }
    int mid=t[x].left+t[x].right>>1;
    if (key>mid) {
        t[x].son[0]=t[rot].son[0];
        Insert (T[x].son[1],t[rot].son[1],key);
    } else {
        t[x].son[1]=t[rot].son[1];
        Insert (T[x].son[0],t[rot].son[0],key);
    }
    t[x].cnt=t[rot].cnt+1;
}

Because the historical version needs to be inherited, all the information for this node, such as Left,right, needs to be inherited, and CNT needs to be updated.

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

So now that we have the N-line tree, with Rot[i] representing the root node of the weighted tree of the interval [1,i] (the node representing the weighted interval of [1,max_sum]), then we can use ROT[R]-ROT[L-1 for the query [L,r] to determine that the representation interval is [l,r] The value of the CNT in the point of the weight tree.

Here are a few more simple questions about the weight tree.

Bzoj 3524: The puzzle

Bzoj 3027: The puzzle

Bzoj 3123: The puzzle

There are other ways to use a persistent line-segment tree, such as

Bzoj 2653: The puzzle

The following is a brief introduction to the persistence of trie.

The simplest type of trie is the two-fork trie tree that stores binary binary since XOR is a group and satisfies the properties of the addition, we can maintain the XOR value of interval [1,i] to maintain the XOR value of interval [l,r] According to this property. This allows us to deal with the question of the maximum value of the XOR value of all numbers and x in the interval [l,r], given a sequence.

If you want to maintain the size of the number of 1<<31 around, at the beginning we will build a depth of 31 two fork tree, such space consumption is certainly not allowed, then we can at the beginning without building empty trees, but directly into each node.

HDU 4757: The puzzle

Bzoj 2741: The puzzle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.