poj 2763 Housewife Wind(线段树+树剖)

106 篇文章 0 订阅
44 篇文章 0 订阅

将边的值转成深度大的端点的点值,然后就是树剖找lca通过dfs序把一条链的点映射到线段树上的区间去查询就好了。


需要注意的一点是,传入线段树的是dfs序,但是线段数的操作不需要去管dfs序,直接根据传进来的参数做就行了,这是一个容易写错的地方。


代码:

#include <iostream>
#include <cstdio>
#define MID int mid=(l+r)>>1;
#define lson o<<1
#define rson o<<1|1

using namespace std;
const int maxn=1e5+5;
int val[maxn<<2];
struct bian
{
    int u, v, w;
}bmap[maxn];
int n, m;
int dfn[maxn], siz[maxn], son[maxn], FA[maxn], dis[maxn], dep[maxn], f[maxn];
void update(int o, int l, int r, int x, int w)
{
//    cout<<l<<" "<<r<<endl;
    if(l==r && l==x)
    {
        val[o]=w;
        return;
    }

    MID
    if(x<=mid)update(lson, l, mid, x, w);
    else update(rson, mid+1,r, x, w);
    val[o]=val[lson]+val[rson];
    return;
}

int query(int o, int l, int r, int ll, int rr)
{
//    printf("%d %d %d %d %d\n", ll, rr,l, r, val[o]);
    if(ll<=l && r<=rr)return val[o];
    int res=0;
    MID
    if(ll<=mid)res+=query(lson, l, mid, ll, rr);
    if(mid<rr)res+=query(rson, mid+1, r, ll, rr);
    return res;
}

void build(int o, int l, int r)
{
    if(l==r)
    {
//        printf("%d %d %d\n", l, dfn[l], dis[dfn[l]]);
        val[o]=dis[l];
        return;
    }
    MID
    build(lson, l, mid);
    build(rson, mid+1, r);
    val[o]=val[lson]+val[rson];
    return;
}

struct edg
{
    int to;
    int w;
    int nex;
}edg[maxn*10];
int head[maxn], cnt;
void add(int u, int v, int w)
{
    edg[cnt].to=v;
    edg[cnt].w=w;
    edg[cnt].nex=head[u];
    head[u]=cnt++;
}

void dfs1(int x, int fa, int d)
{
    siz[x]=1, son[x]=0, dep[x]=d;
    int to, w, i;
    for(i=head[x]; i!=-1; i=edg[i].nex)
    {
        to=edg[i].to, w=edg[i].w;

        if(to!=fa)
        {

            f[to]=x;
            dfs1(to, x, d+1);
            siz[x]+=siz[to];
            if(son[x]==0 || siz[to]>siz[son[x]])son[x]=to;
        }
    }
    return;
}
int tot;
void dfs2(int x, int fa, int F)
{
    FA[x]=F, dfn[x]=++tot;
    if(son[x]!=0)
    {
        dfs2(son[x], x, F);
    }
    int i, to;
    for(i=head[x]; i!=-1; i=edg[i].nex)
    {
        to=edg[i].to;
        if(to!=fa && to!=son[x])
        {
            dfs2(to, x, to);
        }
    }
    return;
}

int pathquery(int x, int y)
{
    int px=FA[x], py=FA[y], ans=0;

    while(px!=py)
    {
//        printf("%d %d %d %d\n", x, y, px, py);
        if(dep[px]>dep[py])
        {
            ans+=query(1, 1, n, dfn[px], dfn[x]);
            x=f[px];
            px=FA[x];
        }
        else
        {
            ans+=query(1, 1, n, dfn[py], dfn[y]);
            y=f[py], py=FA[y];
        }
//        printf("%d %d %d %d\n", x, y, px, py);
    }
    if(x!=y)
    {
        if(dep[x]>dep[y])swap(x, y);
//        cout<<dfn[x]<<" "<<dfn[y]<<endl;
        ans+=query(1, 1, n, dfn[x]+1, dfn[y]);
    }

    return ans;
}
void init()
{
    for(int i=0; i<=n; i++)head[i]=-1;
    cnt=0;
    return;
}
int main()
{
    int q, s, i, j, w;
//    while(~scanf("%d%d%d", &n, &q, &s))
        cin>>n>>q>>s;
    {
    cnt=tot=0;
    init();
    for(i=1; i<=n-1; i++)
    {
        scanf("%d%d%d", &bmap[i].u, &bmap[i].v, &bmap[i].w);
        add(bmap[i].u, bmap[i].v, bmap[i].w);
        add(bmap[i].v, bmap[i].u, bmap[i].w);
    }

    dfs1(1, 0, 1);
    dfs2(1, 0, 1);
    for(i=1; i<=n-1; i++)
    {
        int u=dep[bmap[i].u]>dep[bmap[i].v]?bmap[i].u:bmap[i].v;
        dis[dfn[u]]=bmap[i].w;
    }
//    for(i=2; i<=n; i++)printf("%d ", dis[dfn[i]]);printf("\n");
    build(1, 1, n);
//    for(i=1; i<=n; i++)
//    {
//        printf("%d %d %d %d\n", i, FA[i], son[i], f[i]);
//    }
    int last=s, x, y, op, u, v;
    while(q--)
    {
        scanf("%d", &op);
        if(op==0)
        {
            scanf("%d", &x);
            printf("%d\n", pathquery(last, x));
            last=x;
        }
        else
        {
            scanf("%d%d", &x, &y);
            u=bmap[x].u, v=bmap[x].v;
            int s=dep[u]>dep[v]?u:v;
//            cout<<s<<endl;
            update(1, 1, n, dfn[s], y);
        }
    }
    }
    return 0;
}
/*
13
20 1
1 2 1
2 4 1
4 10 1
2 5 1
5 11 1
11 12 1
1 3 1
3 7 1
3 6 1
6 8 1
8 9 1
9 13 1
1 12 5
0 13
*/


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值