bzoj2809 [ APIO2012 ] -- 主席树

先求出dfs序,然后枚举管理者。

由于只要求数量最多,所以薪水一定从小到大取,用主席树维护,每次在主席树上二分就可以了。

具体看代码。

 

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<vector>
 6 using namespace std;
 7 #define N 100010
 8 #define ll long long
 9 inline char nc(){
10     static char buf[100000],*p1=buf,*p2=buf;
11     if(p1==p2){
12         p2=(p1=buf)+fread(buf,1,100000,stdin);
13         if(p1==p2)return EOF;
14     }
15     return *p1++;
16 }
17 inline void Read(int& x){
18     char c=nc();
19     for(;c<'0'||c>'9';c=nc());
20     for(x=0;c>='0'&&c<='9';x=(x<<3)+(x<<1)+c-48,c=nc());
21 }
22 inline void Read(ll& x){
23     char c=nc();
24     for(;c<'0'||c>'9';c=nc());
25     for(x=0;c>='0'&&c<='9';x=(x<<3)+(x<<1)+c-48,c=nc());
26 }
27 vector<int>g[N];
28 struct Node{
29     int l,r,s;
30     ll S; 
31 }c[N*55];
32 struct Ls{
33     ll w;
34     int f;
35 }A[N];
36 ll Ans,m,b[N];
37 int i,j,k,n,Rt[N],r[N],x,y,L[N],l[N],a[N],Cnt,Num,w[N],M;
38 inline ll Max(ll x,ll y){return x<y?y:x;}
39 inline void Dfs(int x){
40     a[++Cnt]=x;l[x]=Cnt;
41     for(int i=0;i<g[x].size();i++)Dfs(g[x][i]);
42     r[x]=Cnt;
43 }
44 inline void Update(int& x,int l,int r,int y,int z,int a){
45     x=++Num;
46     c[x]=c[y];c[x].s++;c[x].S+=a;
47     if(l==r)return;
48     int Mid=l+r>>1;
49     if(z<=Mid)Update(c[x].l,l,Mid,c[y].l,z,a);else Update(c[x].r,Mid+1,r,c[y].r,z,a);
50 }
51 inline int Query(int x,int y,int l,int r,ll s){
52     if(l==r)return c[y].S-c[x].S<=s?c[y].s-c[x].s:0;
53     int Mid=l+r>>1;ll S=s-c[c[y].l].S+c[c[x].l].S;
54     if(S>0)return c[c[y].l].s-c[c[x].l].s+Query(c[x].r,c[y].r,Mid+1,r,S);
55     return Query(c[x].l,c[y].l,l,Mid,s);
56 }
57 inline bool Cmp(Ls a,Ls b){return a.w<b.w;}
58 int main(){
59     Read(n);Read(m);
60     Read(x);Read(b[1]);Read(L[1]);A[1].w=b[1];A[1].f=1;
61     for(i=2;i<=n;i++){
62         Read(x);Read(b[i]);Read(L[i]);
63         g[x].push_back(i);A[i].w=b[i];A[i].f=i;
64     }
65     sort(A+1,A+n+1,Cmp);
66     for(i=1;i<=n;i++)w[A[i].f]=i;
67     Dfs(1);
68     for(i=1;i<=n;i++)Rt[i]=Rt[i-1],Update(Rt[i],1,n,Rt[i],w[a[i]],b[a[i]]);
69     for(i=1;i<=n;i++)Ans=Max(Ans,1ll*L[i]*Query(Rt[l[i]-1],Rt[r[i]],1,n,m));
70     cout<<Ans<<endl;
71     return 0;
72 }
bzoj2809

 

posted @ 2017-04-20 20:02  gjghfd  阅读(164)  评论(0编辑  收藏  举报