cf893f subtree Minimum Query Chairman Tree

Source: Internet
Author: User

If it's a sum, it's good to do it ...

It's not a sum or a harmless ....

One dimension is too difficult to limit the conditions, consider the two-dimensional limit

One-dimensional $dfs$ sequence, one-dimensional $dep$ sequence

Inquire $ (x, K) $ for the $dfs$ on $[dfn[x], dfn[x] + sz[x]-1]$, $dep$ in order to check $[dep[x], dep[x] + k]$

This way, each query corresponds to the minimum value within a rectangle

However, trees are not able to live .....

Find an inquiry that seems to correspond to a section in the $dep$ sequence, which can actually be extended to a corresponding prefix

This way, you just need a chairman tree to do it.

Complexity $o (n \log N) $

#include <map>#include<queue>#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>namespaceRemoon {    #defineRI Register int#defineTPR Template <typename ra>#defineRep (IU, St, ed) for (ri iu = st; IU <= ed; iu + +)#defineDrep (IU, ed, St) for (ri iu = ed; iu >= St; IU--)#defineGC GetCharinlineintRead () {intp =0, W =1;Charc =GC ();  while(C >'9'|| C <'0') {if(c = ='-') W =-1; c =GC ();}  while(c >='0'&& C <='9') p = p *Ten+ C-'0', C =GC (); returnP *W; }    intwr[ -], rw; #definePC (IW) Putchar (IW)TPR InlinevoidWrite (Ra o,Charc ='\ n') {        if(!o) PC ('0'); if(O <0) o =-O, PC ('-');  while(o) wr[++ rw] = o%Ten, O/=Ten;  while(rw) PC (WR[RW--] +'0');    PC (c); } TPR InlinevoidCmin (RA &a, RA b) {if(A > B) a =b;} TPR InlinevoidCmax (RA &a, RA b) {if(A < b) A =b;} TPR InlineBOOLCkmin (RA &a, RA b) {return(A > B)? A = B,1:0; } TPR InlineBOOLCkmax (RA &a, RA b) {return(A < b)? A = B,1:0; }}using namespacestd;using namespaceRemoon;#defineSid 300050#defineOID 12050000intDfn[sid], sz[sid];intN, R, M, ID, Tim, CNP, Mxd;intRt[sid], Nxt[sid], Node[sid], cap[sid];intFa[sid], Q[sid], W[sid], dep[sid];intls[oid], rs[oid], Miv[oid];inlinevoidAddedge (intUintv) {nxt[+ + CNP] = Cap[u]; Cap[u] = CNP; NODE[CNP] =v;}#defineCur Node[i]inlinevoidDfsintOintf) {Fa[o]= f; Dep[o] = Dep[f] +1; Sz[o]=1; Dfn[o] = + +Tim;  for(inti = Cap[o]; I i =Nxt[i])if(cur! = f) Dfs (cur, o), sz[o] + =sz[cur];} InlinevoidUpdateint&now,intPreintLintRintPintv) { now= ++ID; Ls[now]= Ls[pre]; Rs[now] =Rs[pre]; Miv[now]=min (Miv[pre], V); if(L = = r)return; intMid = (L + r) >>1; if(P <=mid) Update (Ls[now], Ls[pre], L, Mid, p, v); ElseUpdate (Rs[now], Rs[pre], Mid +1, R, p, v);} InlinevoidInsertint&now,intPreintLintRintPintv) { now= ++ID; Ls[now]= Ls[pre]; Rs[now] =Rs[pre]; Miv[now]=min (Miv[pre], V); if(L = = r)return; intMid = (L + r) >>1; if(P <=mid) Insert (Ls[now], Ls[pre], L, Mid, p, v); ElseInsert (Rs[now], Rs[pre], Mid +1, R, p, v);} Inlinevoidbuild () {intFR =1, to =0; q[+ + to] = R; miv[0] =1e9;  while(Fr <=To ) {        into =Q[FR];  for(RI i = cap[o]; i; i =Nxt[i])if(cur! = Fa[o]) q[++ to] =cur; if(Dep[o]! = DEP[Q[FR-1]] ) Insert (Rt[dep[o]], Rt[dep[o]-1],1, N, Dfn[o], w[o]); ElseUpdate (Rt[dep[o]], Rt[dep[o]],1, N, Dfn[o], w[o]); Fr++; Cmax (Mxd, Dep[o]); }}inlineintQryintOintLintRintmlintMR) {    if(ml > R | | | Mr < L | |!o)return1e9; if(ML <= l && Mr >= R)returnMiv[o]; intMid = (L + r) >>1; returnMin (Qry (Ls[o], L, Mid, ML, Mr), Qry (Rs[o], Mid +1, R, ML, Mr));}intMain () {n= Read (); R =read (); Rep (I,1, n) w[i] =read (); Rep (I,2, N) {        intU = Read (), V =read (); Addedge (U, v);    Addedge (V, u); } dfs (R,0);    Build (); intLST =0; m =read (); Rep (I,1, M) {        intx = (read () + LST)% n +1; intK = (read () + LST)%N; Write (LST= Qry (Rt[min (dep[x] + K, mxd)],1, N, Dfn[x], dfn[x] + sz[x]-1)); }    return 0;}

cf893f subtree Minimum Query Chairman Tree

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.