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. 几种查询方法(lambda Linq Enumerable静态类方式)

    1.需要一个数据源类: using System; using System.Collections.Generic; namespace Linq { public class Student { ...

  2. 【C#】串口操作实用类

    做工业通 信有很长时间了,特别是串口(232/485),有VB/VC/C各种版本的串口操作代码,这些代码也经过了多年的现场考验,应该说是比较健壮的代码,但 是目前却没有C#相对成熟的串口操作代码,最近 ...

  3. 基础 ByteBuffer 和 ByteBuf

    缓冲区 ByteBuffer buffer = ByteBuffer.allocate(); ByteBuf https://www.jianshu.com/p/3fbf54b8e8ec

  4. 15 并发编程-(IO模型)

    一.IO模型介绍 1.阻塞与非阻塞指的是程序的两种运行状态 阻塞:遇到IO就发生阻塞,程序一旦遇到阻塞操作就会停在原地,并且立刻释放CPU资源 非阻塞(就绪态或运行态):没有遇到IO操作,或者通过某种 ...

  5. 小学生作业V2.0

    211606320刘佳&211506332熊哲琛 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Plann ...

  6. for 续10

    ---------siwuxie095                   for 帮助信息:                                                     ...

  7. How to use mouse to moving windows of not have title bar?

    How to use mouse to moving windows of not have title bar? #include "widget.h" #include < ...

  8. smarty foreach

    <{foreach from=$data item=val }> <tr align="center"> <td><{$val.item_ ...

  9. qt4.8转qt5.4

    1.头文件包含    #include <QtGui/QProgressBar>    #include <QtGui/QProgressDialog>    #include ...

  10. SQLServer获取临时表列名并判断指定列名是否存在

    if(OBJECT_ID('tempdb.dbo.#tempTB') is not null)begin drop table #tempTB;end create table #tempTB(ID ...