Hihocoder 1145: The Everyday Fantasy Village

Source: Internet
Author: User

#1145: Daily Time limits for Gensokyo:20000msSingle Point time limit:1000msMemory Limit:256MBDescribe

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 11 22) 31 3
Sample output
1
/** Test instructions: In a go to see, how many sub-range; procedure: Tree-like array + discretization **/#include<iostream>#include<string.h>#include<stdio.h>#include<cmath>#include<algorithm>#include<queue>#defineMAXN 110000using namespacestd;intSUM[MAXN];intMMAP[MAXN];intn,m;intnum;intHEAD[MAXN];structline{intl; intR; intID;} LINE[MAXN];intCMP (line A,line b) {returnA.R <B.R;}structnode{intVal; intNext;} NODE[MAXN*2];voidinit () {num=0; memset (Head,-1,sizeof(head));}voidAddedge (intUintv) {Node[num].val=v; Node[num].next=Head[u]; Head[u]= num++;}intLowbit (intx) {    returnx& (-x);}voidAddintUintv) {     for(intI=u; i<=n; i + =lowbit (i)) {Sum[i]+=v; }}intGetsum (intu) {    intSumweight =0;  while(u) {sumweight+=Sum[u]; U-=lowbit (U); }    returnsumweight;}intMain () {//#ifndef Online_judge//freopen ("In.txt", "R", stdin);//#endif//Online_judge     while(~SCANF ("%d%d",&n,&m)) {intu,v;        Init ();  for(intI=1; i<n; i++) {scanf ("%d%d",&u,&v);            Addedge (U,V);        Addedge (V,u); }         for(intI=1; i<=m; i++) {scanf ("%d%d",&line[i].l,&LINE[I].R); Line[i].id=i; }        intIX =1, ans =0; memset (Mmap,0,sizeof(mmap)); Sort ( Line+1, line+m+1, CMP); memset (SUM,0,sizeof(sum));  for(intI=1; i<=n; i++)        {             for(intJ=head[i]; j+1; j=Node[j].next) {                intW =Node[j].val; if(W <i) {Add (W,1); }            }             while(i = = LINE[IX].R && IX <=m) {Mmap[line[ix].id]= Line[ix].r-line[ix].l +1-(Getsum (LINE[IX].R)-Getsum (LINE[IX].L-1)); IX++; }        }         for(intI=1; i<=m; i++) {printf ("%d\n", Mmap[i]); }    }    return 0;}

Hihocoder 1145: The Everyday Fantasy Village

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.