hdu5029 树链剖分(技巧)

  1 #include
  2 #include<string.h>
  3 #include
  4 #include
  5 using namespace std;
  6 vector<int>col[100005];
  7 int cnt,now;
  8 int maxv[400010],ans[100005],maxx[400005];
  9 int next[200005],head[200005],point[200005];
 10 int num[100005],deep[100005],son[100005],father[100005];
 11 int top[100005],tree[100005],pre[100005];
 12 void add(int x,int y)
 13 {
 14     next[++now]=head[x];
 15     head[x]=now;
 16     point[now]=y;
 17 }
 18 void dfs1(int u)
 19 {
 20     num[u]=1;
 21     for (int i=head[u];i!=0;i=next[i])
 22     {
 23         int v=point[i];
 24         if (father[u]==v) continue;
 25         father[v]=u; deep[v]=deep[u]+1;
 26         dfs1(v);
 27         num[u]+=num[v];
 28         if (son[u]==-1||num[v]>num[son[u]]) son[u]=v;
 29     }
 30 }
 31 void dfs2(int u,int lead)
 32 {
 33     top[u]=lead;
 34     tree[u]=++cnt;
 35     pre[cnt]=u;
 36     if (son[u]==-1) return;
 37     dfs2(son[u],lead);
 38     for (int i=head[u];i!=0;i=next[i])
 39     {
 40         int v=point[i];
 41         if (son[u]!=v&&father[u]!=v) dfs2(v,v);
 42     }
 43 }
 44 void change(int l,int r,int d)
 45 {
 46     int temp;
 47     while (top[l]!=top[r])
 48     {
 49         if (deep[top[l]]temp;  }
 50         col[tree[top[l]]].push_back(d);
 51         col[tree[l]+1].push_back(-d);
 52         l=father[top[l]];
 53     }
 54     if (deep[l]>deep[r]) {temp=l; l=r; r=temp; }
 55     col[tree[l]].push_back(d);
 56     col[tree[r]+1].push_back(-d);
 57 }
 58 void update(int o,int l,int r,int p,int v)
 59 {
 60     int mid=(l+r)/2;
 61     if (l==r) {maxv[o]+=v;  maxx[o]=p; }
 62     else{
 63         if (p<=mid) update(o*2,l,mid,p,v);
 64         else update(o*2+1,mid+1,r,p,v);
 65         if (maxv[o*2]>=maxv[o*2+1]) { maxv[o]=maxv[o*2]; maxx[o]=maxx[o*2];}
 66         else{ maxv[o]=maxv[o*2+1]; maxx[o]=maxx[o*2+1];  }
 67     }
 68 }
 69 int main()
 70 {
 71     int n,m,i,j,x,y,d;
 72     while (~scanf("%d%d",&n,&m)&&n)
 73     {
 74         memset(head,0,sizeof(head));
 75         memset(son,-1,sizeof(son));
 76         memset(maxv,0,sizeof(maxv));
 77         memset(maxx,0,sizeof(maxx));
 78         for (i=0;i<=n;i++) col[i].clear();
 79         now=cnt=0;
 80         father[1]=1;  deep[1]=0;
 81         for (i=1;i)
 82         {
 83             scanf("%d%d",&x,&y);
 84             add(x,y); add(y,x);
 85         }
 86         dfs1(1); dfs2(1,1);
 87         int dd=1;
 88         for (i=1;i<=m;i++)
 89         {
 90             scanf("%d%d%d",&x,&y,&d);
 91             change(x,y,d);
 92             if (d>dd) dd=d;
 93         }
 94         for (i=1;i<=n;i++)
 95         {
 96             for (j=0;j)
 97                 if (col[i][j]<0) update(1,1,dd,-col[i][j],-1);
 98                 else update(1,1,dd,col[i][j],1);
 99             ans[pre[i]]=maxx[1];
100         }
101         for (i=1;i<=n;i++) printf("%d\n",ans[i]);
102     }
103     return 0;
104 }

http://acm.hdu.edu.cn/showproblem.php?pid=5029

转载于:https://www.cnblogs.com/xiao-xin/articles/4029730.html

你可能感兴趣的:(hdu5029 树链剖分(技巧))