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]>=BB) {return r-l+1; } else if (MINN[RT]>MM) return 0; else if (MAXX[RT]<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