[cf893f] Subtree Minimum Query (Chairman tree)

Source: Internet
Author: User

Surface:

Portal: Http://codeforces.com/problemset/problem/893/F

to give you a root tree, point to the right value, ask you each node in the subtree distance from its point not exceeding K of the value of the minimum value. (Benquan are 1, mandatory online )

Solution

This is a very interesting question.

We generally see this distance does not exceed the problem of K, the first reaction is generally built with depth as subscript, the DFS sequence for the time axis of the Chairman tree.

Unfortunately, the interval minimum does not derive the state of a subtree by subtracting its historical state .

So, this is the idea of the transformation.

Consider the DFS sequence as the subscript, with the depth of the timeline to build a Chairman tree .

We can take the BFS and add the dots in depth and layer by layer.

In this way, we can query the minimum weight of the subtree within the corresponding depth.

In the sauce, we can cut off the problem's finger licking ( ̄▽ ̄)?

Code
#include <iostream>#include<cstdio>#include<vector>using namespacestd;Long LongRead () {Long Longx=0, f=1;CharC=GetChar ();  while(!isdigit (c)) {if(c=='-') f=-1; c=GetChar ();}  while(IsDigit (c)) {x=x*Ten+c-'0'; c=GetChar ();} returnx*F;}Const intn=100000+ +;Const intm= -*N;intN,r,a[n];vector<int>E[n];structsegmenttree{Static Const intinf=0x3f3f3f3f; #defineMid ((now_l+now_r) >>1)intmin[m],son[m][2],cnt; InlinevoidUpdateintNow ) {Min[now]=min (min[son[now][0]],min[son[now][1]]); }    voidBuild (intNowintnow_l,intnow_r) {        if(now_l==now_r) {Min[now]=inf; return; } Build (son[now][0]=++cnt,now_l,mid); Build (son[now][1]=++cnt,mid+1, Now_r);    Update (now); }    voidChange (intXintNumintNowintPreintnow_l,intnow_r) {        if(now_l==now_r) {Min[now]=num; return; }        if(X<=mid) son[now][1]=son[pre][1],change (x,num,son[now][0]=++cnt,son[pre][0],now_l,mid); Elseson[now][0]=son[pre][0],change (x,num,son[now][1]=++cnt,son[pre][1],mid+1, Now_r);    Update (now); }    intQuery (intLintRintNowintnow_l,intnow_r) {        if(Now_l>=l and now_r<=R)returnMin[now]; intans=inf; if(L<=mid) Ans=min (Ans,query (l,r,son[now][0],now_l,mid)); if(R>mid) Ans=min (Ans,query (l,r,son[now][1],mid+1, Now_r)); returnans; }    voidPrint (intNowintnow_l,intnow_r) {Cerr<<"No."<<now<<"Now_l&r:"<<now_l<<" "<<now_r<<"Sonl&r"<<son[now][0]<<" "<<son[now][1]<<"MIN:"<<MIN[now]<<Endl; if(now_l!=now_r) {Print (son[now][0],now_l,mid); Print (son[now][1],mid+1, Now_r); }    }        #undefMid}sgt;intDfn[n],depth[n],dfn_to,size[n],depth_max;voidDfsintNow ) {Depth_max=Max (Depth_max,depth[now]); Dfn[now]=++dfn_to; Size[now]=1;  for(intI=0;i<int(E[now].size ()); i++)        if(dfn[e[now][i]]==0) {Depth[e[now][i]]=depth[now]+1;            DFS (E[now][i]); Size[now]+=Size[e[now][i]]; }}intDl[n],front,tail,root[n];voidBFs () {Dl[tail++]=R; intdepth_now=0;  while(tail>front) {        intnow=Dl[front]; inttemp=Root[depth_now]; if(depth[now]!=Depth_now) {Depth_now=Depth[now]; Temp=root[depth_now-1]; } Root[depth_now]=++sgt.cnt; Sgt. Change (Dfn[now],a[now],root[depth_now],temp,1, N); //Sgt.        Print (Root[depth_now],1,n); //cerr<<endl;         for(intI=0;i<int(E[now].size ()); i++)            if(depth[e[now][i]]>Depth[now]) Dl[tail++]=E[now][i]; Front++; }}intMain () {n=read (), r=read ();  for(intI=1; i<=n;i++) A[i]=read ();  for(intI=1; i<n;i++)    {        intS=read (), t=read ();        E[s].push_back (t);    E[t].push_back (s); } Depth[r]=1;    DFS (R); Sgt. Build (0,1, N); //Sgt.    Print (0,1,n); //cerr<<endl;BFS (); intM=read (), lans=0;  for(intI=1; i<=m;i++)    {        intX=read (), k=read (); X= ((X+lans)%n) +1, k= (K+lans)%N; intTemp=min (depth[x]+K,depth_max); LANs=sgt. Query (dfn[x],dfn[x]+size[x]-1, Root[temp],1, N); printf ("%d\n", LANs); }    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.