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 = ;
int head[maxn], tot, pos, son[maxn];
void init(){
    memset(head, , sizeof(head));
    pos = ; tot = ;
    MS1(son);
}
struct edge{
    int to, w, nxt;
}e[maxn<<];

inline void ins(int u, int v,int w = )
{
    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] = ;
    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>>] = v;                            // change 边的id -> 子节点;
            dfs(v, u, dep+);
            cnt[u] += cnt[v];
            if(son[u] == - || 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] == -) 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<<], mx[rt<<|]);
mn[rt] = min(mn[rt<<], mn[rt<<|]);
}
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 >> ;
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 >> ;
print(lson);
print(rson);
}
void pushdown(int rt)
{
if(lazy[rt]){
lazy[rt] = ;
rt <<= ; int t = -mx[rt];
mx[rt] = -mn[rt];
mn[rt] = t;
lazy[rt] ^= ; rt |= ; t = -mx[rt];
mx[rt] = -mn[rt];
mn[rt] = t;
lazy[rt] ^= ;
}
}
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 >> ;
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], , n-, ));
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], , n-, ));
}
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 >> ;
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, , n-, );
}
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] ^= ;
return ;
}
pushdown(rt);
int m = l + r >> ;
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], , n-, );
u = fa[fu];
fu = top[u];
}
if(u == v) return ;
if(dept[u] < dept[v]) swap(u, v); lazyNegate(p[son[v]], p[u], , n-, );
}
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 = ;i < n; i++){
scanf("%d%d%d",&u, &v, &w);
ins(u,v,w);
ins(v,u,w);
}
dfs(,,);
dfs(,); build(,n-,);
memset(lazy, , sizeof(lazy)); char op[];
int a, b, cnt = ;
while(scanf("%s", op) == , op[] != 'D'){
scanf("%d%d", &a, &b);
if(op[] == 'Q')
printf("%d\n", query(a, b));
else if(op[] == 'C') Change(a, b);
else Negate(a, b);
//print(1,n-1,1);
}
}
}

poj 3237 Tree 树链剖分+线段树的更多相关文章

  1. POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 )

    POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 ) 题意分析 给出n个点,m个询问,和当前位置pos. 先给出n-1条边,u->v以及边权w. 然后有m个询问 ...

  2. Aizu 2450 Do use segment tree 树链剖分+线段树

    Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...

  3. 【POJ3237】Tree(树链剖分+线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

  4. POJ3237 Tree 树链剖分 线段树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ3237 题意概括 Description 给你由N个结点组成的树.树的节点被编号为1到N,边被编号为1 ...

  5. 【CF725G】Messages on a Tree 树链剖分+线段树

    [CF725G]Messages on a Tree 题意:给你一棵n+1个节点的树,0号节点是树根,在编号为1到n的节点上各有一只跳蚤,0号节点是跳蚤国王.现在一些跳蚤要给跳蚤国王发信息.具体的信息 ...

  6. Spoj Query on a tree SPOJ - QTREE(树链剖分+线段树)

    You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...

  7. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

  8. 【BZOJ-2325】道馆之战 树链剖分 + 线段树

    2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1153  Solved: 421[Submit][Statu ...

  9. POJ3237 (树链剖分+线段树)

    Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...

  10. B20J_3231_[SDOI2014]旅行_树链剖分+线段树

    B20J_3231_[SDOI2014]旅行_树链剖分+线段树 题意: S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,城市信仰不同的宗教,为了方便,我们用不同的正整数代表各种宗教. S国 ...

随机推荐

  1. 《MFC游戏开发》笔记五 定时器和简单动画

    本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9332377 作者:七十一雾央 新浪微博:http:// ...

  2. JAVA 多态的一种实现

    今天一个同事问我一个问题,就是关于子类,父类之间方法的调用这里的.这里我整理了一个小DEMO. 代码如下: 父类的代码: public abstract class ClassA { public f ...

  3. VMware系统运维(十三)部署虚拟化桌面 Horizon View Agent 5.2安装

    1.先打开安装程序如下所示 2.点击"下一步" 3.不接受,你能装吗?点击"下一步" 4.选择安装位置,点击"下一步" 5.开启3389和3 ...

  4. poj 1821 动态规划

    思路:每次枚举每个工人的右边界j,维护最优的左边界k.那么dp[j]=max(dp[j],dp[k]+(j-k)*w[i].p): 对于每个工人的初值k=w[i].s-1; 令x=j-w[i].l,如 ...

  5. BZOJ 1146: [CTSC2008]网络管理Network 树链剖分+线段树+平衡树

    1146: [CTSC2008]网络管理Network Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 870  Solved: 299[Submit] ...

  6. 数理方程:Laplace变换 & 留数(更新中)

    更新:25 APR 2016 Laplace变换 设函数\(f(t)\)在\(t>0\)时有定义,积分 \(F(s)=\int_0^{+\infty}f(t)e^{-st}dt \qquad ( ...

  7. 【CSS3】---为边框应用图片 border-image

    为边框应用图片 border-image 顾名思义就是为边框应用背景图片,它和我们常用的background属性比较相似.例如: background:url(xx.jpg) 10px 20px no ...

  8. 【转载]】Microsoft SQL Server, 错误:4064的解决方法

    SQL SERVER – Fix : Error: 4064 – Cannot open user default database. Login failed. Login failed for u ...

  9. 物联网 WIFI 一键配置原理(smartconfig) ESP8266/QCA4004

    自从物联网 问世以来,如何使得物  能够联网 有了很多的方式,目前运用非常广的WIFI,今天就总结下自这个方面,也对于有需要的盆友 也希望有抛砖引玉之效果. 物联网:  智能硬件+APP+云 APP ...

  10. PAT1014——福尔摩斯的约会

    大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm”.大侦探很快就明白了,字条 ...