hdu 5044 树链剖分(点更新、边更新的更优美姿势才能过)

171 篇文章 0 订阅
99 篇文章 0 订阅


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

Tree

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2673    Accepted Submission(s): 467


Problem Description
You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are numbered from 1 to N

There are N - 1 edges numbered from 1 to N - 1.

Each node has a value and each edge has a value. The initial value is 0.

There are two kind of operation as follows:

● ADD1 u v k: for nodes on the path from u to v, the value of these nodes increase by k.

● ADD2 u v k: for edges on the path from u to v, the value of these edges increase by k.

After finished M operation on the tree, please output the value of each node and edge.
 

Input
The first line of the input is T (1 ≤ T ≤ 20), which stands for the number of test cases you need to solve.

The first line of each case contains two integers N ,M (1 ≤ N, M ≤10 5),denoting the number of nodes and operations, respectively.

The next N - 1 lines, each lines contains two integers u, v(1 ≤ u, v ≤ N ), denote there is an edge between u,v and its initial value is 0.

For the next M line, contain instructions “ADD1 u v k” or “ADD2 u v k”. (1 ≤ u, v ≤ N, -10 5 ≤ k ≤ 10 5)
 

Output
For each test case, print a line “Case #t:”(without quotes, t means the index of the test case) at the beginning.

The second line contains N integer which means the value of each node.

The third line contains N - 1 integer which means the value of each edge according to the input order.
 

Sample Input
  
  
2 4 2 1 2 2 3 2 4 ADD1 1 4 1 ADD2 3 4 2 4 2 1 2 2 3 1 4 ADD1 1 4 5 ADD2 3 2 4
 

Sample Output
  
  
Case #1: 1 1 0 1 0 2 2 Case #2: 5 0 0 5 0 4 0
 

Source
 


题意:给出一棵树,有两种操作:1 给路径上的所有点加vi,2所有边加vi。最后输出所有点的权值和所有边的权值。

编程思想:要换另一种姿势的树链剖分才能过。。

这种姿势的代码参考自:http://blog.csdn.net/qinzhenhua100/article/details/39716851


AC code:

#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
#define N 100010
struct pp
{
	int u,v;
}ed[N];
struct node
{
	int u,v,next;
}bian[N*2];
int e,id,dep[N],son[N],father[N],sz[N],ti[N],mark1[N],mark2[N],top[N],head[N];
__int64 a[N],b[N],ans1[N],ans2[N];
void add(int u,int v)
{
	bian[e].u=u;
	bian[e].v=v;
	bian[e].next=head[u];
	head[u]=e++;
}
void dfs1(int u,int fa)
{
	int i,v;
     dep[u]=dep[fa]+1; son[u]=0;   father[u]=fa; sz[u]=1; 
	 for(i=head[u];i!=-1;i=bian[i].next)
	 {
		 v=bian[i].v;
		 if(v==fa) continue;
		 dfs1(v,u);
		 sz[u]+=sz[v];
		 if(sz[son[u]]<sz[v])
			 son[u]=v;
	 }
}
void dfs2(int u,int fa)
{
	int i,v;
	ti[u]=id++;
	mark1[id-1]=u;
	top[u]=fa;
	if(son[u]!=0)
		dfs2(son[u],fa);
	for(i=head[u];i!=-1;i=bian[i].next)
	{
		v=bian[i].v;
		if(v==father[u]||v==son[u])
			continue;
		dfs2(v,v);
	}
}
void getnode(int u,int v,int k)
{
	while(top[u]!=top[v])
	{
		if(dep[top[u]]>dep[top[v]])
			swap(u,v);
		a[ti[top[v]]]+=k;
		a[ti[v]+1]-=k;
		v=father[top[v]];
	}
	if(ti[u]>ti[v])
		swap(u,v);
	a[ti[u]]+=k;
	a[ti[v]+1]-=k;
}
void getedge(int u,int v,int k)
{
	while(top[u]!=top[v])
	{
		if(dep[top[u]]>dep[top[v]])
			swap(u,v);
		b[ti[top[v]]]+=k;
		b[ti[v]+1]-=k;
		v=father[top[v]];
	}
	if(ti[u]>ti[v])
		swap(u,v);
	if(u!=v)
	{
		b[ti[u]+1]+=k;
		b[ti[v]+1]-=k;
	}
}
int main()
{
	int t,cnt=1,n,m,i,u,v,k;
	__int64 s;
	char str[10];
	scanf("%d",&t);
	while(t--)
	{
      scanf("%d%d",&n,&m);
	  memset(a,0,sizeof(a));
	  memset(head,-1,sizeof(head));
	  memset(b,0,sizeof(b));
	  e=0;
	  for(i=1;i<n;i++)
	  {
		  scanf("%d%d",&ed[i].u,&ed[i].v);
		  add(ed[i].u,ed[i].v);
		  add(ed[i].v,ed[i].u);
	  }
	  sz[0]=0; id=1; dep[1]=0;
	  dfs1(1,1);
	  dfs2(1,1);
	  for(i=1;i<=m;i++)
	  {
		  scanf("%s%d%d%d",str,&u,&v,&k);
		  if(strcmp(str,"ADD1")==0)
		       getnode(u,v,k);
		  else
			  getedge(u,v,k);
	  }
	  for(i=1;i<n;i++)
	  {
		  if(dep[ed[i].u]<dep[ed[i].v])
			  mark2[ti[ed[i].v]]=i;
		  else
			  mark2[ti[ed[i].u]]=i;
	  }
	  printf("Case #%d:\n",cnt++);
	  s=0;
	  for(i=1;i<=n;i++)
	  {
          s+=a[i];
		  ans1[mark1[i]]=s;
	  }
	  for(i=1;i<=n;i++)
	  {
		  if(i==1)
			  printf("%I64d",ans1[i]);
		  else
			  printf(" %I64d",ans1[i]);
	  }
	  printf("\n");
	  s=0;
	  for(i=2;i<=n;i++)
	  {
		  s+=b[i];
          ans2[mark2[i]]=s;
	  }
	  for(i=1;i<n;i++)
	  {
		  if(i==1)
			  printf("%I64d",ans2[i]);
		  else
			  printf(" %I64d",ans2[i]);
	  }
	  printf("\n");
	}
	return 0;
}

附上我自己写的超时代码(以供对比反思借鉴):

TLE  code:

#pragma comment(linker, "/STACK:1024000000,1024000000") //因OJ采用Windows系统,要加入这一行用于 进行手动扩栈,这样就不会引起爆栈  
#include<stdio.h>
#include<string.h>
#include<vector>
const int N=100010;
using namespace std;
 
int head[N], to[N << 1], next1[N << 1], tot;//边信息 
int top[N];    //top[v]=u表示点v,u在一个链中,且u是这个链深度最小的点(即顶端)
int fath[N];   //记录父节点
int deep[N];   //每个点在树上的深度
int num[N];    //每棵子树的节点个数
int son[N];    //选的重边子节点
int p[N];      //树上每个点在线段树中所对应的点
int pos;       //线段树叶子结点总数 
 
void addEdge(const int& u, const int& v) {
  to[tot] = v, next1[tot] = head[u], head[u] = tot++;
}
 
void addUndirEdge(const int& u, const int& v) {
  addEdge(u, v), addEdge(v, u);
}
 
void init(int n)
{
    pos=0; tot=0;
    memset(son,-1,sizeof(son));
    memset(head,-1,sizeof(head));
}
void dfs1(int u,int pre,int d)//第一遍dfs求出fath,deep,num,son
{
    deep[u]=d; fath[u]=pre; num[u]=1;
    for (int i = head[u]; i != -1; i = next1[i]) {
    int v = to[i];
        if(v==pre)continue;
        dfs1(v,u,d+1);
        num[u]+=num[v];
        if(son[u]==-1||num[v]>num[son[u]])
            son[u]=v;
    }
}
void getpos(int u,int root)
{
    top[u]=root;
    p[u]=++pos;//从1开始  
    if(son[u]==-1)
        return ;
    getpos(son[u],root);
    for (int i = head[u]; i != -1; i = next1[i]) {
    int v = to[i];
        if(son[u]!=v&&v!=fath[u])
            getpos(v,v);
    }
}
 
//线段树
struct tree
{
    int sum,maxv,toc,addc;//区间和,区间最大值, 
 }root[N*4],root2[N*2];
int val[N],val2[N];//权值 
int MAX(int a,int b)
{
    return a>b?a:b;
}
void build(int l,int r,int k)//建树,范围为l~r,k为根节点的线段树。  build(1,pos,1);
{
    int mid=(l+r)/2;
    root[k].addc=0;
    root[k].toc=0;
    if(l==r){
        root[k].sum=root[k].maxv=val[l]; return ;
    }
    build(l,mid,k<<1);
    build(mid+1,r,k<<1|1);
    root[k].sum=root[k<<1].sum+root[k<<1|1].sum;
    root[k].maxv=MAX(root[k<<1].maxv,root[k<<1|1].maxv);
}
void build2(int l,int r,int k)//建树,范围为l~r,k为根节点的线段树。  build(1,pos,1);
{
    int mid=(l+r)/2;
    root2[k].addc=0;
    root2[k].toc=0;
    if(l==r){
        root2[k].sum=root2[k].maxv=val2[l]; return ;
    }
    build2(l,mid,k<<1);
    build2(mid+1,r,k<<1|1);
    root2[k].sum=root2[k<<1].sum+root2[k<<1|1].sum;
    root2[k].maxv=MAX(root2[k<<1].maxv,root2[k<<1|1].maxv);
}
void upson(int k,int l,int r)//更新儿子 
{
    int mid=(l+r)/2;
    if(root[k].toc)
    {
        root[k<<1].sum=(mid-l+1)*root[k].toc;
        root[k<<1].maxv=root[k].toc;
        root[k<<1].toc=root[k].toc;
        root[k<<1].addc=0;
 
        root[k<<1|1].sum=(r-mid)*root[k].toc;
        root[k<<1|1].maxv=root[k].toc;
        root[k<<1|1].toc=root[k].toc;
        root[k<<1|1].addc=0;
        root[k].toc=0;
    }
 
    if(root[k].addc)
    {
        root[k<<1].sum+=(mid-l+1)*root[k].addc;
        root[k<<1].maxv+=root[k].addc;
        root[k<<1].addc+=root[k].addc;
 
        root[k<<1|1].sum+=(r-mid)*root[k].addc;
        root[k<<1|1].maxv+=root[k].addc;
        root[k<<1|1].addc+=root[k].addc;
        root[k].addc=0;
    }
}
void upson2(int k,int l,int r)//更新儿子 
{
    int mid=(l+r)/2;
    if(root2[k].toc)
    {
        root2[k<<1].sum=(mid-l+1)*root2[k].toc;
        root2[k<<1].maxv=root2[k].toc;
        root2[k<<1].toc=root2[k].toc;
        root2[k<<1].addc=0;
 
        root2[k<<1|1].sum=(r-mid)*root2[k].toc;
        root2[k<<1|1].maxv=root2[k].toc;
        root2[k<<1|1].toc=root2[k].toc;
        root2[k<<1|1].addc=0;
        root2[k].toc=0;
    }
 
    if(root2[k].addc)
    {
        root2[k<<1].sum+=(mid-l+1)*root2[k].addc;
        root2[k<<1].maxv+=root2[k].addc;
        root2[k<<1].addc+=root2[k].addc;
 
        root2[k<<1|1].sum+=(r-mid)*root2[k].addc;
        root2[k<<1|1].maxv+=root2[k].addc;
        root2[k<<1|1].addc+=root2[k].addc;
        root2[k].addc=0;
    }
}
void updata1(int l,int r,int k,const int L,const int R,int c)//从范围为1(l)~pos(r),根节点为1(k)的线段树中更新 成段更新的区间位置为(L,R)的元素值为c
{
				                                       
    if(L<=l&&r<=R)
    {
        root[k].sum=(r-l+1)*c; root[k].maxv=c;
        root[k].toc=c; root[k].addc=0;
        return ;
    }
    int mid=(l+r)/2;
    upson(k,l,r);
 
    if(L<=mid)
        updata1(l,mid,k<<1,L,R,c);
    if(mid<R)
        updata1(mid+1,r,k<<1|1,L,R,c);
    root[k].sum=root[k<<1].sum+root[k<<1|1].sum;
    root[k].maxv=MAX(root[k<<1].maxv,root[k<<1|1].maxv);
}
void updata12(int l,int r,int k,const int L,const int R,int c)//从范围为1(l)~pos(r),根节点为1(k)的线段树中更新 成段更新的区间位置为(L,R)的元素值为c
{
				                                       
    if(L<=l&&r<=R)
    {
        root2[k].sum=(r-l+1)*c; root2[k].maxv=c;
        root2[k].toc=c; root2[k].addc=0;
        return ;
    }
    int mid=(l+r)/2;
    upson2(k,l,r);
 
    if(L<=mid)
        updata12(l,mid,k<<1,L,R,c);
    if(mid<R)
        updata12(mid+1,r,k<<1|1,L,R,c);
    root2[k].sum=root2[k<<1].sum+root2[k<<1|1].sum;
    root2[k].maxv=MAX(root2[k<<1].maxv,root2[k<<1|1].maxv);
}

void updata2(int l,int r,int k, int L, int R,int c)//从范围为1(l)~pos(r),根节点为1(k)的线段树中更新 成段更新的区间位置为(L,R)的元素值为原来的值加上增量c
{
    if(L<=l&&r<=R)
    {
        root[k].sum+=(r-l+1)*c; root[k].maxv+=c;
        root[k].addc+=c;
        return ;
    }
    int mid=(l+r)/2;
    upson(k,l,r);
 
    if(L<=mid)
        updata2(l,mid,k<<1,L,R,c);
    if(mid<R)
        updata2(mid+1,r,k<<1|1,L,R,c);
    root[k].sum=root[k<<1].sum+root[k<<1|1].sum;
    root[k].maxv=MAX(root[k<<1].maxv,root[k<<1|1].maxv);
}
void updata22(int l,int r,int k, int L, int R,int c)//从范围为1(l)~pos(r),根节点为1(k)的线段树中更新 成段更新的区间位置为(L,R)的元素值为原来的值加上增量c
{
    if(L<=l&&r<=R)
    {
        root2[k].sum+=(r-l+1)*c; root2[k].maxv+=c;
        root2[k].addc+=c;
        return ;
    }
    int mid=(l+r)/2;
    upson2(k,l,r);
 
    if(L<=mid)
        updata22(l,mid,k<<1,L,R,c);
    if(mid<R)
        updata22(mid+1,r,k<<1|1,L,R,c);
    root2[k].sum=root2[k<<1].sum+root2[k<<1|1].sum;
    root2[k].maxv=MAX(root2[k<<1].maxv,root2[k<<1|1].maxv);
}

int sum,maxv;
void query(int l,int r,int k,int L,int R)//查询范围为1(l)~pos(r),根节点为1(k)的线段树[L,R]区间的和(全局变量sum)与最大值(全局变量maxv) 
{
    if(L<=l&&r<=R)
    {
        sum+=root[k].sum;
        maxv=MAX(maxv,root[k].maxv);
        return ;
    }
    int mid=(l+r)/2;
    upson(k,l,r);
 
    if(L<=mid)
        query(l,mid,k<<1,L,R);
    if(mid<R)
        query(mid+1,r,k<<1|1,L,R);
}
void query2(int l,int r,int k,int L,int R)//查询范围为1(l)~pos(r),根节点为1(k)的线段树[L,R]区间的和(全局变量sum)与最大值(全局变量maxv) 
{
    if(L<=l&&r<=R)
    {
        sum+=root2[k].sum;
        maxv=MAX(maxv,root2[k].maxv);
        return ;
    }
    int mid=(l+r)/2;
    upson2(k,l,r);
 
    if(L<=mid)
        query2(l,mid,k<<1,L,R);
    if(mid<R)
        query2(mid+1,r,k<<1|1,L,R);
}
void swp(int &a,int &b)//交换a、b 
{
    int tt;
    tt=a; a=b; b=tt;
}
void Operat0(int u,int v) //查询u->v路径上节点的权值的和全局变量sum)、u->v路径上节点的最大权值(全局变量maxv)  
{
    int f1=top[u], f2=top[v];
    sum=0; maxv=0;
    while(f1!=f2)
    {
        if(deep[f1]<deep[f2])
        {
            swp(f1,f2); swp(u,v);
        }
        query(1,pos,1,p[f1],p[u]);//对应查询范围为1(l)~pos(r),根节点为1(k)的线段树[L,R]区间的和(全局变量sum)与最大值(全局变量maxv) 
        u=fath[f1]; f1=top[u];
    }
    if(deep[u]>deep[v]) swp(u,v);
    query(1,pos,1,p[u],p[v]);//点权查询与边权查询的区别之一:点p[u],边p[son[u]]  
}

void Operat02(int u,int v) //查询u->v路径上节点的权值的和全局变量sum)、u->v路径上节点的最大权值(全局变量maxv)  
{
    int f1=top[u], f2=top[v];
    sum=0; maxv=0;
    while(f1!=f2)
    {
        if(deep[f1]<deep[f2])
        {
            swp(f1,f2); swp(u,v);
        }
        query2(1,pos,1,p[f1],p[u]);//对应查询范围为1(l)~pos(r),根节点为1(k)的线段树[L,R]区间的和(全局变量sum)与最大值(全局变量maxv) 
        u=fath[f1]; f1=top[u];
    }
    if(deep[u]>deep[v]) swp(u,v);
    query2(1,pos,1,p[u],p[v]);//点权查询与边权查询的区别之一:点p[u],边p[son[u]]  
}
void Operat1(int u,int v,int c)//表示从u点到v点的路径的每条边权都变成c
{
    int f1=top[u], f2=top[v];
    while(f1!=f2)
    {
        if(deep[f1]<deep[f2])
        {
            swp(f1,f2); swp(u,v);
        }
        updata1(1,pos,1,p[f1],p[u],c);//对应从范围为1(l)~pos(r),根节点为1(k)的线段树中更新 成段更新的区间位置为(p[f1],p[u])的元素值为c
        u=fath[f1]; f1=top[u];
    }
    if(deep[u]>deep[v]) swp(u,v);
    updata1(1,pos,1,p[u],p[v],c);//点权更新与边权更新的区别之一:点p[u],边p[son[u]]  
}
void Operat2(int u,int v,int c)//表示从u点到v点的路径的每个点都加上c
{
    int f1=top[u], f2=top[v];
    while(f1!=f2)
    {
        if(deep[f1]<deep[f2])
        {
            swp(f1,f2); swp(u,v);
        }
        updata2(1,pos,1,p[f1],p[u],c);//对应从范围为1(l)~pos(r),根节点为1(k)的线段树中更新 成段更新的区间位置为(p[f1],p[u])的元素值为原来的值加上增量c
        u=fath[f1]; f1=top[u];//向上迭代 
    } 
    if(deep[u]>deep[v]) swp(u,v);
    updata2(1,pos,1,p[u],p[v],c);//点权更新与边权更新的区别之一:点p[u],边p[son[u]]  
}
void Operat22(int u,int v,int c)//表示从u点到v点的路径的每个点都加上c
{
    int f1=top[u], f2=top[v];
    while(f1!=f2)
    {
        if(deep[f1]<deep[f2])
        {
            swp(f1,f2); swp(u,v);
        }
        updata22(1,pos,1,p[f1],p[u],c);//对应从范围为1(l)~pos(r),根节点为1(k)的线段树中更新 成段更新的区间位置为(p[f1],p[u])的元素值为原来的值加上增量c
        u=fath[f1]; f1=top[u];//向上迭代 
    }
    if(u==v) return ;//点权更新与边权更新的区别之一:若是点权更新,则这一句要注释掉!!! 
    if(deep[u]>deep[v]) swp(u,v);
    updata22(1,pos,1,p[son[u]],p[v],c);//边权更新用这句,若是点权更新则用下一句代码 
}
struct EDG
{
    int u,v,c;
}edg[N];
int point[N];//点权值 
char ope[10];
void scanf ( int& x , char c = 0 , int flag = 0 ) {
	while ( ( c = getchar () ) != '-' && ( c < '0' || c > '9' ) ) ;
	if ( c == '-' ) flag = 1 , x = 0 ;
	else x = c - '0' ;
	while ( ( c = getchar () ) >= '0' && c <= '9' ) x = x * 10 + c - '0' ;
	if ( flag ) x = -x ;
}
inline void out(int num){
    bool flag=false;
    if(num<0){
        putchar('-');
        num=-num;
    }
    int ans[22],top=0;
    while(num!=0){
        ans[top++]=num%10;
        num/=10;
    }
    if(top==0)
        putchar('0');
    for(int i=top-1;i>=0;i--){
        char ch=ans[i]+'0';
        putchar(ch);
    }
}
int main()
{
	int cas,T;
    int n,m,q,op,a,b,c;
    scanf(cas);
    T=0;
    while(cas--)
    {
    	T++;
    	scanf(n);
    	scanf(q);
    	//scanf("%d%d",&n,&q);
        init(n);//初始化 
        m=n-1; 
       for(int i=1;i<=m;i++)
       {
       	    scanf(edg[i].u);
       	    scanf(edg[i].v);
        	//scanf("%d%d",&edg[i].u,&edg[i].v);
            addUndirEdge(edg[i].u, edg[i].v);
            edg[i].c=0;
            point[i]=0;  
		}
        point[n]=0;    
        dfs1(1,1,1);//第一遍dfs求出fath,deep,num,son
        getpos(1,1);
        pos=n;      //线段树叶子结点总数 
    	for(int i=1;i<n;i++)//将边的权值录入对应的线段树的位置上 
        {
            if(deep[edg[i].u]>deep[edg[i].v])
                edg[i].v=edg[i].u;
            val2[p[edg[i].v]]=edg[i].c; //转换成在线段树上的对应位置  
        }
        for(int i=1;i<=n;i++)
        {
        	val[p[i]]=point[i];//一定要注意转换成在线段树上的对应位置  
		}
        build(1,pos,1);
        build2(1,pos,1);
        while(q--)
        {
            scanf("%s",&ope);
            scanf(a),scanf(b),scanf(c);
            if(ope[3]=='1')
            {
                Operat2(a,b,c);//表示从a点到b点的路径的每条边权都加上c
            }
            else
            {
            	Operat22(a,b,c);//表示从a点到b点的路径的每条边权都加上c
			}
        }
        printf("Case #%d:\n",T);
        Operat0(1,1);
        	printf("%d",sum);
        for(int i=2;i<=n;i++)
        {
        	Operat0(i,i);
        	printf(" %d",sum);
		}
		puts("");
		Operat02(edg[1].u, edg[1].v);
        	//printf("%d",sum);
        	out(sum);
	    for(int i=2;i<=m;i++)
        {
            Operat02(edg[i].u, edg[i].v);
        //	printf(" %d",sum);
            printf(" ");
        	out(sum);
		}
		puts("");
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值