poj3237树链剖分边权+区间取负
树链剖分+线段树lazy-tag
在树链上操作时千万不要写错。。
/*
树链剖分+线段树区间变负
*/
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define maxn 10005
struct Edge{
int to,next;
}edge[maxn<<];
int head[maxn],tot,e[maxn][];
int fa[maxn],son[maxn],num[maxn],deep[maxn];
int top[maxn],fp[maxn],p[maxn],pos;
inline void addedge(int u,int v){
edge[tot].to=v;edge[tot].next=head[u];head[u]=tot++;
}
void dfs1(int u,int pre,int dep){
fa[u]=pre;num[u]=;deep[u]=dep;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(v==pre) continue;
dfs1(v,u,dep+);
num[u]+=num[v];
if(son[u]==- || num[son[u]]<num[v]) son[u]=v;
}
}
void getpos(int u,int sp){
top[u]=sp;p[u]=pos++;fp[p[u]]=u;
if(son[u]==-) return;
getpos(son[u],sp);
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(v!=fa[u] && v!=son[u]) getpos(v,v);
}
} #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int Max[maxn<<],Min[maxn<<],rev[maxn<<];//遇到区间取反的通常维护一下区间两个极值
void build(int l,int r,int rt){
Max[rt]=Min[rt]=rev[rt]=;
if(l==r) return;
int m=l+r>>;
build(lson);build(rson);
}
inline void pushup(int rt){
Max[rt]=max(Max[rt<<],Max[rt<<|]);
Min[rt]=min(Min[rt<<],Min[rt<<|]);
}
inline void pushdown(int rt){
if(rev[rt]){
rev[rt<<]^=,rev[rt<<|]^=;
swap(Min[rt<<],Max[rt<<]);
Min[rt<<]*=-,Max[rt<<]*=-;
swap(Min[rt<<|],Max[rt<<|]);
Min[rt<<|]*=-,Max[rt<<|]*=-;
rev[rt]=;
}
}
void update(int pos,int val,int l,int r,int rt){
if(l==r){Max[rt]=Min[rt]=val;rev[rt]=;return;}
int m=l+r>>;
pushdown(rt);
if(pos<=m) update(pos,val,lson);
else update(pos,val,rson);
pushup(rt);
}
void seg_negate(int L,int R,int l,int r,int rt){
if(L<=l && R>=r){
swap(Min[rt],Max[rt]);
Min[rt]*=-,Max[rt]*=-;
rev[rt]^=;
return;
}
pushdown(rt);
int m=l+r>>;
if(L<=m) seg_negate(L,R,lson);
if(R>m) seg_negate(L,R,rson);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
if(L<=l && R>=r)return Max[rt];
pushdown(rt);
int m=l+r>>,res=-;
if(L<=m) res=max(res,query(L,R,lson));
if(R>m) res=max(res,query(L,R,rson));
pushup(rt);
return res;
}
int find(int u,int v)//查询u->v边的最大值
{
int f1 = top[u], f2 = top[v];
int tmp = -;
while(f1 != f2)
{
if(deep[f1] < deep[f2])
{
swap(f1,f2);
swap(u,v);
}
tmp = max(tmp,query(p[f1],p[u],,pos,));
u = fa[f1]; f1 = top[u];
}
if(u == v)return tmp;
if(deep[u] > deep[v]) swap(u,v);
return max(tmp,query(p[son[u]],p[v],,pos,));
}
void Negate(int u,int v)//把u-v路径上的边的值都设置为val
{
int f1 = top[u], f2 = top[v];
while(f1 != f2)
{
if(deep[f1] < deep[f2])
{
swap(f1,f2);
swap(u,v);
}
seg_negate(p[f1],p[u],,pos,);
u = fa[f1]; f1 = top[u];
}
if(u == v)return;
if(deep[u] > deep[v]) swap(u,v);
return seg_negate(p[son[u]],p[v],,pos,);
}
void init(){
tot=pos=;
memset(head,-,sizeof head);
memset(son,-,sizeof son);
}
int main(){
int T,n,a,b,c;
char op[];
cin >> T;
while(T--){
init();
scanf("%d",&n);
for(int i=;i<=n-;i++){
scanf("%d%d%d",&e[i][],&e[i][],&e[i][]);
addedge(e[i][],e[i][]);addedge(e[i][],e[i][]);
}
dfs1(,,);getpos(,);build(,pos,);
for(int i=;i<=n-;i++){
if(deep[e[i][]]>deep[e[i][]]) swap(e[i][],e[i][]);
update(p[e[i][]],e[i][],,pos,);
} while(scanf("%s",op)){
if(op[]=='D') break;
scanf("%d%d",&a,&b);
if(op[]=='C')update(p[e[a][]],b,,pos,);
else if(op[]=='N') Negate(a,b);
else printf("%d\n",find(a,b));
}
}
}
poj3237树链剖分边权+区间取负的更多相关文章
- poj2763树链剖分边权+区间和
自己写的比原来的板子常数小了不少嘻嘻,边权处理起来比点权要复杂一下 由于根节点没有被映射的边,其被访问到的顺序是0,直接排除在线段树外 #include<iostream> #includ ...
- POJ3237 Tree 树链剖分 边权
POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
- BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分 - 点权剖分 - 单点权修改)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 树链剖分模版题,打的时候注意点就行.做这题的时候,真的傻了,单词拼错检查了一个多小时 ...
- POJ2763 Housewife Wind 树链剖分 边权
POJ2763 Housewife Wind 树链剖分 边权 传送门:http://poj.org/problem?id=2763 题意: n个点的,n-1条边,有边权 修改单边边权 询问 输出 当前 ...
- HDU3669 Aragorn's Story 树链剖分 点权
HDU3669 Aragorn's Story 树链剖分 点权 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: n个点的,m条边,每个点都 ...
- POJ 3237.Tree -树链剖分(边权)(边值更新、路径边权最值、区间标记)贴个板子备忘
Tree Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 12247 Accepted: 3151 Descriptio ...
- POJ3237 (树链剖分+线段树)
Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...
- 洛谷 P3384 【模板】树链剖分-树链剖分(点权)(路径节点更新、路径求和、子树节点更新、子树求和)模板-备注结合一下以前写的题目,懒得写很详细的注释
P3384 [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节 ...
随机推荐
- shell函数与数组
shell函数与数组 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.为什么要使用shell函数 简单的说函数的作用就是把程序里多次调用的相同的代码部分定义成一份,然后起个名字, ...
- 如何下载网易云音乐APP里的MV和短视频?
本人:网易云音乐死粉,朋友圈大多都用的是云音乐,因为推荐功能牛逼 然后:发现云音乐APP里越来越多吸引我的短视频,经常看到好的就想保存到相册,然后微信发给朋友 但是:不知道怎么下载网易云音乐的短视频, ...
- idea 插件的使用 进阶篇(个人收集使用中的)
idea 插件的使用 进阶篇(个人收集使用中的) 恭喜你,如果你已经看到这篇文章,证明在idear使用上已经初有小成!那么就要向着大神进发了! 下边就是大神之路! 插件的设置 在 IntelliJ I ...
- UESTC - 1167 一句话题意
---恢复内容开始--- 题目链接:https://vjudge.net/problem/UESTC-1167 请问从n*n的正方形左下角走到右上角且不越过对角线的情况总数模m的结果~ 分析: 还记得 ...
- Spark2.1.0编译
1.下载spark源码包 http://spark.apache.org/downloads.html 2.安装Scala与maven,解压spark源码包 安装Scala: tar zxf scal ...
- css3让图文不能复制
-webkit-user-select: none; -ms-user-select: none; -moz-user-select: none; -khtml-user-select: none; ...
- Socket 连接建立过程
阻塞模式下: 1,客户端向服务器端发起请求建立连接时,服务器端只需要运行到 serverSocket = ); 客户端注册的 SelectionKey.OP_CONNECT 事件就能够发生. 也就是 ...
- 通过read()读文件
一.在POSIX中的定义 #include <unistd.h> ssize_t read(int fd, void *buf, size_t len); 二.调用read()的可能结果 ...
- 在安卓上用Termux安装sqlmap
1.打开Termux执行以下命令 apt update apt install git apt install python2 // 安装sqlmap运行环境 2.从github上下载sqlmap , ...
- MIPS架构上函数调用过程的堆栈和栈帧
转载于CSDN:http://blog.csdn.net/do2jiang/article/details/5404566 在计算机科学中,Call stack是指存放某个程序的正在运行的函数的信息的 ...