hdu3966 Aragorn's Story(基于点权的树链剖分模板题(模板是基于已完善的边权树剖模板修改的,模板较较完善))

来源:互联网 发布:华夏网络中西 编辑:程序博客网 时间:2024/09/21 06:36


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


Aragorn's Story

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5531    Accepted Submission(s): 1459


Problem Description
Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who want to invade his kingdom. As Aragorn knows, the enemy has N camps out of his kingdom and M edges connect them. It is guaranteed that for any two camps, there is one and only one path connect them. At first Aragorn know the number of enemies in every camp. But the enemy is cunning , they will increase or decrease the number of soldiers in camps. Every time the enemy change the number of soldiers, they will set two camps C1 and C2. Then, for C1, C2 and all camps on the path from C1 to C2, they will increase or decrease K soldiers to these camps. Now Aragorn wants to know the number of soldiers in some particular camps real-time.
 

Input
Multiple test cases, process to the end of input.

For each case, The first line contains three integers N, M, P which means there will be N(1 ≤ N ≤ 50000) camps, M(M = N-1) edges and P(1 ≤ P ≤ 100000) operations. The number of camps starts from 1.

The next line contains N integers A1, A2, ...AN(0 ≤ Ai ≤ 1000), means at first in camp-i has Ai enemies.

The next M lines contains two integers u and v for each, denotes that there is an edge connects camp-u and camp-v.

The next P lines will start with a capital letter 'I', 'D' or 'Q' for each line.

'I', followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, increase K soldiers to these camps.

'D', followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, decrease K soldiers to these camps.

'Q', followed by one integer C, which is a query and means Aragorn wants to know the number of enemies in camp C at that time.
 

Output
For each query, you need to output the actually number of enemies in the specified camp.
 

Sample Input
3 2 51 2 32 12 3I 1 3 5Q 2D 1 2 2Q 1 Q 3
 

Sample Output
748
Hint
1.The number of enemies may be negative.2.Huge input, be careful.
 

Source
2011 Multi-University Training Contest 13 - Host by HIT
 


题意:给一棵树,并给定各个点权的值,然后有3种操作:

I C1 C2 K: 把C1与C2的路径上的所有点权值加上K

D C1 C2 K:把C1与C2的路径上的所有点权值减去K

Q C:查询节点编号为C的权值



AC code:

#pragma comment(linker, "/STACK:1024000000,1024000000") //因OJ采用Windows系统,要加入这一行用于 进行手动扩栈,这样就不会引起爆栈  #include<stdio.h>#include<string.h>#include<vector>const int N=50015;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];int val[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 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 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 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);}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 swp(int &a,int &b)//交换a、b {    int tt;    tt=a; a=b; b=tt;}/* 已经在边权查询的基础上修改好了的点权查询,实现查询u->v路径上节点的权值的和全局变量sum)、u->v路径上节点的最大权值(全局变量maxv)其他点权查询功能在这边权的模板上依样画葫芦修改即可实现*/ 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(u==v) return ;//点权查询与边权查询的区别之一:若是点权查询,则这一句要注释掉!!!     if(deep[u]>deep[v]) swp(u,v);    //query(1,pos,1,p[son[u]],p[v]);//边权查询用这句,若是点权查询则用下一句代码     query(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(u==v) return ;    if(deep[u]>deep[v])swp(u,v);    updata1(1,pos,1,p[son[u]],p[v],c);}/* 已经在边权更新的基础上修改好了的点权更新,实现从u点到v点的路径的每个点都加上c,其他点权更新功能在这边权的模板上依样画葫芦修改即可实现*/ 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(u==v) return ;//点权更新与边权更新的区别之一:若是点权更新,则这一句要注释掉!!!     if(deep[u]>deep[v]) swp(u,v);    //updata2(1,pos,1,p[son[u]],p[v],c);//边权更新用这句,若是点权更新则用下一句代码     updata2(1,pos,1,p[u],p[v],c);//点权更新与边权更新的区别之一:点p[u],边p[son[u]]  }/* 在边权更新的基础上修改好了的点权更新 */ struct EDG{    int u,v,c;}edg[N];int point[N]; char ope[3];int main(){    int n,m,q,op,a,b;    while(scanf("%d%d%d",&n,&m,&q)!=EOF)    {        init(n);//初始化         for(int i=1;i<=n;i++) //输入点权值             scanf("%d",&point[i]);          /*for(int i=1;i<n;i++)        {            scanf("%d%d%d",&edg[i].u,&edg[i].v,&edg[i].c);            addUndirEdge(edg[i].u, edg[i].v);        }*/       for(int i=1;i<=m;i++) //输入点权值        {        scanf("%d%d",&edg[i].u,&edg[i].v);            addUndirEdge(edg[i].u, edg[i].v);}        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;            val[p[edg[i].v]]=edg[i].c; //转换成在线段树上的对应位置          }*/        for(int i=1;i<=n;i++)        {        val[p[i]]=point[i];//一定要注意转换成在线段树上的对应位置  }        build(1,pos,1);        while(q--)        {            //scanf("%d%d%d",&op,&a,&b);            scanf("%s%d",&ope,&a);            b=a;            if(ope[0]=='Q')            {                //Operat0(a,b); //查询u->v路径上节点的权值的和全局变量sum)、u->v路径上节点的最大权值(全局变量maxv)                //printf("%d %d\n",maxv,sum);                Operat0(a,b);                 printf("%d\n",sum);            }            //else if(op==1)              //  updata1(1,pos,1,p[edg[a].v],p[edg[a].v],b);//从范围为1~pos,根节点为1的线段树中更新原先按顺序输入时的第a条边,将该边权值改为b                                           //也就是单点更新(其对应线段树成段更新的区间位置为(p[edg[a].v],p[edg[a].v]))              else            {                int tt,c;                scanf("%d%d",&tt,&c);                //if(tt==0)                 //   Operat1(a,b,c);//表示从a点到b点的路径的每条边权都变成c               // else                  //  Operat2(a,b,c);//表示从a点到b点的路径的每条边权都加上c                if(ope[0]=='D'){c=-c;}Operat2(a,tt,c);//表示从a点到b点的路径的每个点权都加上c             }        }    }}


0 0
原创粉丝点击