1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #include<bits/stdc++.h> #define I inline #define W while #define RI register int #define Cn const #define CI Cn int& #define gc getchar #define pc putchar #define LL long long using namespace std; I void read(int& x){RI f=1;char c=gc();x=0;W(!('0'<=c&&c<='9')) f=c^'-'?f:-1,c=gc();W('0'<=c&&c<='9') x=x*10+(c-'0'),c=gc();x*=f;} I void write(RI x){x<0&&(pc('-'),x=-x),x>=10&&(write(x/10),0),pc(x%10+'0');} Cn int N=1e6+10; int n,m,Ans[N]; struct Edge{int x,y;}e[N]; struct Que{int x,y,id;}q[N]; I bool cmp(Cn Edge& x,Cn Edge& y){return x.x>y.x;} I bool cmp2(Cn Que& x,Cn Que& y){return x.x>y.x;} class TreeArray{ private: int c[N]; #define lowbit(x) (x&(-x)) public: I void U(CI x,CI y){RI i;for(i=x;i<=n;i+=lowbit(i)) c[i]+=y;} I int Q(CI x){RI i,S=0;for(i=x;i;i-=lowbit(i)) S+=c[i];return S;} }T; int main(){ RI i,j,l,r;for(read(n),read(m),i=1;i<n;i++) read(l),read(r),e[i]=(Edge){min(min(l,r),i),max(max(l,r),i)}; for(i=1;i<=m;i++) read(q[i].x),read(q[i].y),q[i].id=i; for(j=1,sort(e+1,e+n,cmp),sort(q+1,q+m+1,cmp2),i=1;i<=m;i++){ W(j<n&&e[j].x>=q[i].x) T.U(e[j].y,1),j++; Ans[q[i].id]=q[i].y-q[i].x+1-T.Q(q[i].y); }for(i=1;i<=m;i++) write(Ans[i]),pc('\n'); return 0; }
|