题意:三种操作 ①修改第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. 简易封装一个带有占位文字的TextView

    在实际iOS应用开发中我们经常会用到类似于下图所示的界面,即带有占位文字的文本框:

  2. LeetCode - 204. Count Primes - 埃拉托斯特尼筛法 95.12% - (C++) - Sieve of Eratosthenes

    原题 原题链接 Description: Count the number of prime numbers less than a non-negative number, n. 计算小于非负数n的 ...

  3. JavaWeb:基于MVC设计模式的一个小案例(一)

    (未经允许,请勿转载,谢谢.) 本案例的处理过程: 客户端发送一个请求给服务器,服务器把这个请求给Servlet,Servlet 获取请求信息,根据请求信息的情况去调用 model (在这里是一个普通 ...

  4. JS(一)

    循环还是很有意思的: 1) 安全数的作业: <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  5. CopyOnWriteArrayList理解与理解

     CopyOnWriteArrayList,因何而存在? ArrayList的一个线程安全的变体,其所有可变操作(add.set 等)都是通过对底层数组进行一次新的复制来实现的,代价昂贵. CopyO ...

  6. 宙斯是一个完整的Hadoop的作业平台[转]

    https://github.com/alibaba/zeus 宙斯(zeus)是什么 宙斯是一个完整的Hadoop的作业平台从Hadoop任务的调试运行到生产任务的周期调度 宙斯支持任务的整个生命周 ...

  7. opencv和javacv版本不一致

    Exception in thread "main" java.lang.UnsatisfiedLinkError: no jniopencv_highgui in java.li ...

  8. python课程第一天作业1-模拟登录

    第一周作业: 作业1:编写登陆接口 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 流程图: 代码:后来修改过一次: #!/usr/bin/env python # -*-conding:ut ...

  9. 使用VS Code调试TypeScript游戏程序JsTankGame成功!!!

    TypeScript游戏程序JsTankGame不是本人写的,是从CSDN下载的. JsTankGame是用Visual Studio开发的,因此在Visual Studio下调试非常顺畅.本人尝试用 ...

  10. poi实现Excel导出

    最近做了一个导出Excel的小功能,以前没接触过,现在分享下自己的代码,想让各位帮忙看看有啥地方可以优化,也方便自己以后查阅... 首先是excelAction的代码: /** * excelActi ...