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. python中tornado的第一个例子

    1  先安装tornado pip install tornado 2 新建tor.py 记住不能建立 tornado.py 这样的名字  不然会报错 ImportError: No module n ...

  2. Mathtype 公式显示方框

    公式编辑器mathtype中一些符号显示方框,如何解决呢?出现这个问题的原因是这是因为windows中的mtextra.ttf(显示为MT Extra (TrueType))字体文件不存在或版本太低, ...

  3. delphi 触摸 手势

    delphi手势,左右滑动, 控件的OnGesture事件写代码. 放一个TGestureManager控件,设置控件的touch属性为TGestureManager控件. 然后勾选控件的Touch& ...

  4. $(window).scrollTop() 获取当前的鼠标位置 offset.left()指定标签在html中的坐标 offset.top() 指定标签在html中的坐标position() 指定标签相对父(relative)标签的坐标

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. docker redis

    https://www.cnblogs.com/cgpei/p/7151612.html 重启docker >systmctl restart docker >mkdir -p ~/red ...

  6. [leetcode]141. Linked List Cycle判断链表是否有环

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  7. oracle触发器--if else demo

    CREATE OR REPLACE Trigger trig_solr_index_el_lesson After Update of lessonid, lessonname, lessongoal ...

  8. Qt5 How to translate App UI languages

    Adding new language file name in app.pro file. TRANSLATIONS += lg_ch.ts \ lg_en.ts \ lg_new.ts Runni ...

  9. mybatis逆向工程的注意事项:mapper文件内容不是覆盖而是追加

    XXXMapper.xml文件已经存在时,如果进行重新生成则mapper.xml文件内容不被覆盖而是进行内容追加,结果导致mybatis解析失败. 解决方法:删除原来已经生成的mapper xml文件 ...

  10. [Selenium]Turn Page By Scroll Bar

    Description: Need to turn page by operating scroll bar and find out the element in the current page. ...