"Bzoj 2809" dispatching (Chairman tree)

Source: Internet
Author: User

This problem is very comfortable to do with the Chairman tree.

First of all, test instructions, it is necessary to find a point I in the tree structure, and find some points in the tree to form a set, so that the sum of C in the set is not more than M, and the number of elements in the li* set and the maximum

Simply think of the need to enumerate each point first, and then find the smallest k point in the subtree, so that Sigma (C[i]) (i = 1..K) does not exceed M, then l[i]*k is the optimal solution for this point

Then we should think that we can take advantage of the nature of the Chairman tree, first of all, the tree structure through the DFS sequence into a linear structure, and then can be judged by the records information in the subtree to obtain the answer (see Code)

Think about it. There are some points where it is not necessary to enumerate it. If the ancestor has greater l than it is, then this point is certainly not superior to its ancestors, so it is not necessary to enumerate it.

#include <cstdio> #include <cstring> #include <algorithm> #define REP (A,B,C) for (int a = B; a <= C; a + +) #define CLR (a) memset (A,0,sizeof (a)) #define CLR1 (a) memset (A,-1,sizeof (a)) #define LL long long#define mid int mid = (L + R ) >> 1#define N 100500using namespace std;struct edge{int to,next;};    struct node{node *ch[2];    LL W;    int siz;        void Init () {w = siz = 0;    Ch[0] = ch[1] = NULL;    } node () {init ();}        void Update () {siz = w = 0;        if (Ch[0]) {siz + = ch[0]->siz;w + = ch[0]->w;}    if (Ch[1]) {siz + = ch[1]->siz;w + = ch[1]->w;}    }}t[n * 20];int qcnt,size;int c[n],l[n],c2[n];struct segmenttree{node *root[n],*null;        void init () {null = &t[qcnt + +];        Null->init ();    Null->ch[0] = null->ch[1] = null;        } void __build (node *&y,node *&x,int l,int r,int t) {if (x = = null) x = NULL;        y = &t[qcnt + +];        Y->init ();         if (L = = r) {   *y = *x;            Y->w + = t;            Y->siz + +;        Return        } Mid;            if (t <= C2[mid]) {__build (y->ch[0],x->ch[0],l,mid,t);            Y-&GT;CH[1] = x->ch[1];        Y->update ();            } else{__build (y->ch[1],x->ch[1],mid+1,r,t);            Y->ch[0] = x->ch[0];        Y->update ();        }} void __find (node *x1,node *x2,int l,int r,int lim,int &ans) {if (x1 = = NULL) x1 = NULL;        if (x2 = = null) x2 = NULL;            if (L = = r) {ans + = min (lim/c2[l],x2->siz-x1->siz);        Return        } LL LW = 0;        int ls = 0;        LW + = X2->ch[0]->w;ls + x2->ch[0]->siz;        LW-= x1->ch[0]->w;ls-= x1->ch[0]->siz;        Mid;            if (Lim >= LW) {ans + = ls;        __find (X1->ch[1],x2->ch[1],mid + 1,r,lim-lw,ans); } else __find (X1->ch[0],x2->ch[0],l,mid,lim,ans);   } void Build (int id,int val) {__build (root[id],root[id-1],1,size,val);        } int Find (int x,int y,int lim) {int ans = 0;        __find (Root[x-1],root[y],1,size,lim,ans);    return ans; }}sgt;edge e[n << 1];int head[n],sz;int l[n],r[n],dfsc,rank[n];bool needs[n];int n,m;void addedge (int u,int v) {E    [sz].to = v;    E[sz].next = Head[u]; Head[u] = sz + +;}    void Init () {CLR (needs);    CLR1 (head); SZ = DFSC = qcnt = 0;}    void Dfs (int u,int Max) {L[u] = ++DFSC;    RANK[DFSC] = u;    if (Max < l[u]) needs[u] = 1;    int nmax = max (Max,l[u]);        for (int i = head[u]; i =-1; i = e[i].next) {int v = e[i].to;    DFS (V,nmax); } R[u] = DFSC;}    void Solve () {int B;    Init ();    Sgt.init ();        Rep (i,1,n) {scanf ("%d", &b);        Addedge (B,i);        scanf ("%d%d", &c[i],&l[i]);    C2[i] = C[i];    } dfs (0,-1);    Sort (C2 + 1,c2 + n + 1);    Size = Unique (C2 + 1,C2 + N + 1)-(C2 + 1); Rep (I,1,DFSC) Sgt.Build (I,c[rank[i]]);    LL ans =-1;        Rep (i,1,n) {if (!needs[i]) continue;    ans = max (ans, (LL) sgt.find (l[i],r[i],m) * l[i]); } printf ("%lld\n", ans);}    int main () {while (~scanf ("%d%d", &n,&m)) solve (); return 0;}


"Bzoj 2809" dispatching (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.