Topic links
Time limit: 20000ms single point time limit: 1000ms memory limit: 256MB description
Gensokyo has a total of n dwellings, numbered from 1 to N. These dwellings were linked together by n-1, forming a tree-shaped structure.
Every dwelling is inhabited by a little elf. Every day the elves elect an interval [l,r], where the elves of the residence number are working together to complete a task.
In particular, the adjacent (with side) Two elves will spontaneously form a team, and if A and b adjacent to B and C, then A and C are also in the same team. After each day's task is completed, the team will dissolve and the next day the new team is formed according to the new interval.
Given the range of elves chosen every day, do you know the number of teams that are formed every day? input
The first line is two numbers n and q (1 <= N, q <= 100000), which represents the number of homes and number of days to team.
Next n-1 line, two numbers a and b per line, indicates that there is an edge between residence A and B.
The next Q line, two numbers L and r per line, satisfies the 1<=l<=r<=n, the range chosen for that day elf. Output
Output q lines, one integer per line, indicating the number of troops for that day. Sample input
3 1
1 2
2 3
1 3
Sample output
1
The problem: Because the graph is a tree, so this question has a very important nature: for a point interval [l,r] of the number of Unicom block, equal to this interval of points, minus the interval between the points and points between the number of sides.
For edges (u,v), U<v, as a sub-range [u,v]. Then the question is converted to how many sub-ranges are included in the [L,r] interval. This is a classic question. Offline processing of queries, sorted by R value from small to large. From 1 to N, the right value V of a sub-interval is added 1 to the left value U. When we encounter the right value R of the inquiry, this is the right value of all the sub-intervals that have been visited <=r, because we add 1 to the left value of the visited sub-interval, then the sum of the interval [l,r] is the number of sub-intervals in that interval.
The code is as follows:
#include <stdio.h> #include <algorithm> #include <iostream> #include <string.h> #define MoD
1000000007 #define NN 110000 typedef long long LL;
using namespace Std; struct node {int en,next;}
E[NN*2];
int n,q;
int p[nn],num;
void Init () {memset (p,-1,sizeof (p));
num=0;
} void Add (int st,int en) {e[num].en=en;
E[NUM].NEXT=P[ST];
p[st]=num++;
} struct ask {int id;
int l,r;
}A[NN];
int ANS[NN];
BOOL CMP (ask X,ask y) {return x.r<y.r;} int tree[nn];
inline int lowbit (int x) {return (x&-x),} void Jia (int id,int x) {for (int i=id;i<=n;i+=lowbit (i)) {
Tree[i]+=x;
}} int sum (int id) {int re=0;
while (ID) {Re+=tree[id];
Id-=lowbit (ID);
} return re;
} int main () {int i,u,v;
while (scanf ("%d%d", &n,&q)!=eof) {init ();
for (i=1;i<n;i++) {scanf ("%d%d", &u,&v);
Add (U,V);
Add (V,u); } for (i=1;i<=q;i++) {scanf ("%d%d", &A[I].L,&A[I].R);
A[i].id=i;
} sort (a+1,a+q+1,cmp);
memset (tree,0,sizeof (tree));
int ix=1,w;
for (i=1;i<=n;i++) {for (int j=p[i];j+1;j=e[j].next) {w=e[j].en;
if (w<i) {Jia (w,1); }} while (i==a[ix].r&&ix<=q) {Ans[a[ix].id]=a[ix].r-a[ix].
l+1-(SUM (A[IX].R)-sum (A[IX].L-1));
ix++;
}} for (i=1;i<=q;i++) {printf ("%d\n", Ans[i]);
}} return 0;
}