Bzoj 3011: [Usaco2012 dec]running away from the Barn (Dfs sequence + chairman tree)

Source: Internet
Author: User

Subtree operation, DFS sequence can be. Then calculate the <=l directly on the persisted segment tree query

-------------------------------------------------------------------

#include <bits/stdc++.h>using namespace std;#define M (L, R) (((L) + (R)) >> 1)const int MAXN = 200009;typedef long Long ll;inline ll Readll () {char C = getchar ();For (;!isdigit (c); c = GetChar ());ll ans = 0;For (; IsDigit (c); c = GetChar ())ans = ans * + C-' 0 ';return ans;}inline int readint () {char C = getchar ();For (;!isdigit (c); c = GetChar ());int ans = 0;For (; IsDigit (c); c = GetChar ())ans = ans * + C-' 0 ';return ans;}struct HASH {ll ID[MAXN];int N;HASH () {N = 0;}void Add (ll v) {id[n++] = v;}void Work () {sort (ID, id + N);N = unique (ID, ID + N)-id;Id[n] = 0x7fffffffffffffff;}int hash (ll v) {return Lower_bound (ID, id + N, v)-id;}int_hash (ll v) {return Upper_bound (ID, id + N, v)-id;}} h;struct Node {Node *l, *r;int s;} POOL[MAXN], *pt = Pool, *null, *ROOT[MAXN]; void Set_null () {null = pt++;null->l = Null->r = null;null->s = 0;}int p;node* Modify (node* t, int l, int r) {node* h = pt++;h->s = t->s + 1;if (R > L) {int m = m (l, R);if (p <= m) {h->l = Modify (T->l, L, m);h->r = t->r;} else {h->l = t->l;h->r = Modify (T->r, M + 1, R);}}return h;} int query (int __l, int __r, int v) {Node *_l = __l? ROOT[__L-1]: null, *_r = Root[__r];int L = 1, R = h.n, ans = 0;While (L < R) {int m = m (L, R);if (R <= v) {ans +=_r->s-_l->s;Break ;}if (M <= v) {ans + = _r->l->s-_l->l->s;_l = _l->r; _r = _r->r; L = m + 1;} else {_l = _l->l; _r = _r->l; R = m;}}return ans;}struct Edge {int to;ll Dist;Edge (int _t, LL _d): to (_t), dist (_d) {}};ll SEQ[MAXN], L;int _L[MAXN], _r[maxn], cur =-1, N;vector<edge> G[MAXN];void Dfs (int x, ll d, int fa) {seq[_l[x] = ++cur] = D;For (vector<edge>::iterator e = G[x].begin (); E! = G[x].end (); e++)if (e->to! = FA) Dfs (e->to, D + e->dist, x);_r[x] = cur;}void init () {N = Readint (); L = Readll ();for (int i = 1; i < N; i++) {int p = readint ()-1; ll d = Readll ();G[p].push_back (Edge (i, D));G[i].push_back (Edge (P, D));}set_null ();}void Work () {DFS (0, 0,-1);for (int i = 0; i < N; i++)H.add (Seq[i]);h.work ();node* FA = null;for (int i = 0; i < N; i++) {p = h.hash (Seq[i]) + 1;FA = Root[i] = Modify (FA, 1, H.N);}for (int i = 0; i < N; i++)printf ("%d\n", Query (_l[i], _r[i], H._hash (L + seq[_l[i] )));}int main () {init ();Work ();return 0;}

-------------------------------------------------------------------

3011: [Usaco2012 dec]running away from the Barn time limit: ten Sec Memory Limit: MB
Submit: 136 Solved: 74
[Submit] [Status] [Discuss] Description

It ' s milking time at Farmer John's farm, but the cows has all run away! Farmer John needs to round them all up, and needs your The search. FJ ' s farm is a series of n (1 <= n <= 200,000) pastures numbered 1...N connected by N-1 bidirectional paths. The barn is located at Pasture 1, and it's possible to reach all pasture from the barn. FJ ' s cows were in their pastures this morning, but who knows where they ran to by now. FJ does know that the cows only run away from the barn, and they is too lazy to run a distance of more than L. For every pasture, FJ wants to know what many different pastures cows starting in that pasture could has ended up in. Note:64-bit integers (Int64 in Pascal, a long long in C + + and long in Java) is needed to store the distance values.

Give a tree with a root at point 1th, and ask how many points in the subtree of each point are less than L.

Input

* Line 1:2 integers, N and L (1 <= N <= 200,000, 1 <= L <= 10^18)

* Lines 2..n:the ith line contains II integers p_i and l_i. P_i (1 <= p_i < i) is the first pasture on the shortes T path between pasture I and the barn, and l_i (1 <= l_i <= 10^12) is the length of this path.

Output

* Lines 1..n:one number per line, the number in line I was the number pastures that can was reached from pasture I by takin G roads that leads strictly farther away from the barn (pasture 1) whose total length does not exceed L.

Sample Input4 5
1 4
2 3
1 5
Sample Output3
2
1
1
OUTPUT details:cows from Pasture 1 can hide at pastures 1, 2, and 4. Cows from Pasture 2 can hide at pastures 2 and 3. Pasture 3 and 4 is as far from the barn as possible, and the cows can hide there. HINT

Source

Bzoj 3011: [Usaco2012 dec]running away from the Barn (Dfs sequence + chairman tree)

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.