Rokua P2982 [Usaco10feb] slow down slowing down (segment tree DFS sequence interval increment/decrement single point query)

Source: Internet
Author: User

To Los Valleys. 2982 slow down slowing down

Title Description

Every day Farmer John's n (1 <= N <= 100,000) cows conveniently numbered 1..N move from the barn to her priv Ate pasture. The pastures is organized as a tree, with the barn being on pasture 1. Exactly N-1 cow unidirectional paths connect the pastures; Directly connected pastures has exactly one path. Path I connects Pastures a_i and b_i (1 <= a_i <= N; 1 <= b_i <= N).

Cow I has a private pasture p_i (1 <= p_i <= N). The barn ' s small door lets only one cow exit at a time; And the patient cows wait until their predecessor arrives at her private pasture. First Cow 1 exits and moves to pasture p_1. Then Cow 2 exits and goes to pasture p_2, and so on.

While cow I walks to p_i she might or might don't pass through a pasture that already contains an eating cow. When a cow was present in a pasture, cow I walks slower than usual to prevent annoying her friend.

Consider the following pasture network, where the number betweenparentheses indicates the pastures' Owner.               1 (3)/(1) 4 3 (5)/\ (2) 2 5 (4) First, cow 1 walks to her pasture:1 (3) /[1] 3 (5)/\ (2) 2 5 (4) When Cow 2 moves to her pasture, she first passes into the barn ' spasture, Pastur E1.Then she sneaks around cow1In pasture4 beforearriving at her own pasture.1 (3)/[1]The3 (5)/\ [2]*5 (4) Cow3 doesn' t get far @ All--she lounges in the barn ' s pasture,#1.1* [3]/[1]The3 (5)/\ [2]*5 (4) Cow4 must slowFor pasture1and4On herTo Pasture5:1* [3]/[1]4* 3 (5)/\ [ 2] 2* 5* [ 4] Cow 5 slows for Cow 3 in pasture 1 and  Then enters her own private pasture: 1* [ 3]/[1] 4* 3*[5]/\ [2] 2* 5* [4]      

FJ would like to know what many times each cow have to slow down.

Farmer John's N-head cows every day (1 <= n <= 100000, number 1 ... N) went from the Granary to his own pasture. The pasture forms a tree, and the barn is on ranch 1th. There is a N-1 road directly connected to the pasture, so that there is a path between the pastures connected. The first road is connected to A_i,b_i, (1 <= a_i <= N; 1 <= b_i <= N). The cows each have a private ranch p_i (1 <= p_i <= N). The barn door can only let one cow leave at a time. The impatient cows will wait until their front friends arrive at their private ranch before leaving. First the cow 1 leaves, goes to p_1, then the Cow 2, and so on.

When cow I goes to pasture p_i, he may pass by a companion who is eating grass. When passing through a ranch that already has cows, cow I slows down its speed and prevents him from disturbing his friends.

FJ wanted to know how many times the cows slowed down in total.

Input/output format

Input format:

    • Line 1:line 1 contains a single integer:n

    • Lines 2..n:line i+1 contains, space-separated integers:a_i and B_i

    • Lines n+1..n+n:line N+i contains a single integer:p_i

Output format:

    • Lines 1..n:line I contains the number of times cow I have to slow down.

Input and Output Sample input example # #:
Sample # # of output:
Code:
1#include <cstdio>2#include <cctype>3 using namespacestd;4 Const intn=100005;5 6 intn,enum,cnt,h[n<<1],sum[n<<2],dfn[n],size[n];7 structEdge8 {9     intto,nxt;Ten}e[n<<1]; One  A voidReadint&Now ) - { -now=0;CharC=GetChar (); the      for(;! IsDigit (c); c=GetChar ()); -      for(; IsDigit (c); c=GetChar ()) -Now= (now<<3) + (now<<1) +c-'0'; - } +  - voidAddedge (intUintv) + { AE[++enum].to =v; atE[ENUM].NXT =H[u]; -H[u] =ENum; - } -  - voidDFS (intx) - { insize[x]=1;//The size of the root subtree (including itself) -dfn[x]=++cnt;//DFN[I]:I subscript in the DFS sequence to      for(intI=h[x];i;i=e[i].nxt) +     { -         intto=e[i].to; the         if(Dfn[to])Continue; * DFS (to); $size[x]+=Size[to];Panax Notoginseng     } - } the  + voidPushup (intRT) A { thesum[rt]=sum[rt<<1]+sum[rt<<1|1]; + } - voidPushdown (intRT) $ { $     if(! SUM[RT])return; -sum[rt<<1]+=Sum[rt]; -sum[rt<<1|1]+=Sum[rt]; thesum[rt]=0; - }Wuyi voidModifysum (intLintRintRtintLintR) the { -     if(L<=l && r<=R) Wu     { -++SUM[RT];//interval modification for lazy tag +1 About         return; $     } - pushdown (RT); -     intM= (l+r) >>1; -     if(l<=m) Modifysum (l,m,rt<<1, l,r); A     if(M<r) Modifysum (m+1,r,rt<<1|1, l,r); +     //pushup (RT); no pushup!! required the } - intQuerysum (intLintRintRtintp) $ { the     if(L==R)returnSum[rt]; the pushdown (RT); the     intM= (l+r) >>1, res=0; the     if(p<=m) Res+=querysum (l,m,rt<<1, p); -     ElseRes+=querysum (m+1,r,rt<<1|1, p); in     returnRes; the } the  About intMain () the { the read (n); the      for(intI=1; i<n;i++) +     { -         intb; the read (a); Read (b);Bayi Addedge (A, b); the Addedge (b,a); the     } -DFS (1); - //for (int i=1;i<=n;i++) the //printf ("%d:dfn:%d size:%d\n", I,dfn[i],size[i]); the      for(intI=1; i<=n;i++) the     { the         intA; - read (a); theprintf"%d\n", Querysum (1N1, Dfn[a]));//Go through and then change theModifysum (1N1, dfn[a],dfn[a]+size[a]-1);//single-point query interval modification the     }94     return 0; the}

Rokua P2982 [Usaco10feb] slow down slowing down (segment tree DFS sequence interval increment/decrement single point query)

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.