bzoj_4756_[usaco2017 jan]promotion counting_ tree-like array

Source: Internet
Author: User

bzoj_4756_[usaco2017 jan]promotion counting_ tree-like array

Descriptionn cows make up a tree-shaped company, each cow has a capacity value pi,1 cow as the root.
Q. For each cow, its subtree has several abilities that are larger than it. Inputn, which means there are several cows n<=100000
Next n behavior 1-n number of cows ability value PI
Next n-1 the manager of the 2-n Cow (father in the tree)
Outputa total of n rows, the output of each row of cows I have several capacity value than I largeSample Input5
804289384
846930887
681692778
714636916
957747794
1
1
2
3Sample Output2
0
1
0
0 the idea of the problem is quite ingenious. we have the opportunity to process two times per point for DFS on the entire tree. One is just traversed, and one is the backtracking of the subtree. The number of points where the two-time capability value is greater than the point is calculated separately, and the second minus the first is the answer to this point. then discrete with a tree-like array. Code:
#include <stdio.h> #include <string.h> #include <algorithm>using namespace std; #define N 100050struct A {int num,id,v;} A[n];bool CMP1 (const a &x,const a &y) {return x.num>y.num;} BOOL CMP2 (const a &x,const a &y) {return x.id<y.id;} int n,head[n],to[n<<1],nxt[n<<1],cnt,ans[n],c[n];inline void Add (int u,int v) {To[++cnt]=v;nxt[cnt]=head [U];head[u]=cnt;} void fix (int x,int v) {for (; x<=n;x+=x&-x) c[x]+=v;}    int inq (int x) {int re=0;    for (; x;x-=x&-x) re+=c[x]; return re;}    void Dfs (int x,int y) {int i;    Fix (a[x].v,1);    int tmp=inq (A[X].V-1);        for (I=head[x];i;i=nxt[i]) {if (to[i]!=y) {DFS (to[i],x); }} ans[x]=inq (A[x].v-1)-tmp;}    int main () {//freopen ("tt.in", "R", stdin);    scanf ("%d", &n);    int i,x,j;    for (i=1;i<=n;i++) scanf ("%d", &a[i].num), a[i].id=i; Sort (A+1,A+N+1,CMP1);    a[0].num=-245345; for (i=1,j=0;i<=n;i++) {if (A[i].num!=a[i-1].num) j + +; a[i].v=j;    } sort (A+1,A+N+1,CMP2);        for (i=2;i<=n;i++) {scanf ("%d", &x); Add (i,x);    Add (x,i);    } dfs (1,0); for (i=1;i<=n;i++) printf ("%d\n", Ans[i]);}

bzoj_4756_[usaco2017 jan]promotion counting_ 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.