hdu5893List wants to travel

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5893

题意:给定一棵n个节点的树,有边权。m个操作,操作1:给定a,b,求树上a到b上有多少个数字段(比如11233就是3段);操作2:给定a,b,c,将树上a到b路径上所有的边权全部修改为c。

分析:树链剖分,在查询时合并的时候注意一下细节即可。

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=40010;
const int M=50010;
const int mod=1000000007;
const int MOD1=1000000007;
const int MOD2=1000000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const int INF=1000000010;
const ll MAX=1ll<<55;
const double eps=1e-5;
const double inf=~0u>>1;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
int tot,u[N],v[2*N],w[2*N],pre[2*N];
void add_edge(int a,int b,int c) {
    v[tot]=b;w[tot]=c;pre[tot]=u[a];u[a]=tot++;
}
int pos[N],val[N],pov[N];
int fa[N],dep[N],siz[N],son[N],top[N];
void dfs1(int a,int b,int c) {
    fa[a]=b;dep[a]=dep[b]+1;
    siz[a]=1;son[a]=0;val[a]=c;
    for (int i=u[a];~i;i=pre[i])
    if (v[i]!=b) {
        dfs1(v[i],a,w[i]);siz[a]+=siz[v[i]];
        if (siz[v[i]]>siz[son[a]]) son[a]=v[i];
    }
}
void dfs2(int a,int b) {
    if (son[b]==a) top[a]=top[b];
    else top[a]=a;pos[a]=++tot;pov[tot]=val[a];
    if (son[a]) dfs2(son[a],a);
    for (int i=u[a];~i;i=pre[i])
    if (v[i]!=b&&v[i]!=son[a]) dfs2(v[i],a);
}
char s[10];
int n,sig[N<<2];
struct node {
    int l,r,type;
    node() {}
    node(int l,int r,int type):l(l),r(r),type(type) {}
}tr[N<<2];
node unio(node a,node b) {
    node ret;
    ret=node(a.l,b.r,a.type+b.type);
    if (a.r==b.l) ret.type--;
    return ret;
}
void tran(int a,int b) {
    tr[a<<1]=node(b,b,1);
    tr[a<<1|1]=node(b,b,1);
    sig[a]=0;sig[a<<1]=sig[a<<1|1]=1;
}
void build(int a,int l,int r) {
    if (l==r) {
        tr[a]=node(pov[l],pov[l],1);sig[a]=0;return ;
    }
    int mid=(l+r)>>1;
    build(a<<1,l,mid);build(a<<1|1,mid+1,r);
    sig[a]=0;tr[a]=unio(tr[a<<1],tr[a<<1|1]);
}
void update(int a,int l,int r,int L,int R,int b) {
    if (l==L&&r==R) {
        tr[a]=node(b,b,1);sig[a]=1;return ;
    }
    if (sig[a]) tran(a,tr[a].l);
    int mid=(l+r)>>1;
    if (R<=mid) update(a<<1,l,mid,L,R,b);
    else if (L>mid) update(a<<1|1,mid+1,r,L,R,b);
        else update(a<<1,l,mid,L,mid,b),update(a<<1|1,mid+1,r,mid+1,R,b);
    tr[a]=unio(tr[a<<1],tr[a<<1|1]);
}
void change(int a,int b,int c) {
    int t1=top[a],t2=top[b];
    while (t1!=t2) {
        if (dep[t1]<dep[t2]) swap(a,b),swap(t1,t2);
        update(1,1,n,pos[t1],pos[a],c);
        a=fa[t1];t1=top[a];
    }
    if (dep[a]>dep[b]) swap(a,b);
    if (pos[a]<pos[b]) update(1,1,n,pos[a]+1,pos[b],c);
}
node find_node(int a,int l,int r,int L,int R) {
    if (l==L&&r==R) return tr[a];
    if (sig[a]) tran(a,tr[a].l);
    int mid=(l+r)>>1;
    if (R<=mid) return find_node(a<<1,l,mid,L,R);
    else if (L>mid) return find_node(a<<1|1,mid+1,r,L,R);
        else return unio(find_node(a<<1,l,mid,L,mid),find_node(a<<1|1,mid+1,r,mid+1,R));
}
node query(int a,int b) {
    if (a==b) return node(0,0,0);
    int t1=top[a],t2=top[b];
    node L=node(0,0,0),R=node(0,0,0);
    while (t1!=t2)
    if (dep[t1]<dep[t2]) {
        R=unio(find_node(1,1,n,pos[t2],pos[b]),R);
        b=fa[t2];t2=top[b];
    } else {
        L=unio(find_node(1,1,n,pos[t1],pos[a]),L);
        a=fa[t1];t1=top[a];
    }
    if (a!=b) {
        if (dep[a]<dep[b]) R=unio(find_node(1,1,n,pos[a]+1,pos[b]),R);
        else L=unio(find_node(1,1,n,pos[b]+1,pos[a]),L);
    }
    swap(L.l,L.r);
    return unio(L,R);
}
int main()
{
    int a,b,c,i,m;
    while (scanf("%d%d", &n, &m)!=EOF) {
        tot=0;memset(u,-1,sizeof(u));
        for (i=1;i<n;i++) {
            scanf("%d%d%d", &a, &b, &c);
            add_edge(a,b,c);add_edge(b,a,c);
        }
        dfs1(1,0,0);tot=0;dfs2(1,0);
        build(1,1,n);
        while (m--) {
            scanf("%s%d%d", s, &a, &b);
            if (s[0]=='Q') {
                node ans=query(a,b);
                printf("%d\n", ans.type);
            } else {
                scanf("%d", &c);change(a,b,c);
            }
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值