LCA+RMQ+树状数组poj2763

40 篇文章 0 订阅
18 篇文章 0 订阅

Language:
Housewife Wind
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 5754 Accepted: 1442

Description

After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautiful huts. There are some pairs of huts connected by bidirectional roads. We say that huts in the same pair directly connected. XX Village is so special that we can reach any other huts starting from an arbitrary hut. If each road cannot be walked along twice, then the route between every pair is unique.  

Since Jiajia earned enough money, Wind became a housewife. Their children loved to go to other kids, then make a simple call to Wind: 'Mummy, take me home!'  

At different times, the time needed to walk along a road may be different. For example, Wind takes 5 minutes on a road normally, but may take 10 minutes if there is a lovely little dog to play with, or take 3 minutes if there is some unknown strange smell surrounding the road.  

Wind loves her children, so she would like to tell her children the exact time she will spend on the roads. Can you help her?  

Input

The first line contains three integers n, q, s. There are n huts in XX Village, q messages to process, and Wind is currently in hut s. n < 100001 , q < 100001.  

The following n-1 lines each contains three integers a, b and w. That means there is a road directly connecting hut a and b, time required is w. 1<=w<= 10000.  

The following q lines each is one of the following two types:  

Message A: 0 u  
A kid in hut u calls Wind. She should go to hut u from her current position.  
Message B: 1 i w  
The time required for i-th road is changed to w. Note that the time change will not happen when Wind is on her way. The changed can only happen when Wind is staying somewhere, waiting to take the next kid.  

Output

For each message A, print an integer X, the time required to take the next child.

Sample Input

3 3 1
1 2 1
2 3 2
0 2
1 2 3
0 3

Sample Output

1
3


更新边的时候用树状数组更新,但自己写的一直WA,实在不知道哪错了 ,期待大神解救。。。好像不用树状数组也可以过

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int N = 100005;
const int M = 20;

int __pow[M];
int head[N],tot;
struct edge{
    int u,v,w,next;
}e[2*N];
int dep[N],first[N],dir[N],vis[N],ver[2*N],R[2*N];
int dp[2*N][M];

inline void add(int u ,int v ,int w)
{
    e[tot].u = u; e[tot].v = v; e[tot].w = w;
    e[tot].next = head[u]; head[u] = tot++;
}

void dfs(int u ,int d)
{
    tot++; vis[u] = 1; dep[u] = d; first[u] = tot; ver[tot] = u; R[tot] = d;
    for(int k=head[u]; k!=-1; k=e[k].next)
        if(!vis[e[k].v])
        {
            int v = e[k].v , w = e[k].w;
            dir[v] = dir[u] + w;
            dfs(v,d+1);
            tot++; ver[tot] = u; R[tot] = d;
        }
}

void ST(int n)
{
    int K = (int)(log((double)n) / log(2.0));
    for(int i=1; i<=n; i++) dp[i][0] = i;
    for(int j=1; j<=K; j++)
        for(int i=1; i+__pow[j]-1<=n; i++)
        {
            int a = dp[i][j-1] , b = dp[i+__pow[j-1]][j-1];
            if(R[a] < R[b]) dp[i][j] = a;
            else            dp[i][j] = b;
        }
}

inline int RMQ(int x ,int y)
{
    int K = (int)( log((double)(y-x+1)) / log(2.0) );
    int a = dp[x][K] , b = dp[y-__pow[K]+1][K];
    if(R[a] < R[b]) return a;
    else            return b;
}

int LCA(int u ,int v)
{
    int x = first[u] , y = first[v];
    if(x > y) swap(x,y);
    int index = RMQ(x,y);
    return ver[index];
}

void travel(int u , int par ,int delta)
{
    dir[u] += delta;
    for(int k=head[u]; k!=-1; k=e[k].next)
        if(e[k].v != par)
        {
            int v = e[k].v;
            travel(v,u,delta);
        }
}

int main()
{
    int n,q,sp;
    for(int i=0; i<M; i++) __pow[i] = (1<<i);
    while(scanf("%d%d%d",&n,&q,&sp)!=EOF)
    {
        tot = 0;
        memset(head,-1,sizeof(head));
        memset(vis,0,sizeof(vis));
        for(int i=1; i<n; i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            add(v,u,w);
        }
        tot = dir[1] = 0;
        dfs(1,1);
        ST(2*n-1);
        while(q--)
        {
            int c;
            scanf("%d",&c);
            if(c == 0)
            {
                int v;
                scanf("%d",&v);
                int lca = LCA(sp,v);
                printf("%d\n",dir[sp] + dir[v] - 2*dir[lca]);
                sp = v;
            }
            else
            {
                int k,w,delta,u,v;
                scanf("%d%d",&k,&w);
                k = (k-1) << 1; u = e[k].u; v = e[k].v; 
                delta = w - e[k].w; e[k].w = e[k^1].w = w;
                int x = dep[u] > dep[v] ? u : v;
                int y = dep[u] < dep[v] ? u : v;
                travel(x,y,delta);
            }

//            for(int i=1; i<=n; i++) printf("%d ",dir[i]); printf("\n");
        }
    }
    return 0;
}

自己的:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=400010;
int pre[maxn],E[maxn],pos[maxn],dep[maxn],last[maxn];
int vis[maxn],dis[maxn],d[maxn][40];
int N,Q,s,e,num;
int head[maxn],tree[maxn];
struct EDGE
{
    int u,v,next,f;
}edge[maxn*2];
struct A
{
    int a,b,w;
    A(int nx,int y,int p)
    {
        a=nx;
        b=y;
        w=p;
    }
    A(){}
}Ed[maxn];

void init()
{
    //for(int i=0;i<=N;i++)g[i].clear();
    memset(dis,0,sizeof(dis));
    memset(vis,0,sizeof(vis));
    memset(pos,-1,sizeof(pos));
    memset(head,-1,sizeof(head));
    memset(tree,0,sizeof(tree));
    num=0;
}
void add_edge(int a,int b,int w)
{
    edge[num].u=a;
    edge[num].v=b;
    edge[num].next=head[a];
    edge[num].f=w;
    head[a]=num++;
}

void dfs(int u,int depth)
{
    E[++num]=u,dep[num]=depth;
    if(pos[u]==-1)pos[u]=num;
    vis[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(vis[v])continue;
        dis[v]=dis[u]+edge[i].f;
        pre[v]=u;
        dfs(v,depth+1);
        E[++num]=u;
        dep[num]=u;
    }
    last[u]=num;
}
void initRMQ(int n)
{
    for(int i=0;i<=n;i++)d[i][0]=i;
    for(int j=1;(1<<j)<=n;j++)
        for(int i=1;i+(1<<j)<=n;i++)
        {
            int x=d[i][j-1],y=d[i+(1<<(j-1))][j-1];
            if(dep[x]<dep[y])d[i][j]=x;
            else d[i][j]=y;
        }
}

void add(int x,int val)
{
    while(x<maxn)
    {
        tree[x]+=val;
        x+=x&(-x);
    }
}
int getsum(int x)
{
    int sum=0;
    while(x>0)
    {
        sum+=tree[x];
        x-=x&(-x);
    }
    return sum;
}
int LCA(int u,int v)
{
    int x=pos[u],y=pos[v];
    if(x>y)swap(x,y);
    int k=0;
    while((1<<(k+1))<=y-x+1)k++;
    int a=d[x][k],b=d[y-(1<<k)+1][k];
    if(dep[a]<dep[b])return E[a];
    else return E[b];
}
int main()
{
    while(scanf("%d%d%d",&N,&Q,&s)!=EOF)
    {
        init();
        for(int i=1;i<N;i++)
        {
            int a,b,w;
            scanf("%d%d%d",&a,&b,&w);
            Ed[i]=A(a,b,w);
            add_edge(a,b,w);
            add_edge(b,a,w);
        }
        num=0;
        dfs(s,1);
        pre[s]=s;
        initRMQ(num);
        while(Q--)
        {
            int op,u,x,w;
            scanf("%d",&op);
            if(!op)
            {
                scanf("%d",&e);
                int lca=LCA(s,e);
                printf("%d\n",getsum(pos[s])+getsum(pos[e])-2*getsum(pos[lca])+dis[s]+dis[e]-2*dis[lca]);
                s=e;
            }
            else
            {
                scanf("%d%d",&x,&w);
                int a=Ed[x].a;
                int b=Ed[x].b;
                if(pre[a]==b)swap(a,b);
                int tmp=w-Ed[x].w;
                Ed[x].w=w;
                add(pos[b],tmp);
                add(last[b]+1,-tmp);
            }
        }
    }
    return 0;
}






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值