bzoj3011: [Usaco2012 dec]running away from the Barn can be stacked (left-leaning tree)

Source: Internet
Author: User

Test instructions: Give a tree with a root of 1th points, and ask how many points in the subtree of each point are less than L. N<=2e5,l<=1e18

Readily smoked a, thought is a water problem, brush water does not reverse be brushed.
At first, the problem is not difficult, then the sample old, and then look at the zyf2000 code. Or wrong, finally directly to her hand, the result is wrong. Rage Erase rewrite:
The beginning of the thinking is the multiplication of what, but it seems not good dynamic maintenance, and the complexity of the great.
Then this can only be and heap, the heap maintenance of each subtree within the distance of not more than L point, from the bottom to go up, walk through a sub-tree to merge a wave, and then dynamically put the distance over the deletion of the L can be.
Long time did not hit the left side tree, is very ugly.
It is important to note that D[i] is the dist,dis[i of the left-leaning tree, and the distance from I to the root is exactly the same as the left-leaning tree.
Record Lazy[x] to pass the weight value. F[x] represents the top of the heap where x resides.

#include <cstdio> #include <algorithm> #include <cstring> #define FO (i,a,b) for (int i=a;i<=b;i++) #
Define FD (I,A,B) for (int i=a;i>=b;i--) using namespace std;
const int n=4e5+5;
typedef long Long LL;
int head[n],next[n],go[n],d[n];
int ans[n],n;
ll Dis[n],lazy[n],val[n],l;
int Ls[n],rs[n],f[n],size[n],tot;
    inline void Add (int x,int y,ll z) {go[++tot]=y;
    Val[tot]=z;
    NEXT[TOT]=HEAD[X];
Head[x]=tot;
    } inline void solve (int x,ll v) {if (!x) return;
    Dis[x]+=v;
Lazy[x]+=v; } inline void pushup (int x) {size[x]=size[ls[x]]+size[rs[x]]+1;} inline void pushdown (int x) {if (lazy[x]) solve
(Ls[x],lazy[x]), solve (Rs[x],lazy[x]), lazy[x]=0;
    } inline int merge (int x,int y) {if (!x) return y;
    if (!y) return x;
    if (Dis[x]<dis[y]) swap (x, y);
    Pushdown (x);
    Rs[x]=merge (Rs[x],y);
    if (D[ls[x]]<d[rs[x]]) swap (ls[x],rs[x]);
    d[x]=d[rs[x]]+1;
    Pushup (x);
return x;
    } inline void Pop (int x) {pushdown (f[x]); F[x]=merge (Ls[f[x]],rs[f[x]);
    } inline void Dfs (int x,int fa) {f[x]=x,d[x]=size[x]=1;
        for (int i=head[x];i;i=next[i]) {int v=go[i];
            if (V!=FA) {DFS (v,x);
            Solve (f[v],val[i]);
            while (dis[f[v]]>l) pop (v);
        F[x]=merge (F[x],f[v]);
}} Ans[x]=size[f[x]];
    } int main () {scanf ("%d%lld", &n,&l);
        Fo (i,2,n) {int x;
        ll Z;
        scanf ("%d%lld", &x,&z);
        Add (x,i,z);
    Add (i,x,z);
    } dfs (1,0);
Fo (i,1,n) printf ("%d\n", Ans[i]);
 }

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.