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区间用一次就消失。

代码

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<algorithm>
  4 #include<cstring>
  5 using namespace std;
  6 #define ll long long
  7 #define N 100050
  8 #define INF 0x7f7f7f7f
  9 int n,bh[N],w[N];
 10 struct Tree{int l,r,lazy,max,min;}tr[N<<2];
 11 struct Edge{int from,to,val,id,s;}edges[N<<1];
 12 int tot,last[N];
 13 int cnt,fa[N],size[N],dp[N],son[N],rk[N],kth[N],top[N];
 14
 15 template<typename T>void read(T&x)
 16 {
 17   ll k=0; char c=getchar();
 18   x=0;
 19   while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
 20   if (c==EOF)exit(0);
 21   while(isdigit(c))x=x*10+c-'0',c=getchar();
 22   x=k?-x:x;
 23 }
 24 void AddEdge(int x,int y,int z,int id)
 25 {
 26   edges[++tot]=Edge{x,y,z,id,last[x]};
 27   last[x]=tot;
 28 }
 29 void read_char(char &c)
 30 {while(!isalpha(c=getchar())&&c!=EOF);}
 31 void push_up(int x)
 32 {
 33   int len=(tr[x].r-tr[x].l+1);
 34   if (len>1)
 35     {
 36       tr[x].max=max(tr[x<<1].max,tr[x<<1|1].max);
 37       tr[x].min=min(tr[x<<1].min,tr[x<<1|1].min);
 38     }
 39   if (tr[x].lazy==-1)
 40     {
 41       tr[x].max*=-1;
 42       tr[x].min*=-1;
 43       swap(tr[x].max,tr[x].min);
 44     }
 45   if (len==1)tr[x].lazy=1;
 46 }
 47 void push_down(int x)
 48 {
 49   tr[x<<1].lazy*=tr[x].lazy;
 50   tr[x<<1|1].lazy*=tr[x].lazy;
 51   push_up(x<<1);
 52   push_up(x<<1|1);
 53   tr[x].lazy=1;
 54 }
 55 void bt(int x,int l,int r)
 56 {
 57   tr[x].l=l; tr[x].r=r; tr[x].lazy=1;
 58   if (l==r)
 59     {
 60       tr[x].max=tr[x].min=w[kth[l]];
 61       return;
 62     }
 63   int mid=(l+r)>>1;
 64   bt(x<<1,l,mid);
 65   bt(x<<1|1,mid+1,r);
 66   push_up(x);
 67 }
 68 void update(int x,int p,int tt)
 69 {
 70   if (p<=tr[x].l&&tr[x].r<=p)
 71     {
 72       tr[x].max=tr[x].min=tt;
 73       tr[x].lazy=1;
 74       return;
 75     }
 76   int mid=(tr[x].l+tr[x].r)>>1;
 77   push_down(x);
 78   if (p<=mid)update(x<<1,p,tt);
 79   if (mid<p)update(x<<1|1,p,tt);
 80   push_up(x);
 81 }
 82 int neg(int x,int l,int r,int tt)
 83 {
 84   if (l<=tr[x].l&&tr[x].r<=r)
 85     {
 86       tr[x].lazy*=tt;
 87       push_up(x);
 88       return tr[x].max;
 89     }
 90   int mid=(tr[x].l+tr[x].r)>>1,ans=-INF;
 91   push_down(x);
 92   if (l<=mid)ans=max(ans,neg(x<<1,l,r,tt));
 93   if (mid<r)ans=max(ans,neg(x<<1|1,l,r,tt));
 94   push_up(x);
 95   return ans;
 96 }
 97 void dfs1(int x,int pre)
 98 {
 99   fa[x]=pre;
100   dp[x]=dp[pre]+1;
101   size[x]=1;
102   son[x]=0;
103   for(int i=last[x];i;i=edges[i].s)
104     {
105       Edge &e=edges[i];
106       if (e.to==pre)continue;
107       w[e.to]=e.val;
108       bh[e.id]=e.to;
109       dfs1(e.to,x);
110       size[x]+=size[e.to];
111       if (size[e.to]>size[son[x]])son[x]=e.to;
112     }
113 }
114 void dfs2(int x,int y)
115 {
116   rk[x]=++cnt;
117   kth[cnt]=x;
118   top[x]=y;
119   if (son[x]==0)return;
120   dfs2(son[x],y);
121   for(int i=last[x];i;i=edges[i].s)
122     {
123       Edge &e=edges[i];
124       if (e.to==fa[x]||e.to==son[x])continue;
125       dfs2(e.to,e.to);
126     }
127 }
128 int get_max(int x,int y,int tt)
129 {
130   int fx=top[x],fy=top[y],ans=-INF;
131   while(fx!=fy)
132     {
133       if (dp[fx]<dp[fy])swap(x,y),swap(fx,fy);
134       ans=max(ans,neg(1,rk[fx],rk[x],tt));
135       x=fa[fx];fx=top[x];
136     }
137   if (dp[x]<dp[y])swap(x,y);
138   ans=max(ans,neg(1,rk[y]+1,rk[x],tt));
139   return ans;
140 }
141 void work()
142 {
143   read(n);
144   for(int i=1;i<=n-1;i++)
145     {
146       int x,y,z;
147       read(x); read(y); read(z);
148       AddEdge(x,y,z,i);
149       AddEdge(y,x,z,i);
150     }
151   dfs1(1,0);
152   dfs2(1,1);
153   bt(1,1,n);
154   while(1)
155     {
156       char id; int x,y;
157       read_char(id);
158       if (id=='D') break;
159       read(x); read(y);
160       if (id=='C') update(1,rk[bh[x]],y);
161       if (id=='N') get_max(x,y,-1);
162       if (id=='Q') printf("%d\n",get_max(x,y,1));
163     }
164 }
165 void clear()
166   {
167     cnt=0; tot=0;
168     memset(last,0,sizeof(last));
169   }
170 int main()
171 {
172 #ifndef ONLINE_JUDGE
173   freopen("aa.in","r",stdin);
174 #endif
175   int q;
176   read(q);
177   while(q--)
178     {
179       clear();
180       work();
181     }
182 }

View Code

转载于:https://www.cnblogs.com/mmmqqdd/p/10768265.html

[POJ-3237] [Problem E]相关推荐

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

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

  2. HDU 3966 POJ 3237 HYSBZ 2243 HRBUST 2064 树链剖分

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

  3. POJ 3237 Tree (树链剖分)

    Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 2825   Accepted: 769 Description ...

  4. POJ 3237.Tree -树链剖分(边权)(边值更新、路径边权最值、区间标记)贴个板子备忘...

    Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 12247   Accepted: 3151 Descriptio ...

  5. POJ 3265 Problem Solving 动态规划

    Problem Solving Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1914   Accepted: 747 De ...

  6. POJ 3237 树链剖分学习(树链剖分小结)

    一个地方wa了3发,找了一组数组发现的错误,太蠢,就是两个人数作交换写成了 a=b ,b=a.最近智商真是感人.这道题是由spoj375这题改编的,多了一个取反的操作,这个操作只需要多维护一个最小值就 ...

  7. 北大OJ(POJ 3237)鸡兔同笼

    描述 一个笼子里面关了鸡和兔子(鸡有2只脚,兔子有4只脚,没有例外).已经知道了笼子里面脚的总数a,问笼子里面至少有多少只动物,至多有多少只动物 输入 第1行是测试数据的组数n,后面跟着n行输入.每组 ...

  8. POJ 3265 Problem Solving

    题意:P个问题,雇佣相同的人去解决,每个人每月解决一道题,每个人解决问题的代价都分两次,解决问题当月给a[i],事后第二月给b[i],然后每个月有m的钱,问最快多久解决所有问题.(问题必须按照序号一个 ...

  9. POJ 1207 The 3n + 1 problem

    题目链接:http://poj.org/problem?id=1207 题目大意:给你一个数x,规定一个函数F(x),如果x为1则F(x)==1,否则如果x是偶数,F(x)==F(x/2),x为奇数F ...

  10. poj 1681 Painter#39;s Problem(高斯消元)

    http://poj.org/problem? id=1681 求最少经过的步数使得输入的矩阵全变为y. 思路:高斯消元求出自由变元.然后枚举自由变元,求出最优值. 注意依据自由变元求其它解及求最优值 ...

最新文章

  1. 干货|2018年中国智能硬件行业现状与发展趋势报告
  2. pc站转手机站的首页(扫二维码)
  3. 通过forms来创建用户注册
  4. Spring Boot实践教程(二):SpringApplication分析
  5. 操作系统(八)进程管理——进程同步
  6. 结合DvaJS来写小程序
  7. Linux 搜索文件
  8. 【转】简单的解释XSS攻击
  9. angular分页插件tm.pagination 解决触发二次请求的问题
  10. 华为c语言中static的作用,C语言编程规范(华为、林锐、MISRAC).pdf
  11. Android: app不被系统kill掉
  12. 捷联惯导系统模型及仿真(三)
  13. 可靠产品设计的5项技术 02-识别和评估关键的可靠性风险
  14. 二分法求解函数零点的Python程序
  15. 网页特效——花间飞舞的蝴蝶
  16. 硅基生命之漫谈-1:天马行空
  17. mysql 上周时间_mysql 获取上周1到周日的时间
  18. Python的Module,Library,Package的区别
  19. 左边是地狱右边也是地狱_走出教程地狱
  20. Arduino开发:网页控制ESP8266三色LED灯闪烁

热门文章

  1. Python Numpy介绍
  2. springmvc 音频流输出_音频管理模块AudioDeviceModule解读
  3. 前端运行python代码几种方式_前的解释|前的意思|汉典“前”字的基本解释
  4. js设置div高度低于滚动高度时固定
  5. android java 给控件设置style,在Android Lollipop for Material Design中为SwitchCompat按钮设置样式/着色...
  6. 企业网络推广方法之网站内容链接SEO该怎么做?
  7. linux pid t 头文件_Linux信号处理
  8. linux c不占用cpu的延时,linux下写个C语言程序,要求有0.5微秒以下的延时,要怎样写...
  9. adb server version(31) doesnt match this client(41)
  10. intel python加速效果初探