【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 ab 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 树链剖分+线段树的更多相关文章

  1. 【POJ3237】Tree(树链剖分+线段树)

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

  2. POJ3237 Tree 树链剖分 线段树

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

  3. POJ3237 (树链剖分+线段树)

    Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...

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

  5. 【CF725G】Messages on a Tree 树链剖分+线段树

    [CF725G]Messages on a Tree 题意:给你一棵n+1个节点的树,0号节点是树根,在编号为1到n的节点上各有一只跳蚤,0号节点是跳蚤国王.现在一些跳蚤要给跳蚤国王发信息.具体的信息 ...

  6. 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, ...

  7. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

  8. 【BZOJ-2325】道馆之战 树链剖分 + 线段树

    2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1153  Solved: 421[Submit][Statu ...

  9. B20J_3231_[SDOI2014]旅行_树链剖分+线段树

    B20J_3231_[SDOI2014]旅行_树链剖分+线段树 题意: S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,城市信仰不同的宗教,为了方便,我们用不同的正整数代表各种宗教. S国 ...

  10. BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 )

    BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 ) 题意分析 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 ...

随机推荐

  1. TCP/IP五层模型

    (2)TCP/IP五层模型的协议   应用层 传输层 网络层 数据链路层 物理层   物理层:中继器.集线器.还有我们通常说的双绞线也工作在物理层 数据链路层:网桥(现已很少使用).以太网交换机(二层 ...

  2. Java使用正则表达式解析LRC歌词文件

    LRC歌词是一种应用广泛的歌词文件,各主流播放器都支持. lrc歌词文本中含有两类标签: 1.标识标签(ID-tags) [ar:艺人名] [ti:曲名] [al:专辑名] [by:编者(指编辑LRC ...

  3. iOS - 常用的宏定义

    1.处理NSLog事件(开发者模式打印,发布者模式不打印) 1 2 3 4 5   #ifdef DEBUG   #define NSLog(FORMAT, ...) fprintf(stderr,& ...

  4. NYOJ题目916胖子小的百宝袋

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsQAAAL6CAIAAAC+R9cJAAAgAElEQVR4nOydO5LcvM6GzyYmn4U47o

  5. Java之POJO

    转: POJO    一:什么是POJOPOJO的名称有多种,pure old java object .plain ordinary java object 等.按照Martin Fowler的解释 ...

  6. python字符串中插入变量

  7. 与你相遇好幸运,mbview的mbtiles文件分析

    mbview是一个查看.mbtiles文件的本地程序. https://github.com/mapbox/mbview .mbtiles文件就是一个Sqlite文件,用Navicat Premium ...

  8. android 相对布局里面的一些属性

    一.   有关于RelativeLayout布局的一些属性 1.  相对于兄弟控件的位置:android:layout_below Android:layout_toLeftof Android:la ...

  9. hdu 4762 公式 java

    n/(n)^(m-1) import java.io.*; import java.math.*; import java.util.*; public class Main { static Big ...

  10. ASP.NET 5探险(1):Azure中配置连接字符串、独立项目执行EF7数据迁移

    (此文章同时发表在本人微信公众号“dotNET每日精华文章”,欢迎右边二维码来关注.) 题记:我开始把ASP.NET 5用于生产系统开发已经有1个多月了,也填了一些坑积累了一些经验,从今天开始会陆陆续 ...