[POJ-3237] [Problem E]
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]的更多相关文章
- POJ 3237:Tree(树链剖分)
http://poj.org/problem?id=3237 题意:树链剖分.操作有三种:改变一条边的边权,将 a 到 b 的每条边的边权都翻转(即 w[i] = -w[i]),询问 a 到 b 的最 ...
- poj 3237 Tree 树链剖分
题目链接:http://poj.org/problem?id=3237 You are given a tree with N nodes. The tree’s nodes are numbered ...
- POJ 3237 Tree (树链剖分 路径剖分 线段树的lazy标记)
题目链接:http://poj.org/problem?id=3237 一棵有边权的树,有3种操作. 树链剖分+线段树lazy标记.lazy为0表示没更新区间或者区间更新了2的倍数次,1表示为更新,每 ...
- ●POJ 3237 Tree
题链: http://poj.org/problem?id=3237 题解: LCT 说一说如何完成询问操作就好了(把一条链的边权变成相反数的操作可以类比着来): 首先明确一下,我们把边权下放到点上. ...
- POJ 3237 树链剖分
题目链接:http://poj.org/problem?id=3237 题意:给定一棵n个结点n-1条边的树. 每条边都是一个边权. 现在有4种操作 1:CHANGE I V:把(输入的)第i条边的边 ...
- HDU 3966 & POJ 3237 & HYSBZ 2243 树链剖分
树链剖分是一个很固定的套路 一般用来解决树上两点之间的路径更改与查询 思想是将一棵树分成不想交的几条链 并且由于dfs的顺序性 给每条链上的点或边标的号必定是连着的 那么每两个点之间的路径都可以拆成几 ...
- poj 3237 Tree [LCA] (树链剖分)
poj 3237 tree inline : 1. inline 定义的类的内联函数,函数的代码被放入符号表中,在使用时直接进行替换,(像宏一样展开),没有了调用的开销,效率也很高. 2. 很明显,类 ...
- poj 1651 http://poj.org/problem?id=1651
http://poj.org/problem?id=1651Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K To ...
- 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 ...
- 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 ...
随机推荐
- 让别人能登陆你的mysql
线上的数据库肯定是不能轻易在开发新功能的时候动的,如果你的数据库跟线上不一样了又没有新数据库的备份,就很麻烦. 当然去动线上数据库,出了什么问题我是不想背锅的. 最稳健的办法!让管理线上数据库的同学, ...
- 使用TCPDF输出完美的中文PDF文档
TCPDF是一个用于快速生成PDF文件的PHP5函数包.TCPDF基于FPDF进行扩展和改进.支持UTF-8,Unicode,HTML和XHTML.在基于PHP开发的Web应用中,使用它来输出PDF文 ...
- eclipse报Access restriction: The type 'BASE64Decoder' is not API处理方法
今天从svn更新代码之后,由于代码中使用了BASE64Encoder 更新之后报如下错误: Access restriction: The type ‘BASE64Decoder’ is not A ...
- js获取当前项目根路径URL (转自CSDN 红领巾-sunlight)
/** * //获取当前项目根路径 * @return {TypeName} */ function getRootPath(){ //获取当前网址,如: http://localhost:8083/ ...
- SpringMvc配置拦截器
SpringMVC可以通过配置拦截器,进行url过滤等处理. 在spring-mvc.xml的配置文件中,如下示: 其中,在<mvc:interceptors>中可以配置多个拦截器< ...
- Ubuntu解决sudo: source: command not found错误
Ubuntu Server上执行以下命令,可以看到默认打开的文件数限制为1024个. $ ulimit -n 1024 编辑/etc/profile配置文件,在最后添加一行: ulimit -SHn ...
- win2003 ent 64 + mssql ent 64
1.打win2003补丁(取消IE8.0)2.msconfig->boot.int->高级中,将内核改成83.将cd1,cd2,高到Servers,Tools目录中,并保证Servers, ...
- CodeForces 686A Free Ice Cream (水题模拟)
题意:给定初始数量的冰激凌,然后n个操作,如果是“+”,那么数量就会增加,如果是“-”,如果现有的数量大于等于要减的数量,那么就减掉,如果小于, 那么孩子就会离家.问你最后剩下多少冰激凌,和出走的孩子 ...
- Multi-Sensor, Multi- Network Positioning
Ruizhi Chen, Heidi Kuusniemi, Yuwei Chen, Ling Pei, Wei Chen, Jingbin Liu, Helena Leppäkoski, Jarmo ...
- mysql数据表简单拷贝及重命名
CREATE TABLE to LIKE from;//拷贝结构 RENAME TABLE from TO to;//重命名