"Segment Tree" CSU 1414 Query on a tree

Source: Internet
Author: User

Click to open link

Line tree new function get, it's amazing. @[email protected]

Go through the tree first, the time stamp records the time before and after

The pre-and post-time of the child node will be within the parent node's time range

Maintaining the minimum depth and depth of the interval with the segment tree

#include <cstdio> #include <cstring> #include <cstdlib> #include <string> #include <iostream > #include <algorithm> #include <sstream> #include <cmath>using namespace std; #include <queue > #include <stack> #include <set> #include <vector> #include <deque> #include <map># Define Cler (arr, Val) memset (arr, Val, sizeof (arr)) #pragma comment (linker, "/stack:102400000,102400000") typedef long LO ng Ll;const int maxn = 102020;const int MAXM = 240000;const int INF = 0x3f3f3f3f;const int mod = 1000000007; #define Lson L,m,rt<<1#define Rson M+1,r,rt<<1|1int Ti,maxx[maxn<<2],minn[maxn<<2],head[maxn],tol,deep [maxn];struct node{int v,next;} edge[maxm];struct node1{int t1,t2;} Tt[maxn];void init () {Cler (head,-1); tol=0;ti=0;} void Addedge (int u,int v) {edge[tol].v=v;edge[tol].next=head[u];head[u]=tol++;}    void Dfs (int u,int dep) {DEEP[TI]=DEP; for (int i=head[u];~i;i=edge[i].next) {iNT V=EDGE[I].V;        Tt[v].t1=++ti;        DFS (V,DEP+1);    Tt[v].t2=ti;    }}void DFS1 (int u,int dep) {tt[u].t1=++ti;    DEEP[TI]=DEP;        for (int i=head[u];~i;i=edge[i].next) {int v=edge[i].v;    DFS1 (v,dep+1); } Tt[u].t2=ti;}    void pushup (int rt) {Maxx[rt]=max (maxx[rt<<1],maxx[rt<<1|1]); Minn[rt]=min (minn[rt<<1],minn[rt<<1|1]);}        void build (int l,int R,int RT) {if (l==r) {maxx[rt]=minn[rt]=deep[l];    return;    } int m= (L+R) >>1;    Build (Lson);    Build (Rson); Pushup (RT);} int query (int l,int r,int bb,int mm,int l,int r,int RT) {if (l<=l&&r<=r&&maxx[rt]<=mm&&    MINN[RT]&GT;=BB) {return r-l+1;    } else if (MINN[RT]&GT;MM) return 0;    else if (MAXX[RT]&LT;BB) return 0;    int m= (L+R) >>1;    int ans=0;    if (l<=m) ans+=query (L,r,bb,mm,lson);    if (r>m) ans+=query (L,r,bb,mm,rson); return ans;} int main () {#ifndef Online_judge freopen ("In.txt"), "R", stdin);    Freopen ("OUT.txt", "w", stdout); #endif int t,n,v,m,x,d;    scanf ("%d", &t);        while (t--) {init ();        scanf ("%d", &n);            for (int i=2;i<=n;i++) {scanf ("%d", &v);        Addedge (V,i);        } ti=0;        DFS1 (1,0);        Build (1,n,1);        scanf ("%d", &m);            while (m--) {scanf ("%d%d", &x,&d);            int Ans=query (tt[x].t1,tt[x].t2,deep[tt[x].t1],deep[tt[x].t1]+d-1,1,n,1);        printf ("%d\n", ans); }} return 0;}


"Segment Tree" CSU 1414 Query on a 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.