题意:三种操作 ①修改第i条边的权值为val,②把u到v路径上的所有边的权值 去相反数③求u 到v路径上最大的边权

线段树的区间更新还是不熟练,,一直搞不对调试了好久还是没对,最后还是看的kuangbin的代码。

 #include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-;
const int maxn = 1e5+;
struct
{
int to,next;
}e[maxn<<];
int head[maxn],edge;
void add(int x,int y)
{
e[edge].to = y;
e[edge].next = head[x];
head[x] = edge++;
}
int son[maxn],fa[maxn],siz[maxn],dep[maxn];
void dfs(int root)
{
siz[root] = ;
son[root] = ;
for (int i = head[root]; i > ; i = e[i].next)
{
if (fa[root] != e[i].to)
{
dep[e[i].to] = dep[root] + ;
fa[e[i].to] = root;
dfs(e[i].to);
if (siz[e[i].to] > siz[son[root]])
son[root] = e[i].to;
siz[root] += siz[e[i].to];
}
}
}
int tot,top[maxn],li[maxn<<],pos[maxn];
void build(int root,int father)
{
top[root] = father;
pos[root] = tot;
li[tot++] = root;
if (son[root] > )
build(son[root],top[root]);
for (int i = head[root]; i > ; i = e[i].next)
if (fa[root] != e[i].to && son[root] != e[i].to)
build(e[i].to,e[i].to);
} int minv[maxn<<],maxv[maxn<<], tt[maxn],lazy[maxn<<];
void build_Segtree(int l,int r,int o)
{
lazy[o] = ;
if (l == r)
{
minv[o] = tt[li[l]];
maxv[o] =tt[li[l]];
return;
}
int mid = (l + r) >> ;
build_Segtree(l,mid,o<<);
build_Segtree(mid+,r,o<<|);
minv[o] = min(minv[o<<],minv[o<<|]);
maxv[o] = max(maxv[o<<],maxv[o<<|]);
}
char op[];
int val;
inline void push_down(int l,int r,int o)
{
if (lazy[o]&&l!=r)
{
maxv[o<<] = -maxv[o<<];
minv[o<<] = -minv[o<<];
maxv[o<<|] = -maxv[o<<|];
minv[o<<|] = -minv[o<<|];
swap(maxv[o<<],minv[o<<]);
swap(maxv[o<<|],minv[o<<|]);
lazy[o<<] ^= ;
lazy[o<<|] ^= ;
lazy[o] = ;
}
}
void update(int l,int r,int o,int ua,int ub)
{
if (ua <= l && ub >= r)
{
if (op[] == 'N')
{
maxv[o] = -maxv[o];
minv[o] = -minv[o];
swap(maxv[o],minv[o]);
lazy[o] ^= ;
}
if (op[] == 'C')
minv[o] = maxv[o] = val,lazy[o] = ;
return;
}
//if (op[0] == 'N')
push_down(l,r,o);
int mid = (l + r) >> ;
if (ua <= mid)
update(l,mid,o<<,ua,ub);
if (ub > mid)
update(mid+,r,o<<|,ua,ub);
minv[o] = min(minv[o<<],minv[o<<|]);
maxv[o] = max(maxv[o<<],maxv[o<<|]);
} int query(int l,int r,int o,int ua,int ub)
{
if (ua <= l && ub >= r)
return maxv[o];
int mid = (l + r) >> ;
push_down(l,r,o);
int t1 = -inf,t2 = -inf;
if (ua <= mid)
t1 = query(l,mid,o<<,ua,ub);
if (ub > mid)
t2 = query(mid+,r,o<<|,ua,ub);
minv[o] = min(minv[o<<],minv[o<<|]);
maxv[o] = max(maxv[o<<],maxv[o<<|]);
return max(t1,t2);
} int get_max(int ua,int ub)
{
int f1 = top[ua];
int f2 = top[ub];
int tmp = -inf;
while (f1 != f2)
{
if (dep[f1] < dep[f2])
swap(ua,ub),swap(f1,f2);
tmp = max(tmp,query(,tot,,pos[f1],pos[ua]));
ua = fa[f1];
f1 = top[ua];
}
if (ua == ub)
return tmp;
if (dep[ua] > dep[ub])
swap(ua,ub);
return tmp = max(tmp,query(,tot,,pos[son[ua]],pos[ub]));
}
void get_negate(int ua,int ub)
{
int f1 = top[ua];
int f2 = top[ub];
while (f1 != f2)
{
if (dep[f1] < dep[f2])
swap(ua,ub),swap(f1,f2);
update(,tot,,pos[f1],pos[ua]);
ua = fa[f1];
f1 = top[ua];
}
if (dep[ua] > dep[ub])
swap(ua,ub);
if (ua == ub)
return;
update(,tot,,pos[son[ua]],pos[ub]);
} int d[maxn][];
void init()
{
int root,n;
scanf ("%d",&n);
root = (n + ) >> ;
edge = tot = ;
memset(siz,,sizeof(siz));
memset(head,,sizeof(head));
fa[root] = dep[root] = ;
for (int i = ; i < n; i++)
{
scanf ("%d%d%d",&d[i][],&d[i][],&d[i][]);
add(d[i][],d[i][]);
add(d[i][],d[i][]);
}
dfs(root);
build(root,root);
build_Segtree(,tot,);
op[] = 'C';
for (int i = ; i < n; i++)
{
if (dep[d[i][]] < dep[d[i][]])
swap(d[i][],d[i][]);
tt[d[i][]] = val = d[i][];
update(,tot,,pos[d[i][]],pos[d[i][]]);
}
}
int main(void)
{
freopen("in.txt","r",stdin);
int t;
scanf ("%d",&t);
while (t--)
{
init();
while (scanf ("%s",op),op[] != 'D')
{
int x,y;
scanf ("%d%d",&x,&y);
if (op[] == 'Q'&&x!=y)
printf("%d\n",get_max(x,y));
if (op[] == 'Q' && x == y)
printf("0\n");
if (op[] == 'C')
{
val = y;
update(,tot,,pos[d[x][]],pos[d[x][]]);
}
if (op[] == 'N'&&x!=y)
get_negate(x,y);
}
}
return ;
} /*
1
11
2 1 1
2 4 2
2 3 3
1 5 4
3 6 5
3 7 6
7 8 7
4 9 8
9 10 9
10 11 10
N 2 10
Query 4 10
DONE */

poj3237--Tree 树链剖分的更多相关文章

  1. POJ3237 Tree 树链剖分 边权

    POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...

  2. POJ3237 Tree 树链剖分 线段树

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

  3. Hdu 5274 Dylans loves tree (树链剖分模板)

    Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...

  4. Query on a tree——树链剖分整理

    树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...

  5. 【BZOJ-4353】Play with tree 树链剖分

    4353: Play with tree Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 31  Solved: 19[Submit][Status][ ...

  6. SPOJ Query on a tree 树链剖分 水题

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

  7. poj 3237 Tree 树链剖分

    题目链接:http://poj.org/problem?id=3237 You are given a tree with N nodes. The tree’s nodes are numbered ...

  8. Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序

    Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...

  9. poj 3237 Tree 树链剖分+线段树

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

  10. 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 ...

随机推荐

  1. 魅蓝Note2跑分 MT6753性能究竟如何

    MT6753实力究竟如何? 采用LP工艺的MT6753实际上在性能和功耗方面并不比MT6752高,相反,同频下功耗要高1/3左右.并且其内存带宽是5.3G/s,小于MT 6752的6.4G/s 而且没 ...

  2. 内核与ramdisk到底是什么关系

    转自:http://www.lupaworld.com/forum.php?mod=viewthread&tid=61425 原名:内核与ramdisk到底是什么关系? 个人Notes:    ...

  3. C# winform xml的增删改查

    代码如下: using System; using System.Collections.Generic; using System.IO; using System.Linq; using Syst ...

  4. 当setTimeout遇到闭包

    1: function myTest(){ for(var i=0; i< 5; i++){ setTimeout(console.log(i), 0); } } myTest(); 或者比较正 ...

  5. JavaWeb三大组件(Servlet,Filter,Listener 自己整理,初学者可以借鉴一下)

    JavaWeb三大组件(Servlet,Filter,Listener 自己整理,初学者可以借鉴一下) Reference

  6. 关于eclipse几种插件的安装方法

    首先这里的安装方法按文件类型和安装方式来分 首先介绍按不同安装方式来分: 1.利用eclipse自带插件安装功能: 以jode插件为例,启动eclipse,help -> Software Up ...

  7. itext poi 学习之旅 (1)创建pdf

    从零开始学习itext 创建pdf 1.用到流进行创建的pdf import java.io.File; import java.io.FileOutputStream; import com.ite ...

  8. defer和async

    1.decument.wirte不能使用 2.<script src="text.js" type="text/javascript" defer=&qu ...

  9. windows启动、停止和重新启动Apache服务

    启动.停止和重新启动Apache服务(1) 在Windows操作系统中,Apache一般以服务的方式运行.在安装Apache时,如果你选择了“for all users”,Apache就会自动安装为一 ...

  10. HTML&CSS基础学习笔记1.17-表格的头部与尾部

    表格的头部和尾部 既然有标签表示表格的主体,那么自然表格的头部和尾部也有对应的标签. HTML中使用<thead>标签表示表格的头部,使用<tfoot>标签表示表格的尾部. 有 ...