Fzoj 2245 Dynamic Tree (discrete + offline + tree-like Array)

Source: Internet
Author: User

problem 2245 Dynamic treeAccept:17 submit:82
Time limit:3000 mSec Memory limit:65536 KB problem Description

Yellowstar has a magical dynamic tree, the tree is composed of n-weighted nodes, n-1, any two nodes can reach each other, labeled I node of the right value of Wi.

As the material is moving, the tree will change every day, and on day i, the tree weights will emit a strong glow in [l,r] nodes, Yellowstar see this phenomenon to be very pleasant, and its pleasant value depends on: How many connected subtrees the glowing nodes form.

Now that you know the glow of the dynamic tree over the next m days, please calculate the Yellowstar per day.

Input

The first line inputs t, which indicates a T-group sample (t <= 20)

Sample first action n for each group, indicating the number of nodes in the tree (1 <= n <= 1e5)

Next n digits W1, W2, ..., Wn (1 <= Wi <= 1e9) indicates the weight of each node

Next n-1 line, two digits per line u, v (1 <= u, v <= n) indicates tree edge

The next number M (1 <= m <= 1e5) represents M-Day

Next M inquiry, each one row l, R (1 <= l <= R <= 1e9) indicates the weight range of the glowing nodes each day

Output m rows per set of sample outputs, representing Yellowstar in this M-Day pleasure value sample Input131 3 331 3 Sample Output112 Hint

Long long type please use%i64d output

Sourcefoj Award-April 2017 (school Match)"analysis" gives you a tree, each node has a weight ([1,1e9]), and then Q times, each given an interval [l,r], ask the right value in this interval of the nodes constitute the number of Unicom Block. at first think of IS and check set + Mo team, later found and check set over the bad Deal. Listen to seniors, first of all the ownership values are discrete, the interval offline, according to the right end point from small to large sort. and then, for each edge, it's considered an interval.sort like the front face. then, for a query interval, how many points in this interval we can preprocess out, assuming that the s, that is, when there is no edge of the Unicom block is s, if there are two points connected with a side,so the Unicom block-1, how many sides, s on the reduction of how Much. The question now turns into how many sides there are in the query Interval. Since we are ordered, we fixed the right endpoint of the query, for the right endpoint <= queries the edge of the right endpoint, with a tree-like arraycount the number of edges on the right end of the <=.
#include <cstdio>#include<vector>#include<cstring>#include<string>#include<cstdlib>#include<iostream>#include<map>#include<cmath>#include<algorithm>using namespaceStd;typedefLong LongLl;typedef pair<int,int>pii;Const intN = 1e5+5;Const DoubleEPS = 1e-8;intt,n,w[n],sum[n<<2],p[n<<2],cnt,m,ret[n],c[n<<2];structquery{intl,r,id; BOOL operator< (ConstQuery &rhs)Const{      returnr<rhs.r; }}q[n],t[n];voidAddintx) {    for(; X<=cnt;x+=x&-x) + +c[x];}intAskintx) {   intRET =0;  for(;x>0; X-=x&-x) ret+=c[x]; returnret;}intmain () {scanf ("%d",&T);  while(t--) {scanf ("%d",&n); CNT=0;  for(intI=1; i<=n;++I) {SCANF ("%d",&w[i]); p[++cnt]=w[i]; }       for(intI=1; i<n;++I) {SCANF ("%d%d",&t[i].l,&t[i].r); T[I].L=w[t[i].l]; T[I].R=w[t[i].r]; } scanf ("%d",&m);  for(intI=1; i<=m;++I) {SCANF ("%d%d",&q[i].l,&q[i].r); p[++cnt]=q[i].l; p[++cnt]=q[i].r; Q[i].id=i; } Sort (p+1, p+1+cnt); CNT=unique (p+1, p+1+cnt)-p-1;  for(intI=0; I<=cnt;++i) c[i]=sum[i]=0;  for(intI=1; i<=n;++I) {w[i]=lower_bound (p+1, p+1+cnt,w[i])-p; ++sum[w[i]]; }      for(intI=1; I<=cnt;++i) sum[i]+=sum[i-1];  for(intI=1; i<n;++I) {T[I].L=lower_bound (p+1, p+1+cnt,t[i].l)-p; T[I].R=lower_bound (p+1, p+1+cnt,t[i].r)-p; if(t[i].l>T[i].r) Swap (t[i].l,t[i].r); }      for(intI=1; i<=m;++I) {Q[I].L=lower_bound (p+1, p+1+cnt,q[i].l)-p; Q[I].R=lower_bound (p+1, p+1+cnt,q[i].r)-p; }     if(n!=1) Sort (t+1, t+1+n-1); Sort (q+1, q+1+m); intj=1;  for(intI=1; i<=m;++I) {ret[q[i].id]=sum[q[i].r]-sum[q[i].l-1];  for(; j<=n-1&&t[j].r<=q[i].r;++j) Add (t[j].l); ret[q[i].id]-=ask (q[i].r)-ask (q[i].l-1); }      for(intI=1; I<=m;++i) printf ("%d\n", ret[i]); }   return 0;}

Fzoj 2245 Dynamic Tree (discrete + offline + tree-like Array)

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.