码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3237 Tree 树链剖分+线段树

时间:2016-08-07 10:49:56      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

Description

You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edges are numbered 1 through N ? 1. Each edge is associated with a weight. Then you are to execute a series of instructions on the tree. The instructions can be one of the following forms:

CHANGE i v : Change the weight of the ith edge to v

NEGATE a b : Negate the weight of every edge on the path from a to b

QUERY a b: Find the maximum weight of edges on the path from a to b

Input

The input contains multiple test cases. The first line of input contains an integer t (t ≤ 20), the number of test cases. Then follow the test cases.

Each test case is preceded by an empty line. The first nonempty line of its contains N (N ≤ 10,000). The next N ? 1 lines each contains three integers a, b and c, describing an edge connecting nodes a and b with weight c. The edges are numbered in the order they appear in the input. Below them are the instructions, each sticking to the specification above. A lines with the word “DONE” ends the test case.

Output

For each “QUERY” instruction, output the result on a separate line.

Sample Input

1

3
1 2 1
2 3 2
QUERY 1 2
CHANGE 1 3
QUERY 1 2
DONE

Sample Output

1
3
解析:很纯的一道树链剖分和线段树lazy标记的题,题解也有很多,这里只写下我处理的细节
细节:
1. 将边权赋给子节点,所以dfs求解父节点时,就直接给子节点赋予边的权值。同时为了之后的线段树的下标从1 ~ n-1根节点在树链剖分中的index 需要从0开始;
2.  对于改变某条边的权值,必须知道该边所对应的节点的id, 由于是链式建边的,所以最好使得2 ,3表示为第一条边,这样 i >> 1就表示当初输入时边的序号。
3. 线段树的pushdown操作注意下即可;(一是出现的位置不同,二是 是否及时pushdown)
#include<iostream>
#include
<cstdio>
#include
<algorithm>
#include
<cstring>
using namespace std;
#define lson l , m , rt<<1
#define rson m+1, r, rt<<1|1
#define inf 0x3f3f3f3f
#define MS1(a) memset(a,-1,sizeof(a))
const int maxn = 100007;
int head[maxn], tot, pos, son[maxn];
void init(){
    memset(head,
0, sizeof(head));
    pos
= 0; tot = 1;
    MS1(son);
}
struct edge{
   
int to, w, nxt;
}e[maxn
<<1];

inline
void ins(int u, int v,int w = 0)
{
    e[
++tot].nxt = head[u];
    e[tot].to
= v;
    e[tot].w
= w;
    head[u]
= tot;
}
int idx[maxn], weight[maxn];
int fa[maxn], cnt[maxn], dept[maxn];
void dfs(int u,int pre,int dep)
{
    cnt[u]
= 1;
    fa[u]
= pre;
    dept[u]
= dep;
   
for(int i = head[u]; i; i = e[i].nxt){
       
int v = e[i].to;
       
if(v != pre){
            weight[v]
= e[i].w;                       // 建线段树时,根据点来得到边权
              idx[i>>1] = v;                            // change 边的id -> 子节点;
            dfs(v, u, dep
+1);
            cnt[u]
+= cnt[v];
           
if(son[u] == -1 || cnt[son[u]] < cnt[v])
                son[u]
= v;
        }
    }
}
int p[maxn], fp[maxn], top[maxn];

void dfs(int u,int sp)
{
    top[u]
= sp;
    p[u]
= pos++;   // pos++
    fp[p[u]] = u;
   
if(son[u] == -1) return ;
    dfs(son[u], sp);
   
for(int i = head[u]; i; i = e[i].nxt){
       
int v = e[i].to;
        if(v != fa[u] && v != son[u])
            dfs(v, v);
    }
}

int mx[maxn], mn[maxn], lazy[maxn];
void pushup(int rt)
{
    mx[rt] = max(mx[rt<<1], mx[rt<<1|1]);
    mn[rt] = min(mn[rt<<1], mn[rt<<1|1]);
}
void build(int l, int r, int rt)
{
    if(l == r){
        mx[rt] = mn[rt] = weight[fp[l]];  // l为树链剖分之后点的id,需要转化为之前的id,得到权值;
        return ;
    }
    int m = l + r >> 1;
    if(l <= m) build(lson);
    if(m < r) build(rson);
    pushup(rt);
}
void print(int l,int r,int rt)
{
    if(l == r){
        printf("%d ",mx[rt]);
        return ;
    }
    int m = l + r >> 1;
    print(lson);
    print(rson);
}
void pushdown(int rt)
{
    if(lazy[rt]){
        lazy[rt] = 0;
        rt <<= 1;
     
        int t = -mx[rt];
        mx[rt] = -mn[rt];
        mn[rt] = t;
        lazy[rt] ^= 1;

        rt |= 1;
       
        t = -mx[rt];
        mx[rt] = -mn[rt];
        mn[rt] = t;
        lazy[rt] ^= 1;
    }
}
int query(int L, int R, int l,int r,int rt)
{
    if(lazy[rt]) pushdown(rt);
    if(L <= l && r <= R) return mx[rt];

    int m = l + r >> 1;
    int mx = -inf;
    if(L <= m) mx = max(mx, query(L, R, lson));
    if(m < R) mx = max(mx, query(L, R, rson));
    return mx;
}
int n;
int query(int u,int v)
{
    int fu = top[u], fv = top[v];
    int ans = -inf;
    while(fu != fv){
        if(dept[fu] < dept[fv]){
            swap(fu, fv); swap(u, v);
        }
        
        ans = max(ans, query(p[fu], p[u], 1, n-1, 1));
        u = fa[fu];
        fu = top[u];
       
    }
    if(u == v) return ans;
    if(dept[u] < dept[v]) swap(u, v);
    
    return max(ans, query(p[son[v]], p[u], 1, n-1, 1));
}
void update(int p,int val,int l,int r,int rt)
{
    pushdown(rt);
    if(l == r){
        mx[rt] = mn[rt] = val;
        return ;
    }
    int m = l + r >> 1;
    if(p <= m) update(p, val, lson);
    else update(p, val, rson);
    pushup(rt);
}
void Change(int pos, int val)
{
    int id = p[idx[pos]];
    update(id, val, 1, n-1, 1);
}
void lazyNegate(int L,int R,int l,int r,int rt)
{
    if(L <= l && r <= R){
       
        int t = mx[rt];
        mx[rt] = -mn[rt];
        mn[rt] = -t;
        lazy[rt] ^= 1;
        return ;
    }
    pushdown(rt);
    int m = l + r >> 1;
    if(L <= m) lazyNegate(L, R, lson);
    if(m < R) lazyNegate(L, R, rson);
    pushup(rt);
}
void Negate(int u,int v)
{
    int fu = top[u], fv = top[v];
    while(fu != fv){
        if(dept[fu] < dept[fv]){
            swap(fu, fv); swap(u, v);
        }
        lazyNegate(p[fu], p[u], 1, n-1, 1);
        u = fa[fu];
        fu = top[u];
    }
    if(u == v) return ;
    if(dept[u] < dept[v]) swap(u, v);
   
    lazyNegate(p[son[v]], p[u], 1, n-1, 1);
}
int main()
{
    //freopen("in.txt","r", stdin);
    //freopen("out.txt","w",stdout);
    int T;
    cin >> T;
    while(T--){
        init();
        int u, v, w;
        scanf("%d", &n);
        for(int i = 1;i < n; i++){
            scanf("%d%d%d",&u, &v, &w);
            ins(u,v,w);
            ins(v,u,w);
        }
        dfs(1,0,0);
        dfs(1,1);

        build(1,n-1,1);
        memset(lazy, 0, sizeof(lazy));
       
        char op[10];
        int a, b, cnt = 0;
        while(scanf("%s", op) == 1, op[0] != D){
            scanf("%d%d", &a, &b);
            if(op[0] == Q)
                printf("%d\n", query(a, b));
            else if(op[0] == C) Change(a, b);
            else Negate(a, b);
            //print(1,n-1,1);
        }
    }
}

poj 3237 Tree 树链剖分+线段树

标签:

原文地址:http://www.cnblogs.com/hxer/p/5745596.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!