Test instructions
time limit: 20000ms single point time limit: 1000ms memory limit: 256MB description
Girly Fragrance These days are learning advanced theoretical computer science, but she has not learned anything, very painful. So she went out and flash and made some meaningless things to relax herself.
There is a tree of n nodes in front of the door, the fragrance found that there are N elves on the tree. However, these elves are shy and will only be active on a specific path. The Pokemon will be active on the AI to bi path.
Two elves are friends, when and only if their paths are of a common point.
So the fragrance wants to know, how many pairs of elves A and b,a and B are friends? Where a is not equal to b,a,b and b,a as a pair.
Input
The first row N and P (1 <= N, p <=100000) represent the size of the tree and the number of sprites. The nodes of the tree are labeled from 1 to N.
Next n-1 line, two numbers per line, a, a, indicates an edge between A and B.
Next P line, line I two number Ai,bi, indicating that the activity range of Elf I is AI to bi, where AI is not equal to BI.
Output
A row of answers that represents the logarithm.
-
Sample input
-
6 31 22 32 44 54 61 31 55 6
-
Sample output
2
-
Analysis
ORZ ...
I'm so stupid. Always want to cut the tree and the intersection of the line ... "And not a line segment
Great God's main puzzle here:
Two tree chains intersect, when and only if the LCA of a tree chain is on another tree chain, for each tree chain, count how many of the tree chains of LCA are included in him, sometimes two tree chains satisfy this condition, but only when the LCA of the two tree chains is equal, so it's a special sentence.
The above is clear, the interval and maintenance with a tree-like array can be.
1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <algorithm>6#include <cmath>7 using namespacestd;8 #defineMAXN 1000109 #defineLL Long LongTen One structnode A { - intX,y,next; -}t[maxn*2];intLen; the - intFIRST[MAXN],PX[MAXN],PY[MAXN]; - - voidInsintXinty) + { -t[++len].x=x;t[len].y=y; +t[len].next=first[x];first[x]=Len; A } at - intSON[MAXN],DFN[MAXN],SM[MAXN],DEP[MAXN],FA[MAXN]; - voidDFS1 (intXintf) - { -sm[x]=1; son[x]=0;d ep[x]=dep[f]+1; fa[x]=F; - for(intI=first[x];i;i=t[i].next)if(t[i].y!=f) in { - inty=t[i].y; to DFS1 (y,x); +sm[x]+=Sm[y]; - if(Sm[y]>sm[son[x]]) son[x]=y; the } * } $ Panax Notoginseng inttp[maxn],cnt; - voidDFS2 (intXintFintTPP) the { +dfn[x]=++cnt;tp[x]=TPP; A if(Son[x]) DFS2 (SON[X],X,TPP); the for(intI=first[x];i;i=t[i].next)if(t[i].y!=f&&t[i].y!=Son[x]) + DFS2 (T[I].Y,X,T[I].Y); - } $ $ intC[maxn],n; - BOOLLCA[MAXN]; - the voidAddintXinty) - {Wuyi for(inti=x;i<=n;i+=i& (-i)) thec[i]+=y; - } Wu - intQueryintLintR) About { $ intans=0; - for(inti=r;i>=1;i-=i& (-i)) -ans+=C[i]; -l--; A for(inti=l;i>=1;i-=i& (-i)) +ans-=C[i]; the returnans; - } $ the intGans (intXintYintp) the { the intans=0, TT; the while(tp[x]!=Tp[y]) - { in if(Dep[tp[x]]<dep[tp[y]]) tt=x,x=y,y=tt; the if(p==1) ans+=query (dfn[tp[x]],dfn[x]); thex=Fa[tp[x]]; About } the if(Dep[x]<dep[y]) tt=x,x=y,y=tt; the if(p==1) the { +ans+=query (dfn[y],dfn[x]); - returnans; the }Bayi Else returny; the } the - intMain () - { the intp; theLL ans=0; thescanf"%d%d",&n,&p); thelen=0; -memset (First,0,sizeof(first)); the for(intI=1; i<n;i++) the { the intx, y;94scanf"%d%d",&x,&y); the ins (x, y); ins (y,x); the } the for(intI=1; i<=p;i++) scanf ("%d%d",&px[i],&py[i]);98sm[0]=0;d ep[0]=0; AboutDFS1 (1,0); cnt=0; -DFS2 (1,0,1);101Memset (c,0,sizeof(c));102memset (LCA,0,sizeof(LCA));103 for(intI=1; i<=p;i++)104 { the intX=gans (Px[i],py[i],0);106lca[x]=1;107Add (Dfn[x],1);108 }109 for(intI=1; i<=p;i++) the {111 intX=gans (Px[i],py[i],1); theans+=x;113 } the for(intI=1; i<=n;i++)if(Lca[i]) the { the intx=query (Dfn[i],dfn[i]);117ans-=x* (x1)/2+x;118 }119printf"%lld\n", ans); - return 0;121}
View Code
2016-11-10 18:17:12
"Hihocoder 1167" Advanced Theoretical Computer Science (intersection of tree chains, segment tree or tree array maintenance interval and)