Luo Gu P3834 [template] can persist tree line 1 (Chairman of the tree) Chairman tree

URL: https://www.luogu.org/problem/P3834

Meaning of the questions:

$ N-number is given $, $ m $ a query, inquiry interval $ l $ ~ $ r $ of $ k $ small.

answer:

Establishing a weight value segment tree, is stored in the node segment tree is the number of different numbers of the sub-tree, and then for the interval $ [1, k] $, $ k \ in (1, n) $ established segment tree, apparently interval $ [1, r] - [1, l-1] $ is the number of the required number of intervals, then the query can be in this range. See in particular code.

AC Code:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 200005
struct value
{
    int id;
    int x;
};
value v[MAXN];
int hashing[MAXN];
bool cmp(const value &a,const value &b)
{
    return a.x<b.x;
}
int cnt=0;
struct cheiftree
{
    struct node
    {
        int l,r;//l,r记录节点编号而不是区间长
        int sum;
        node()
        {
            sum=0;
        }
    };
    node tr[MAXN*20];
    int root[MAXN];
    the init void () 
            // Similarly right child
    {
        cnt = 1; // count root 
        tr [0] .l = tr [ 0] .r = tr [0] .sum = 0; // children around weights is initialized to 0 
        the root [0] = 0; / / root initialized to 0       
    } 
    void Update (int L, R & lt int, int & NRT, int NUM) // L, m represents the range of the interval 
    { 
        TR [CNT ++] = [NRT] TR; // new roots, nrt representatives the unit in this case, the root [-I. 1] 
        NRT = cnt-1; // cnt-1 is assigned to the root [i] is the root of the tag number 
        ++ tr [nrt] .sum; // then pass a new number, i.e. the number of +1 the number of weights, that is, the number of the root [-I. 1] + 1 
        IF (R & lt == L) reaches a leaf node // 
            return; 
        int m = (L + R & lt) / 2; // midpoint computing section 
        IF (NUM <= m) 
            update (L, m, TR [NRT] .L, NUM); // root of the left child of the current incoming 
            // Since update using a reference, so the left child It will be linked to its parent node on 
        the else 
            Update (. 1 + m, R & lt, TR [NRT] .r, NUM); 
    }
    int query (int rl, int rr , int k, int l, int r) // rl i refers to the tree before, rr j refers to the tree before, k means the k-th largest, l and r refers to a range 
    { 
        int D = TR [tr [rr] .l] .sum -tr [tr [rl] .l] .sum; // first incoming rl and rr is the root of root [a-1] and root [b], and TR [rr ] .L and tr [rl] .l is left son of the root of two, d represents the first half of the maximum value of this weight range (that is, a maximum of several hours) 
        IF (R & lt == L) 
            return L; 
        int m = ( R & lt + L) / 2; 
        IF (K <= D) 
            return Query (TR [rl is an] .L, TR [RR] .L, K, L, m); // K in which 
        the else 
            return Query (TR [rl is an ] .r, tr [rr] .r , kd, m + 1, r); // k is greater than the maximum priority value 
    } 
}; 
cheiftree CT; 
int main () 
{ 
    int n-, m; 
    iOS :: sync_with_stdio (0) ; 
    cin.tie (0); 
    CIN >> >> n-m; 
    // discretization
    for (int I =. 1; I <= n-; I ++) 
        CIN >> V [I] .x, V [I] = I .id; 
    Sort (. 1 + V, V +. 1 + n-, CMP); 
    for (int I =. 1; I <= n-; I ++) 
        Hashing [V [I] .id] = I; 
    // President tree 
    ct.init (); 
    for (int I =. 1; I <= n-; I ++) 
    { 
        ct.root [I] = ct.root [-I. 1]; 
        // If a value is added each time; ct.update (1, n, ct.root [i], hashing [i]) build a new tree, the new node is added to the value of the weights in the tree 
    } 
    int A, B, K; 
    for (int I = 0; I <m; I ++) 
    { 
        CIN >> A >> B >> K ; 
        //v[i].id=b,hash[v[i].id]=c => V [C] = V [the hash [V [I] .id]] = V [I]; 
        COUT < <v [ct.query (ct.root [a -1], ct.root [b], k, 1, n)] x << endl;. // query interval k-th largest value of the hash, then the inverse Release true value 
    } 
    return 0; 
}

 

Guess you like

Origin www.cnblogs.com/Aya-Uchida/p/11247303.html