"Tree-shaped array" Gym-101147j-whistle ' s New Car

Source: Internet
Author: User

Test instructions is for each point I, statistics in its subtree (excluding itself), and the number of points depj-depi<=xj.

Sort the points separately according to Dep-x and DEP, take them offline,

Inserting a point of dep-x less than or equal to the current DEP value into a tree array becomes an inquiry into how many points in the DFS sequence are within an interval and can be easily resolved with a tree array.

#include <cstdio> #include <algorithm> #include <cstring>using namespace std;int f,c;void R (int &x    ) {c=0;f=1; for (; c< ' 0 ' | | C> ' 9 ';    C=getchar ()) if (c== '-') f=-1; for (x=0; c>= ' 0 ' &&c<= ' 9 ';    C=getchar ()) (x*=10) + = (c ' 0 '); X*=f;} typedef long long ll; #define N 500010int T,n,a[n];int v[n<<1],next[n<<1],w[n<<1],first[n],e;void Addedge (int u,int v,int W) {v[++e]=v;w[e]=w;next[e]=first[u];first[u]=e;} ll Dep[n];int ls[n],rs[n],tot;bool vis[n];void dfs (int U) {vis[u]=1;      ls[u]=++tot;for (int i=first[u];i;i=next[i]) if (!vis[v[i]]) {dep[v[i]]=dep[u]+ (LL) w[i];    DFS (V[i]); }rs[u]=tot;} struct POINT//{//LL v;//int p;//}t[n<<1];//bool cmp (const point &a,const point &b)//{//return a.v< B.v;//}struct poin2{ll v;int p;} All[n<<1];bool cm2 (const Poin2 &a,const Poin2 &b) {return A.V&LT;B.V;} int anss[n];int d[n];void Update (int p) {for (;p <=n;p+= p& (-P)) ++d[p];} int query (int p) {int res=0; for (;p; p-= (P&(-P))) res+=d[p]; return res;} int main () {freopen ("car.in", "R", stdin);//freopen ("Car.out", "w", stdout); int x, y, Z; R (T); for (;  T;--T) {e=tot=0;  memset (v,0,sizeof (v));  Memset (W,0,sizeof (w));  memset (Next,0,sizeof (next));  Memset (first,0,sizeof (first));  memset (vis,0,sizeof (VIS));  memset (dep,0,sizeof (DEP));  Memset (d,0,sizeof (d));  R (n);  for (int i=1;i<=n;++i) R (A[i]); for (int i=1;i<n;++i) {R (x); R (y);    R (z);    Addedge (x, y, z);    Addedge (Y,X,Z); } dfs (1);//for (int i=1;i<=n;++i)//{//t[i].v=dep[i]-(LL) a[i];//t[i].p=i;//}//for (int i=n+1;i<=n* 2;++i)//{//t[i].v=dep[i-n];//t[i].p=i;//}//sort (t+1,t+n*2+1,cmp);//int zy=1;//all[t[1].p].v=zy;//A ll[t[1].p].p= (t[1].p>n t[1].p-n: T[1].P);//for (int i=2;i<=n*2;++i)//{//if (T[I].V!=T[I-1].V)//++zy ;//all[t[i].p].v=zy;//all[t[i].p].p= (t[i].p>n t[i].p-n: T[I].P);/} for (int i=1;i<=n;++i) {all[    i].v=dep[i]-(LL) a[i];    All[i].p=i;  }for (int i=n+1;i<=n*2;++i) {all[i].v=dep[i-n];    All[i].p=i-n;  } sort (all+1,all+n+1,cm2);  Sort (all+n+1,all+n*2+1,cm2);  int j=1;      for (int i=n+1;i<=n*2;++i) {while (all[j].v<=all[i].v && j<=n) {update (LS[ALL[J].P]);      ++j;    } anss[all[i].p]=query (RS[ALL[I].P])-query (LS[ALL[I].P]);  } for (int i=1;i<n;++i) printf ("%d", anss[i]);  printf ("%d\n", Anss[n]); }return 0;}

"Tree array" Gym-101147j-whistle ' s New Car

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.