Tree

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 13156   Accepted: 3358

题目链接

http://poj.org/problem?id=3237

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

题解

树练剖分的模板题,单点修改,区间查询,还有区间取反(就是变成相反数。。。)。

注意一下单点的lazy标记处理就好了,我是每个区间权值由子区间和lazy标记组成,

然后叶子节点lazy区间用一次就消失。

代码

 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define ll long long
#define N 100050
#define INF 0x7f7f7f7f
int n,bh[N],w[N];
struct Tree{int l,r,lazy,max,min;}tr[N<<];
struct Edge{int from,to,val,id,s;}edges[N<<];
int tot,last[N];
int cnt,fa[N],size[N],dp[N],son[N],rk[N],kth[N],top[N]; template<typename T>void read(T&x)
{
ll k=; char c=getchar();
x=;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit();
while(isdigit(c))x=x*+c-'',c=getchar();
x=k?-x:x;
}
void AddEdge(int x,int y,int z,int id)
{
edges[++tot]=Edge{x,y,z,id,last[x]};
last[x]=tot;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
void push_up(int x)
{
int len=(tr[x].r-tr[x].l+);
if (len>)
{
tr[x].max=max(tr[x<<].max,tr[x<<|].max);
tr[x].min=min(tr[x<<].min,tr[x<<|].min);
}
if (tr[x].lazy==-)
{
tr[x].max*=-;
tr[x].min*=-;
swap(tr[x].max,tr[x].min);
}
if (len==)tr[x].lazy=;
}
void push_down(int x)
{
tr[x<<].lazy*=tr[x].lazy;
tr[x<<|].lazy*=tr[x].lazy;
push_up(x<<);
push_up(x<<|);
tr[x].lazy=;
}
void bt(int x,int l,int r)
{
tr[x].l=l; tr[x].r=r; tr[x].lazy=;
if (l==r)
{
tr[x].max=tr[x].min=w[kth[l]];
return;
}
int mid=(l+r)>>;
bt(x<<,l,mid);
bt(x<<|,mid+,r);
push_up(x);
}
void update(int x,int p,int tt)
{
if (p<=tr[x].l&&tr[x].r<=p)
{
tr[x].max=tr[x].min=tt;
tr[x].lazy=;
return;
}
int mid=(tr[x].l+tr[x].r)>>;
push_down(x);
if (p<=mid)update(x<<,p,tt);
if (mid<p)update(x<<|,p,tt);
push_up(x);
}
int neg(int x,int l,int r,int tt)
{
if (l<=tr[x].l&&tr[x].r<=r)
{
tr[x].lazy*=tt;
push_up(x);
return tr[x].max;
}
int mid=(tr[x].l+tr[x].r)>>,ans=-INF;
push_down(x);
if (l<=mid)ans=max(ans,neg(x<<,l,r,tt));
if (mid<r)ans=max(ans,neg(x<<|,l,r,tt));
push_up(x);
return ans;
}
void dfs1(int x,int pre)
{
fa[x]=pre;
dp[x]=dp[pre]+;
size[x]=;
son[x]=;
for(int i=last[x];i;i=edges[i].s)
{
Edge &e=edges[i];
if (e.to==pre)continue;
w[e.to]=e.val;
bh[e.id]=e.to;
dfs1(e.to,x);
size[x]+=size[e.to];
if (size[e.to]>size[son[x]])son[x]=e.to;
}
}
void dfs2(int x,int y)
{
rk[x]=++cnt;
kth[cnt]=x;
top[x]=y;
if (son[x]==)return;
dfs2(son[x],y);
for(int i=last[x];i;i=edges[i].s)
{
Edge &e=edges[i];
if (e.to==fa[x]||e.to==son[x])continue;
dfs2(e.to,e.to);
}
}
int get_max(int x,int y,int tt)
{
int fx=top[x],fy=top[y],ans=-INF;
while(fx!=fy)
{
if (dp[fx]<dp[fy])swap(x,y),swap(fx,fy);
ans=max(ans,neg(,rk[fx],rk[x],tt));
x=fa[fx];fx=top[x];
}
if (dp[x]<dp[y])swap(x,y);
ans=max(ans,neg(,rk[y]+,rk[x],tt));
return ans;
}
void work()
{
read(n);
for(int i=;i<=n-;i++)
{
int x,y,z;
read(x); read(y); read(z);
AddEdge(x,y,z,i);
AddEdge(y,x,z,i);
}
dfs1(,);
dfs2(,);
bt(,,n);
while()
{
char id; int x,y;
read_char(id);
if (id=='D') break;
read(x); read(y);
if (id=='C') update(,rk[bh[x]],y);
if (id=='N') get_max(x,y,-);
if (id=='Q') printf("%d\n",get_max(x,y,));
}
}
void clear()
{
cnt=; tot=;
memset(last,,sizeof(last));
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
int q;
read(q);
while(q--)
{
clear();
work();
}
}

[POJ-3237] [Problem E]的更多相关文章

  1. POJ 3237:Tree(树链剖分)

    http://poj.org/problem?id=3237 题意:树链剖分.操作有三种:改变一条边的边权,将 a 到 b 的每条边的边权都翻转(即 w[i] = -w[i]),询问 a 到 b 的最 ...

  2. poj 3237 Tree 树链剖分

    题目链接:http://poj.org/problem?id=3237 You are given a tree with N nodes. The tree’s nodes are numbered ...

  3. POJ 3237 Tree (树链剖分 路径剖分 线段树的lazy标记)

    题目链接:http://poj.org/problem?id=3237 一棵有边权的树,有3种操作. 树链剖分+线段树lazy标记.lazy为0表示没更新区间或者区间更新了2的倍数次,1表示为更新,每 ...

  4. ●POJ 3237 Tree

    题链: http://poj.org/problem?id=3237 题解: LCT 说一说如何完成询问操作就好了(把一条链的边权变成相反数的操作可以类比着来): 首先明确一下,我们把边权下放到点上. ...

  5. POJ 3237 树链剖分

    题目链接:http://poj.org/problem?id=3237 题意:给定一棵n个结点n-1条边的树. 每条边都是一个边权. 现在有4种操作 1:CHANGE I V:把(输入的)第i条边的边 ...

  6. HDU 3966 & POJ 3237 & HYSBZ 2243 树链剖分

    树链剖分是一个很固定的套路 一般用来解决树上两点之间的路径更改与查询 思想是将一棵树分成不想交的几条链 并且由于dfs的顺序性 给每条链上的点或边标的号必定是连着的 那么每两个点之间的路径都可以拆成几 ...

  7. poj 3237 Tree [LCA] (树链剖分)

    poj 3237 tree inline : 1. inline 定义的类的内联函数,函数的代码被放入符号表中,在使用时直接进行替换,(像宏一样展开),没有了调用的开销,效率也很高. 2. 很明显,类 ...

  8. poj 1651 http://poj.org/problem?id=1651

      http://poj.org/problem?id=1651Multiplication Puzzle   Time Limit: 1000MS   Memory Limit: 65536K To ...

  9. poj-3056 http://poj.org/problem?id=3056

    http://poj.org/problem?id=3056 The Bavarian Beer Party Time Limit: 6000MS   Memory Limit: 65536K Tot ...

  10. poj 1679 http://poj.org/problem?id=1679

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

随机推荐

  1. [think\exception\ErrorException] glob() has been disabled for security reasons

    今天同事开发 出现了这个错误 [think\exception\ErrorException] glob() has been disabled for security reasons 打开php. ...

  2. Access空字符串和Null值

    什么是空字符串和Null值: Microsoft Access可以区分两种类型的空值.因为在某些情况下,字段为空,可能是因为信息目前无法获得,或者字段不适用于某一特定的记录.例如,表中有一个“电话号码 ...

  3. Maven(三)理解Maven核心概念

    转载自: http://www.cnblogs.com/holbrook/archive/2012/12/24/2830519.html 本文以类图的方式,介绍maven核心的12个概念以及相互之间的 ...

  4. UNITY WWW使用代码

    string detailURL = "https://www.xxx.xxx."; using (var w = new WWW(detailURL)) { yield retu ...

  5. php解决时间超过2038年

    问题 超过2038年的时间 php怎么处理? echo date('Y-m-d',2147483647); //date函数能处理的最大整数2147483647 ->2038-01-19 就是2 ...

  6. httpwebrequest详解

    HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最好选择.它们支持一系列有用的属性.这两个类位 于System.Net命名空间,默认情况下这个类对于控制台程 ...

  7. unix时间戳与时间

    [root@pserver ~]# date -d "@1381371010" Thu Oct :: CST [root@pserver ~]# date --date=" ...

  8. YUI前端优化之内容篇

    Excetional Performance团队总结出了一系列可以提高网站速度的方法.可以分为7大类34条.包括内容.服务器.cookie.CSS.JavaScript.图片.移动应用等七部分.一.内 ...

  9. [Java] Java API文档下载方法

    Java API文档下载方法:http://jingyan.baidu.com/article/a3aad71ac9e48fb1fb009692.html Oracle : http://www.or ...

  10. POJ 3057 Evacuation (二分匹配)

    题意:给定一个图,然后有几个门,每个人要出去,但是每个门每个秒只能出去一个,然后问你最少时间才能全部出去. 析:初一看,应该是像搜索,但是怎么保证每个人出去的时候都不冲突呢,毕竟每个门每次只能出一个人 ...