8.4 poj 2763 Housewife Wind (the right side of the split tree processing chain

Meaning of the questions:

An edge weight containing a given tree and the tree of n nodes, a total of q times the operation, is divided into two

0 c: s to find the distance from the position c, and s c becomes

1 ab: the article becomes a weight of the side b

 

Right edge processing generally have two options:

1. The side of the point

All upcoming the n-1 sides of a node are increased, i.e. the right side of the right point, no new tree 2n-1 nodes in point to the right of the split tree chain

(Generally not

 

2. The edge attachment point

The edge weights attached on its end deeper depth (dep [v]> dep [u]), the processing x, y when a path between the other operations before, but does not process the final x, y point of the right of lca (since the upper right side of this path it is not represented in the right point):

The final step is: ans + = query (1, dfn [son [x]], dfn [y]); or ans + = query (1, dfn [x] +1, dfn [y]);

Note that when x when x == y, y is present Shen lca, does not operate directly on the return line

 

The inscribed one day also have not seen orz

First, wa: the problem is to deal with when it comes to above x == y no attention

Then has been tle, the problem is unknown, the segment into a tree structure or wording tle

Paste the code, it could be found a mistake in which:

//树链剖分 b
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>

#define fi first
#define se second
#define rep( i ,x ,y ) for( int i= x; i<= y ;i++ )
#define reb( i ,y ,x ) for( int i= y; i>= x ;i-- )
#define mem( a ,x ) memset( a ,x ,sizeof(a))
#define lson  pos<<1 ,l ,m
#define rson  pos<<1|1 ,m+1 ,r
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int ,int> pii;
typedef pair<ll ,ll> pll;
typedef pair<string ,int> psi;

const int inf = 0x3f3f3f3f;
const int N = 100005;
const int M = 1000005;

struct Edge{
    int to ,val ,next;
}edge[N<<1];

struct E{
    int u ,v ,c;
}e[N<<1];

struct Tree{
    int left ,right;
    int t ,lazy;
}tree[N<<2];

int val[N] ,tr[N<<2];
int head[N];
int dep[N] ,dfn[N] ,son[N] ,fa[N];
int rnk[N] ,sz[N] ,top[N];
int cnt = 0,tot = 0 ,n ,q ,s ;


void init( ){
    cnt = tot =0;
    dep[1] = val[1] = 0;
    mem( head ,0 );
    mem( son , 0 );
    
}

void add_edge( int u ,int v ){
    edge[++cnt].to = v;
    edge[cnt].next = head[u];
    head[u] = cnt;
}

// sgement tree

void push_up( int pos ){
    tree[pos].t = tree[pos<<1].t + tree[pos<<1|1].t;
}

void build( int pos , int l ,int r ){
    
        tree[pos].left = l;
        tree[pos].right= r;
    if( l==r ){    
        tree[pos].t = val[ rnk[l] ];
        return;
    }
    int m = (l+r)>>1;
    build( lson );
    build( rson );
    push_up( pos ); 
}


void updata( int pos ,int p ,int v ){
    //cout<<tree[pos].left<<" "<<tree[pos].right<<endl;
    if( tree[pos].left == p && tree[pos].right ==p ){
        tree[pos].t = v; return;
    }
    
    int m = (tree[pos].left+tree[pos].right)>>1;
    if( p<=m)updata( pos<<1 ,p ,v );
    else updata( pos<<1|1 ,p ,v );
    push_up( pos );
}

int query( int pos ,int l ,int r  ){
    if( l <= tree[pos].left && tree[pos].right <= r){
        return tree[pos].t;
    }
    int ans = 0;
    int m = ( tree[pos].left+tree[pos].right )>>1;
    if( m >= l )ans += query( pos<<1 ,l ,r );
    if( m < r ) ans += query( pos<<1|1 ,l ,r );
    return ans;
}

// tree - chain 
// dfs 1-- dep ,sz ,fa ,son
void dfs1( int u ,int f ){
    sz[u] = 1;
    for( int i = head[u]; i ;i = edge[i].next ){
        int v = edge[i].to;
        if( v == f )continue;
        dep[v] = dep[u] + 1;
        fa[v] = u;
        dfs1( v ,u );
        sz[u] += sz[v];
        if( sz[v] >sz[ son[u] ] )son[u] = v;
    }
}

void dfs2( int u ,int t ){
    dfn[u] = ++tot;
    top[u] = t;
    rnk[tot] = u;
    if( son[u] )dfs2( son[u] ,t );
    for( int i = head[u] ;i ;i = edge[i].next ){
        int v = edge[i].to;
        if( v!= fa[u] && v!= son[u] )dfs2(v,v);
    }
}

int LCA( int x ,int y ){
    int fx = top[x] ,fy = top[y];
    while( fx != fy ){
        if( dep[x] < dep[y] )swap(x ,y) ,swap( fx ,fy );
        x= fa[fx] ,fx = top[x];
    }
    return dep[x] < dep[y] ? x : y ;
}

/*void c_updata( int x ,int y ,int v ){
    int fx = top[x] ,fy = top[y];
    while( fx != fy ){
        if( dep[fx] < dep[fy] )swap(x ,y) ,swap(fx ,fy);
        updata( 1 ,1 ,n ,dfn[fx] ,dfn[x] ,v);
        x = f[x] ,fx = top[x];
    }
    if( dfn[x] > dfn[y] )swap( x ,y );
    updata( 1 ,1 ,n ,dfn[x] ,dfn[y] ,v);
}
*/
int c_query( int x ,int y ){
    int fx = top[x] ,fy = top[y];
    int ans = 0;
    while( fx != fy ){
        if( dep[fx] < dep[fy] )swap(x ,y) ,swap(fx ,fy);
        ans += query( 1 ,dfn[fx] ,dfn[x] );
        x = fa[x] ,fx = top[x];
    }
    if( x==y )return ans;
    if( dfn[x] > dfn[y] )swap( x ,y );
    //cout<<x<<" son2 "<<son[x]<<endl;
    // x = son[x]; 致死错误 
    if(dfn[x] != dfn[y])ans += query( 1 ,dfn[son[x]] ,dfn[y] );
    return ans ;
}

void rev_edge( ){
    rep( i ,1 ,n-1 ){
        if( dep[e[i].v] > dep[e[i].u] )val[e[i].v] = e[i].c;
        else val[e[i].u] = e[i].c;
        //cout<<" rev_edge "<<e[i].u<<" "<<val[e[i].u]<<" "<<e[i].v<<" "<<val[e[i].v]<<endl;
        } 
}

int main( ){
    scanf( "%d%d%d" ,&n ,&q ,&s ) ;
    //init();
    rep( i ,1 ,n-1 ){
       scanf("%d%d%d" ,&e[i].v ,&e[i].u ,&e[i].c);
       add_edge( e[i].u ,e[i].v);
       add_edge( e[i].v ,e[i].u);
    }
    dfs1( 1 ,1 );
    dfs2( 1 ,1 );
    rev_edge( );
    build( 1 ,1 ,n );
    int op ,l ,r ,z;
    while( q-- ){
        scanf("%d" ,&op );
        if( op ){
            scanf("%d%d",&l ,&z);
            if( dep[e[l].v] > dep[e[l].u] )l = e[l].v;
            else l = e[l].u;
            updata( 1 ,dfn[l] ,z );
        }
        else{
            scanf("%d" ,&l );
            printf( "%d\n" ,c_query( s ,l) );
            s = l;
        }
    }

    return 0;
} 

 

Others can live code: https: //www.cnblogs.com/yaoyueduzhen/p/5311230.html

I think that is attached to the right side of the node that process we slightly different, the rest is basically the same, ah, why I had not written it?

sad, caught in the confusion and great frustration

Guess you like

Origin www.cnblogs.com/-ifrush/p/11298343.html