Knowledge Point: Chairman of the tree

concept

Chairman of the tree, also known as the tree line can be persistent,
in fact, for the state after each modification of the tree storage
means we can operate on any tree of this state

achieve

We then consider modifying in the end modified the number of points,
that is, before the tree line and modified after revision tree line which points are different
by storing these changes point
spatial complexity can achieve \ (O (n + n * log_n) \)
but the time complexity is still \ (O (log_n) \)

Analogy

A job is not done because you want to copy B job
but because of lack of time A, A think as little as possible to copy
how to achieve it, find some questions if A and B are wrong job
A in that their work is a question make a mark on
other issues directly linked to job B go up
, but if you want to change jobs B answer?
Obviously, A B will not let him amend pressed
because after modification B, A's need to re-work copying
other words,
check the teacher's working methods must ask what is the answer to some questions is the A
and A can not see job answer
here also reflects a limitation Chairman of the tree,
after a state of calm can not be modified

example

topic

Code

#include<iostream>
using namespace std;
struct node
{
    int l,r;
    int lson;
    int rson;
    int maxx;
}tre[32*100000];
int n,q;
int cm;
int cnt;
int k,l,r;
int p,v;
int tot;
int root[1000005];
int build(int l,int r)
{
    int now;
    tot++;
    now=tot;
    tre[tot].l=l;
    tre[tot].r=r;
    if(l==r)
    {
        cin>>tre[tot].maxx;
        return tot;
    }
    int mid=(l+r)>>1;
    tre[now].lson=build(l,mid);
    tre[now].rson=build(mid+1,r);
    tre[now].maxx=max(tre[tre[now].lson].maxx,tre[tre[now].rson].maxx);
    return now;
}
int ask(int k,int l,int r)
{
    if(tre[k].r<l||r<tre[k].l)
        return 0;
    if(l<=tre[k].l&&tre[k].r<=r)
        return tre[k].maxx;
    return max(ask(tre[k].lson,l,r),ask(tre[k].rson,l,r));
}
int change(int k,int p,int v)
{
    int now;
    int mid=(tre[k].l+tre[k].r)>>1;
    if(tre[k].l==tre[k].r&&tre[k].l==p)
    {
        tre[++tot].l=tre[k].l;
        tre[tot].r=tre[k].r;
        tre[tot].maxx=v;
        return tot;
    }
    if(tre[k].l<=p&&p<=mid)
    {
        tot++;
        now=tot;
        tre[tot].l=tre[k].l;
        tre[tot].r=tre[k].r;
        tre[tot].rson=tre[k].rson;
        tre[tot].lson=change(tre[k].lson,p,v);
    }
    else
    {
        tot++;
        now=tot;
        tre[tot].l=tre[k].l;
        tre[tot].r=tre[k].r;
        tre[tot].lson=tre[k].lson;
        tre[tot].rson=change(tre[k].rson,p,v);
    }
    tre[now].maxx=max(tre[tre[now].lson].maxx,tre[tre[now].rson].maxx);
    return now;
}
int main()
{
//  ios::sync_with_stdio(false);
    cin>>n>>q;
    root[++cnt]=1;
    build(1,n);
    for(int i=1;i<=q;i++)
    {
        cin>>cm;
        if(cm==0)
        {
            cin>>k>>l>>r;
            cout<<ask(root[k],l,r)<<'\n';
        }
        else
        {
            cin>>k>>p>>v;
            root[++cnt]=change(root[k],p,v);
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/loney-s/p/11742087.html
Recommended