【POJ3237】Tree 树链剖分+线段树
【POJ3237】Tree
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
题解:树剖+线段树裸题,其中取相反数的操作就是把最大值和最小值交换在分别取相反数就好了
#include <stdio.h>
#include <string.h>
#include <iostream>
#define lson x<<1
#define rson x<<1|1
using namespace std;
typedef long long ll;
const int maxn=10010;
int n,cnt,tot;
int sm[maxn<<2],sn[maxn<<2],tag[maxn<<2];
int to[maxn<<1],next[maxn<<1],val[maxn<<1],v[maxn],u[maxn],fa[maxn],head[maxn];
int deep[maxn],size[maxn],son[maxn],p[maxn],top[maxn];
char str[10];
int readin()
{
int ret=0,sig=1; char gc;
while(gc<'0'||gc>'9') sig=(gc=='-')?-1:1,gc=getchar();
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret*sig;
}
void add(int a,int b,int c)
{
to[cnt]=b;
val[cnt]=c;
next[cnt]=head[a];
head[a]=cnt++;
}
void dfs1(int x)
{
size[x]=1;
for(int i=head[x];i!=-1;i=next[i])
{
if(to[i]!=fa[x])
{
fa[to[i]]=x;
u[to[i]]=val[i];
deep[to[i]]=deep[x]+1;
dfs1(to[i]);
size[x]+=size[to[i]];
if(size[to[i]]>size[son[x]]) son[x]=to[i];
}
}
}
void dfs2(int x,int tp)
{
top[x]=tp;
p[x]=++tot;
v[p[x]]=u[x];
if(son[x]) dfs2(son[x],tp);
for(int i=head[x];i!=-1;i=next[i])
if(to[i]!=son[x]&&to[i]!=fa[x])
dfs2(to[i],to[i]);
}
void pushup(int x)
{
sm[x]=max(sm[lson],sm[rson]);
sn[x]=min(sn[lson],sn[rson]);
}
void pushdown(int x)
{
if(tag[x])
{
swap(sm[lson],sn[lson]),swap(sm[rson],sn[rson]);
sm[lson]=-sm[lson],sn[lson]=-sn[lson],sm[rson]=-sm[rson],sn[rson]=-sn[rson];
tag[lson]^=1,tag[rson]^=1;
tag[x]=0;
}
}
void build(int l,int r,int x)
{
if(l==r)
{
sm[x]=sn[x]=v[l];
return ;
}
int mid=l+r>>1;
build(l,mid,lson),build(mid+1,r,rson);
pushup(x);
}
void updata(int l,int r,int x,int a,int b)
{
if(l==r)
{
sm[x]=sn[x]=b;
return ;
}
pushdown(x);
int mid=l+r>>1;
if(a<=mid) updata(l,mid,lson,a,b);
else updata(mid+1,r,rson,a,b);
pushup(x);
}
void upgate(int l,int r,int x,int a,int b)
{
if(a<=l&&r<=b)
{
tag[x]^=1;
swap(sm[x],sn[x]);
sm[x]=-sm[x],sn[x]=-sn[x];
return ;
}
pushdown(x);
int mid=l+r>>1;
if(b<=mid) upgate(l,mid,lson,a,b);
else if(a>mid) upgate(mid+1,r,rson,a,b);
else upgate(l,mid,lson,a,b),upgate(mid+1,r,rson,a,b);
pushup(x);
}
int query(int l,int r,int x,int a,int b)
{
if(a<=l&&r<=b) return sm[x];
pushdown(x);
int mid=l+r>>1;
if(b<=mid) return query(l,mid,lson,a,b);
if(a>mid) return query(mid+1,r,rson,a,b);
return max(query(l,mid,lson,a,b),query(mid+1,r,rson,a,b));
}
void change()
{
int x=readin(),y=readin();
updata(1,n,1,max(p[to[x*2-2]],p[to[x*2-1]]),y);
}
void fan()
{
int x=readin(),y=readin();
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);
upgate(1,n,1,p[top[x]],p[x]);
x=fa[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
if(x!=y) upgate(1,n,1,p[x]+1,p[y]);
}
void getmax()
{
int x=readin(),y=readin(),ans=1<<31;
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);
ans=max(ans,query(1,n,1,p[top[x]],p[x]));
x=fa[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
if(x!=y) ans=max(ans,query(1,n,1,p[x]+1,p[y]));
printf("%d\n",ans);
}
void work()
{
n=readin();
memset(head,-1,sizeof(head));
memset(son,0,sizeof(son));
memset(fa,0,sizeof(fa));
memset(tag,0,sizeof(tag));
cnt=tot=0;
int i,a,b,c;
for(i=1;i<n;i++)
{
a=readin(),b=readin(),c=readin();
add(a,b,c),add(b,a,c);
}
deep[1]=1;
dfs1(1);
dfs2(1,1);
build(1,n,1);
while(scanf("%s",str))
{
switch(str[0])
{
case 'C':change(); break;
case 'N':fan(); break;
case 'Q':getmax(); break;
case 'D':return ;
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--) work();
return 0;
}
【POJ3237】Tree 树链剖分+线段树的更多相关文章
- 【POJ3237】Tree(树链剖分+线段树)
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
- POJ3237 Tree 树链剖分 线段树
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ3237 题意概括 Description 给你由N个结点组成的树.树的节点被编号为1到N,边被编号为1 ...
- POJ3237 (树链剖分+线段树)
Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...
- 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 ...
- 【CF725G】Messages on a Tree 树链剖分+线段树
[CF725G]Messages on a Tree 题意:给你一棵n+1个节点的树,0号节点是树根,在编号为1到n的节点上各有一只跳蚤,0号节点是跳蚤国王.现在一些跳蚤要给跳蚤国王发信息.具体的信息 ...
- 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, ...
- Water Tree CodeForces 343D 树链剖分+线段树
Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...
- 【BZOJ-2325】道馆之战 树链剖分 + 线段树
2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1153 Solved: 421[Submit][Statu ...
- B20J_3231_[SDOI2014]旅行_树链剖分+线段树
B20J_3231_[SDOI2014]旅行_树链剖分+线段树 题意: S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,城市信仰不同的宗教,为了方便,我们用不同的正整数代表各种宗教. S国 ...
- BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 )
BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 ) 题意分析 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 ...
随机推荐
- 利用CocoaPods,在项目中导入AFNetworking类库
场景1:利用CocoaPods,在项目中导入AFNetworking类库 AFNetworking类库在GitHub地址是:https://github.com/AFNetworking/AFNetw ...
- NYOJ题目817英文藏头诗
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtEAAAKXCAIAAADLqoEhAAAgAElEQVR4nO3dO1LrzNbG8W8S5AyE2D
- hdu 2546饭卡
用5块钱去买最贵的物品,用剩下的m-5块去买尽量多的物品 #include<stdio.h> #include<math.h> #include<vector> # ...
- 查看局域网内在线的主机ip和mac地址
]# nmap -sP Starting Nmap 5.51 ( http://nmap.org ) at 2016-12-12 22:43 CST Nmap scan report for 192. ...
- 在iMac机os x上装win7双系统经验心得
首先,以上iMac的内存超过4GB,需要安装x64版的win7,可以用QQ旋风从这里下载(cn_windows_7_ultimate_with_sp1_x64_dvd_u_677408.iso) 下载 ...
- JAVA 堆栈知识和Volatile关键字
栈内存:存放基本类型的变量和对象的引用 堆内存:存放用new创建的对象和数组 栈帧:保存了局部变量表,操作数栈,方法的返回地址以及其它的附加信息 volatile修饰的变量,jvm虚拟机只是保证从主内 ...
- 攻城狮在路上(壹) Hibernate(四)--- 对象标识符(OID)生成机制
Hibernate使用对象标识符(OID)来建立内存中对象和数据库表中记录的对应关系,对象的OID和数据库的主键对应.为了保证OID的唯一性和不可变性,应该让Hibernate来为OID赋值.Hibe ...
- Installing Hadoop on Mac OSX Yosemite Tutorial Part 1.
Installing Hadoop on Mac OSX Yosemite Tutorial Part 1. September 23, 2014 Marek 68 Comments Install ...
- html5移动Web开发实战
1.解决横竖屏字体大小变化 html{ -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; text-size-adjust:100 ...
- hdu 4061 福州赛区网络赛A 数学 ***
a1/sum #include<cstdio> #include<iostream> #include<algorithm> #include<cstring ...