[12.10] Diary

12.10 Diary

Chairman of the tree

  1. P2617 (Chairman tree with repair template): given sequence number of n, + Query Interval k small single-point modification. This non-mandatory questions online.

Ideas : In fact, the Chairman of the tree is only a thought repeated reuse of space, not a particular data structure. Instead, he opened and dynamic point there are a lot of similarities. Even said common segment tree is a special kind of abstract tree line. I feel entitled to do so many segments of the tree, this is the best version I summed up the.

First segment of the tree structure:

struct Tree{
    int l,r,val;
    Tree(int a=0,int b=0,int c=0):l(a),r(b),val(c){}
}v[M*200];

\ (L \) represents a subscript of the left son, \ (R & lt \) represents a subscript of the right son, \ (Val \) represents a weight value of each node tree line.

The real segment tree is the case. Just Traditionally, in order to facilitate beginners to understand, we are all from the \ (l = id * 2, r = id * 2 + 1 \) This simple tree line start with. Indeed son left and right son does not necessarily have to be fixed twice relationship, not necessarily even have two sons, you write three sons, became l, m, r, and then do some more operating time, It may be better (my mouth Hu), but write them too much trouble, but the difference is most constant.

So if the father and son relationship is not fixed, then determine how to scale it down son? It is simply

  • Dynamic open point. Before each went to a node, you want to operate (operate / query), first to determine what the node exists (whether or not equal to 0), if it is 0, then that would not operate, and for the query, direct return 0 on the line (for summation with a single point of inquiry), less a bunch of constants of a wood there! ?
  • Some have been connected to the node, for example, the President of trees. Then the deeper parts of your son do not have control, and a bunch of less constant.

This idea is so useful, simple terms is, for the tree line, I just need to know the node number l, r son , as is not twice or small to large, it does not matter .

Get rid of the shackles of this idea, then Chairman of the tree is relatively easy to understand it, because in fact modify every single point will only modify a chain, so the only change \ (\ log n \) nodes, so a new tree line, when many nodes are then connected directly to the previous tree, do not need to re-new, it reached reusing existing information, the purpose of the anti-bombing space.

But if it can not be used to modify the President of the interval tree, because node change far more than \ (\ log n \) a, naturally, can not achieve the purpose of reuse.

So if you want to modify the interval how to do it? One key idea of data structures: the subtraction section by establishing a differential array, to become a single point addition and subtraction , so that the interval becomes and the corresponding node values, and can then set an interval request back to the original interval and a. The complexity of the case, just in time to modify and maintain more constant double the progressive complexity should be consistent. (The above are personal Hu mouth, I have not written).

To sum up the basic operations common (in fact, balanced tree) of (insert, delete, find No. k, k ranking of seeking to find k precursor, find the successor k) implementation (basic operation is to operate (the number of change), query_num (a number of inquiries there are several), query_rk (k-query ranking who is), query_sa (k seek the ranking), the latter two can be used to find a successor precursor front of several operations to achieve) :( go back and summarize it)

  • Static Overall: direct sort

  • Dynamic whole (with repair):
  • Dynamic overall (with a mandatory repair + online): Weight segment tree
  • Static range: Chairman of the tree
  • Dynamic range (with repair):
  • Dynamic range (with mandatory repair + online): Fenwick tree, each node is a tree line weights, dynamic open point. \ (^ nlog 2n \) .

(My diary a few days if you do not know what the weight segment tree is recommended before looking -)

I still do not dynamically offline practice, only by force large constant practice. I should feel that sets off a layer of CDQ after seeing solution to a problem? In order to achieve replacement level data structures + by constant action?

Well, said so much nonsense, this talk about the question how to do it.

The problem is the dynamic range (with repair), offline (whole half), Los solution to a problem in the Valley have, but I will not CDQ. So we talk about online practices.

First think, for an interval, if I get this weight interval tree line consisting of all numbers, then this question becomes a dynamic whole, and write functions directly in the search tree line weights on it.

So how about every fast to get specified interval corresponding weights tree line ?

Taking into account the weight itself has an adder tree line associativity, because each node is represented by the number of values, obviously incorrect. A tree can be set outside the array, recording the weight of the segment tree and the corresponding section. Proved that by the section, any interval need only be divided up \ (\ log n \) subintervals, the obtained value of a node of the tree segment weight corresponding to the specified range , the complexity is \ (O (\ log n ) \) , this method is that the interval corresponding to n small sections of tree line weights, corresponding to this node values are added together.

Therefore, the equivalent of a total particle weight nlogn tree line, space certainly fried, it must be discrete, and then open the dynamic point, to maximize the space-saving (though you can not actually discrete too).

Then each modification, equivalent to \ (O (\ log n) \) pieces of single-point modified segment tree, so complexity is \ (log ^ 2n \) a.

Meng Xin: I know what you want to modify what logn stars?

I: Fenwick tree ah, every lowbit directly on the line.

It is recommended for use Fenwick tree form, it is easy to understand:

for(int i=x;i;i-=lowbit(i))
    操作

In this case, all i is the query (1, x) prefix and when this range is divided into that \ (O (\ log n) \) intervals subscript (or is the number of weights segment tree). Every so write on the line, a no-brainer, I do not want the principle of good.

Each query, you need to pay attention, you still operate in a tree on the nature of the segment weights, but this segment tree in memory space you does not really exist, except that he may split into logn pieces you've already saved and trees. So for this "virtual" weight tree line, the value of each node should use the time logn go together, and that this value corresponds to the weight of this segment tree node. Only a single query when in use.

That his son about it? You did not actually exist, and just, like, just know that the demolition son became logn stars tree line corresponding node. So every time before ...... go, now have to use a bit of the current array of deposit and every single tree line went where, if his son left to go, so logn stars tree line should go left son, this operation is also logn. If you have a dynamic open point, it is likely to come to a certain depth when some son all 0s, then you can save some time, but be aware that once complexity is logn, can actually save a lot (but this time I code did not write).

Then the problem is over, look at the code. Fenwick tree tree line and the weight of pos, id really easy to mix and so on.

(Balanced tree? What is that? I will only weights segment tree thank you)

#include<bits/stdc++.h>
using namespace std;
#define mid ((l+r)>>1)
#define db(x) cout<<#x<<":"<<x<<endl;
const int M=1e5+20;
vector<int> lsh;
unordered_map<int,int> rev;
struct Opt{
    char op[2];
    int l,r,k;
    Opt():l(0),r(0),k(0){
        op[0]='\000';
    }
}opt[M];
struct Tree{
    int l,r,val;
    Tree(int a=0,int b=0,int c=0):l(a),r(b),val(c){}
}v[M*200];
int a[M],cnt,len,now[M*2];
inline int lowbit(int x){return x&(-x);}
void operate(int &id,int l,int r,int pos,int x){
    if (!id)
        id=++cnt;
    v[id].val+=x;
    if (l==r)
        return;
    if (pos<=mid)
        operate(v[id].l,l,mid,pos,x);
    else
        operate(v[id].r,mid+1,r,pos,x);
}
inline void BIToperate(int id,int pos,int k){
    while(id<=len)
        operate(id,1,len,pos,k),id+=lowbit(id);
}
int query_rk(int l,int r,int ql,int qr,int k){
    int Lnum=0;
    if (l==r){
        for(int i=qr;i;i-=lowbit(i))
            now[i]=i;
        for(int i=ql-1;i;i-=lowbit(i))
            now[i]=i;
        return l;
    }
    for(int i=qr;i;i-=lowbit(i))
        Lnum+=v[v[now[i]].l].val;
    for(int i=ql-1;i;i-=lowbit(i))
        Lnum-=v[v[now[i]].l].val;
    if (Lnum>=k){
        for(int i=qr;i;i-=lowbit(i))
            now[i]=v[now[i]].l;
        for(int i=ql-1;i;i-=lowbit(i))
            now[i]=v[now[i]].l;
        return query_rk(l,mid,ql,qr,k);
    }
    else{
        for(int i=qr;i;i-=lowbit(i))
            now[i]=v[now[i]].r;
        for(int i=ql-1;i;i-=lowbit(i))
            now[i]=v[now[i]].r;
        return query_rk(mid+1,r,ql,qr,k-Lnum);
    }
}
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i)
        scanf("%d",&a[i]),lsh.push_back(a[i]);
    for(int i=1;i<=m;++i){
        scanf("%s",opt[i].op);
        if (opt[i].op[0]=='Q')
            scanf("%d%d%d",&opt[i].l,&opt[i].r,&opt[i].k);
        else
            scanf("%d%d",&opt[i].l,&opt[i].r),lsh.push_back(opt[i].r);
    }
    sort(lsh.begin(),lsh.end());
    len=unique(lsh.begin(),lsh.end())-lsh.begin();
    for(int i=0;i<len;++i)
        rev[lsh[i]]=i+1;
    for(int i=1;i<=len;++i)
        now[i]=i;
    cnt=len;
    for(int i=1;i<=n;++i)
        BIToperate(i,rev[a[i]],1);
    for(int i=1;i<=m;++i)
        if (opt[i].op[0]=='Q')
            printf("%d\n",lsh[query_rk(1,len,opt[i].l,opt[i].r,opt[i].k)-1]);
        else
            BIToperate(opt[i].l,rev[a[opt[i].l]],-1),a[opt[i].l]=opt[i].r,BIToperate(opt[i].l,rev[a[opt[i].l]],1);
    return 0;
}

to sum up

Day today laboratory experiments not doing well, we are not very happy. Of course, there is no time to write the title, only this one. But the process of writing really learned a lot, write a second channel tree cover tree. The key is in order to understand, to understand, to write the code will not be a problem, even do not need to debug.

Then there would be the question of Hong Kong H, interval query balanced tree (two forced balanced tree) A will can wear. So think so, Hong Kong beat copper tail is a really my own food.

Tomorrow Plan

  1. Two forced balanced tree P3380
  2. Hong Kong H title, could not pass, then look off school practice.
  3. FHQ-treap and scapegoat tree on the first do not learn it, learn also feel bad practice, you finally learn about everything CDQ partition, consolidate and more strings and mathematics before it on, then so be it.

Guess you like

Origin www.cnblogs.com/diorvh/p/12020122.html