LCA+RMQ+树状数组poj2763

Language: Default
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 
#include 
#include 
#include 
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 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
#include
#include
#include
#include
#include
#include
#include
#include
#include
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<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<






你可能感兴趣的:(LCA,DFS,RMQ,树状数组/线段树)