题意:三种操作 ①修改第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. win7配置简单的FTP服务器

    1.开启一些win7默认是关闭状态的功能 步骤:控制面板 -> 程序和功能 -> 打开或关闭Windows功能,然后勾选下图中圈起来的部分: 2.添加FTP站点 步骤:桌面的计算机图标 - ...

  2. 导航条上UIBarButtonItem的更改方法(使用initWithCustomView:btn)

    UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:self.newMe ...

  3. nexus5 root教程

    转载自: http://www.inexus.co/article-1280-1.html http://www.pc6.com/edu/71016.html https://download.cha ...

  4. uva 10626 - Buying Coke(记忆化搜索)

    题目链接:10626 - Buying Coke 题目大意:给出要买可乐的数量, 以及1元,5元和10元硬币的数量, 每瓶可乐8元,每次照钱会按照最少硬币的方式找回, 问如何投币可使得投入的硬币数最少 ...

  5. [React] React Router: hashHistory vs browserHistory

    In this lesson we'll look at hashHistory which uses a hash hack to track our route changes vs browse ...

  6. Java基础知识强化59:String(字符串)和其他类型的相互转化

    1. String类型 ---> 其他类型 (1)使用基本类型包装类的parseXXX方法 e.g:String(字符串)转化为int(整型) String MyNumber ="12 ...

  7. inode-软链接与硬链接

    一.inode是什么?理解inode,要从文件储存说起.文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector).每个扇区储存512字节(相当于0.5KB).操作系统读取硬 ...

  8. 小学生之Log4j使用教程

    以前都是把所有日志都输出到一个文件下面,今天有个同事问想把某个包下的日志输出到 指定的地方,于是就在网上查了一些资料,总结一下,以免以后用到. 一.log4j是什么?  Log4j是一个开源的日志记录 ...

  9. (转)反射发送实战(-)InvokeMember

    反射是.net中的高级功能之一,利用反射可以实现许多以前看来匪夷所思的功能,下面是我看了<Programming C#>(O'Reilly)之后对于反射的一点实践,本想直接做个应用程序来说 ...

  10. Android Activity 分类

    在安卓系统中,Activity 按照优先级可以分为三种: 1. 前台Activity,是指正在和用户进行交互的Activity,优先级最高: 2.可见但非前台Activity,是指可见但无法与用户进行 ...