【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. IOS中定时器NSTimer的开启与关闭

    调用一次计时器方法: myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scro ...

  2. Jquery学习笔记---闭包

    1. 简要介绍 闭包可谓是js中的一大特色了,即使你对闭包没概念,你可能已经在不知不觉中使用到了闭包.闭包是什么,闭包就是一个函数可以访问到另一个函数的变量.这就是闭包,解释起来就这么一句话,不明白? ...

  3. 在Eclipse中自定义类似syso的快捷代码模板

    sysout/syso syserr/ syse 点击菜单栏的“Window”->“Preferences”,打开“Preferences”对话框.在Preferences”对话框中点击“Jav ...

  4. Delphi中的异常处理

    转载:http://www.cnblogs.com/doit8791/archive/2012/05/08/2489471.html 以前写Delphi程序一直不注意异常处理,对其异常处理的机制总是一 ...

  5. 攻城狮在路上(叁)Linux(十一)--- 用户与用户组、文件权限、目录配置

    一.用户与用户组: 3个概念:文件所有者(user).用户组(group).其他人(others). /etc/passwd  <==存放所有的用户名 /etc/shadow  <==存放 ...

  6. 为什么没有选择sipml5

    转自:http://www.myvoipapp.com/blogs/yxh/2015/01/23/%E4%B8%BA%E4%BB%80%E4%B9%88%E6%B2%A1%E6%9C%89%E9%80 ...

  7. 推荐一些顶级的Android开发书籍(转)

    本帖对Android开发菜鸟和中级老兵同样适用,因为本帖重点列出了那些最常用的Android开发资源. 推荐书籍 你或许觉得,都现在了,再去看个什么书来学习Android开发,这方法应该早就过时了.没 ...

  8. BZOJ 1861: [Zjoi2006]Book 书架 splay

    1861: [Zjoi2006]Book 书架 Description 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书 ...

  9. c++11 正则表达式基本使用

    c++ 11 正则表达式 常用的方法 regex_match regex_search regex_replace 等. regex_match 要求正则表达式必须与模式串完全匹配,例如: strin ...

  10. 【项目经验】之——Controller向View传值

    我们的ITOO进行了一大部分了,整体上来说还是比较顺利的.昨天进行了一次验收,大体上来说,我们新生这块还是可以的.不仅仅进行了学术上的交流,还进行了需求上的更新.也正是由于这一次,我有了解到了一个新的 ...