Description

Farmer John has N barren pastures connected by N-1 bidirectional roads, such that there is exactly o
ne path between any two pastures. Bessie, a cow who loves her grazing time, often complains about ho
w there is no grass on the roads between pastures. Farmer John loves Bessie very much, and today he 
is finally going to plant grass on the roads. He will do so using a procedure consisting of M steps 
.
At each step one of two things will happen:
- FJ will choose two pastures, and plant a patch of grass along each road in between the two pastures, or,
- Bessie will ask about how many patches of grass on a particular road, and Farmer John must answer her question.
Farmer John is a very poor counter -- help him answer Bessie's questions!
大致题意: 维护一棵树,支持两种操作:
P x y x到y路径上的每条边的值+1;
Q x y 询问x到y路径上所有边的值的和。

Input

Line 1: Two space-separated integers N and M.
Lines 2..N: Two space-separated integers describing the endpoints of a road.
Lines N+1..N+M: Line i+1 describes step i. The first character of the line is either P or Q, which d
escribes whether or not FJ is planting grass or simply querying. This is followed by two space-separ
ated integers A_i and B_i (1 ≤A_i, B_i ≤N) which describe FJ's action or query.
第一行两个正整数,N,M表示点数和操作数;
接下来N-1行每行两个数表示一条边;
接下来M行表示M个操作,每行形如P x y或Q x y。
2≤N≤100,000,1≤M≤100,000。

Output

Lines 1..???: Each line has the answer to a query, appearing in the same order as the queries appear in the input.
M行,对应相应询问的答案。

Sample Input

  1. 4 6
  2. 1 4
  3. 2 4
  4. 3 4
  5. P 2 3
  6. P 1 3
  7. Q 3 4
  8. P 1 4
  9. Q 2 4
  10. Q 1 4

Sample Output

  1. 2
  2. 1
  3. 2

来写此题的一定都是学过树剖的了吧。

这道题用到了线段树的区间修改,难道真的只要在单点修改的情况下在线段树中使用lazy标记来进行区间修改就行了么

答案是否定的!!

你或许能过样例,但是肯定是红色的wa

我们知道树剖要不断的去跳重链,如果你要修改的区间不在一条重链上,那就会出错哦(样例很坑哦)

所以需要在加lazy标记的同时再不断地跳重链

代码:

  1. #include<cstdio>
  2. #include<algorithm>
  3. #include<iostream>
  4. using namespace std;
  5. const int N=;
  6. int n,m,cnt,now,pre[N],f[N],nxt[N],h[N],top[N],id[N],size[N],dep[N],sum;
  7. struct oo{int a,b,v,lazy;}s[N*];
  8. char p[];
  9. void dfs(int x)
  10. {
  11. size[x]=;
  12. for(int i=h[x];i;i=nxt[i])
  13. {
  14. if(pre[i]==f[x])continue;
  15. dep[pre[i]]=dep[x]+;
  16. f[pre[i]]=x;
  17. dfs(pre[i]);
  18. size[x]+=size[pre[i]];
  19. }
  20. }
  21. void dfs2(int x,int f)
  22. {
  23. int k=;
  24. id[x]=++cnt;
  25. top[x]=f;
  26. for(int i=h[x];i;i=nxt[i])
  27. if(size[pre[i]]>size[k]&&dep[pre[i]]>dep[x])k=pre[i];
  28. if(!k)return ;
  29. dfs2(k,f);
  30. for(int i=h[x];i;i=nxt[i])
  31. if(dep[pre[i]]>dep[x]&&pre[i]!=k)
  32. dfs2(pre[i],pre[i]);
  33. }
  34. void ins(int x,int y)
  35. {
  36. pre[++now]=y;
  37. nxt[now]=h[x];
  38. h[x]=now;
  39. }
  40. void build(int x,int l,int r)
  41. {
  42. s[x].a=l,s[x].b=r;
  43. if(l==r)return ;
  44. build(x<<,l,l+r>>);
  45. build(x<<|,(l+r>>)+,r);
  46. }
  47. void pushdown(int x)
  48. {
  49. int l=x<<,r=x<<|;
  50. s[l].lazy+=s[x].lazy,s[r].lazy+=s[x].lazy;
  51. s[l].v+=s[x].lazy*(s[l].b-s[l].a+);
  52. s[r].v+=s[x].lazy*(s[r].b-s[r].a+);
  53. s[x].lazy=;
  54. }
  55. void get(int x,int l,int r)
  56. {
  57. if(s[x].lazy)pushdown(x);
  58. if(s[x].a>=l&&r>=s[x].b)
  59. sum+=s[x].v;
  60. else
  61. {
  62. int mid=s[x].a+s[x].b>>;
  63. if(l<=mid)get(x<<,l,r);
  64. if(r>mid)get(x<<|,l,r);
  65. }
  66. }
  67. void qsum(int x,int y)
  68. {
  69. sum=;
  70. while(top[x]!=top[y])
  71. {
  72. if(dep[top[x]]<dep[top[y]])swap(x,y);
  73. get(,id[top[x]],id[x]);
  74. x=f[top[x]];
  75. }
  76. if(id[x]>id[y])swap(x,y);
  77. get(,id[x]+,id[y]);
  78. }
  79. void change(int x,int l,int r)
  80. {
  81. if(s[x].lazy)pushdown(x);
  82. if(l<=s[x].a&&r>=s[x].b)
  83. {
  84. s[x].v+=s[x].b-s[x].a+;
  85. s[x].lazy++;
  86. return ;
  87. }
  88. int mid=s[x].a+s[x].b>>;
  89. if(l<=mid)change(x<<,l,r);
  90. if(r>mid)change(x<<|,l,r);
  91. s[x].v=s[x<<].v+s[x<<|].v;
  92. }
  93. void qchange(int x,int y)
  94. {
  95. while(top[x]!=top[y])
  96. {
  97. if(dep[top[x]]<dep[top[y]])swap(x,y);
  98. change(,id[top[x]],id[x]);
  99. x=f[top[x]];
  100. }
  101. if(id[x]>id[y])swap(x,y);
  102. change(,id[x]+,id[y]);
  103. }
  104. int main()
  105. {
  106. scanf("%d",&n);
  107. scanf("%d",&m);
  108. for(int i=,x,y;i<n;i++)
  109. {
  110. scanf("%d%d",&x,&y);
  111. ins(x,y);ins(y,x);
  112. }
  113. dfs();dfs2(,);
  114. build(,,n);
  115. for(int i=,x,y;i<=m;i++)
  116. {
  117. scanf("%s%d%d",p+,&x,&y);
  118. if(p[]=='Q')
  119. {
  120. qsum(x,y);
  121. printf("%d\n",sum);
  122. }
  123. if(p[]=='P')qchange(x,y);
  124. }
  125. }

[Usaco2011 Dec]Grass Planting的更多相关文章

  1. USACO Grass Planting

    洛谷 P3038 [USACO11DEC]牧草种植Grass Planting 洛谷传送门 JDOJ 2282: USACO 2011 Dec Gold 3.Grass Planting JDOJ传送 ...

  2. spoj - Grass Planting(树链剖分模板题)

    Grass Planting 题意 给出一棵树,树有边权.每次给出节点 (u, v) ,有两种操作:1. 把 u 到 v 路径上所有边的权值加 1.2. 查询 u 到 v 的权值之和. 分析 如果这些 ...

  3. 洛谷P3038 [USACO11DEC]牧草种植Grass Planting

    题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...

  4. [USACO11DEC] Grass Planting (树链剖分)

    题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...

  5. AC日记——[USACO11DEC]牧草种植Grass Planting 洛谷 P3038

    题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...

  6. 洛谷 P3038 [USACO11DEC]牧草种植Grass Planting

    题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...

  7. P3038 [USACO11DEC]牧草种植Grass Planting

    题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...

  8. 【 SPOJ - GRASSPLA】 Grass Planting (树链剖分+树状数组)

    54  种草约翰有 N 个牧场,编号为 1 到 N.它们之间有 N − 1 条道路,每条道路连接两个牧场.通过这些道路,所有牧场都是连通的.刚开始的时候,所有道路都是光秃秃的,没有青草.约翰会在一些道 ...

  9. 洛谷 P3038 [USACO11DEC]牧草种植Grass Planting(树链剖分)

    题解:仍然是无脑树剖,要注意一下边权,然而这种没有初始边权的题目其实和点权也没什么区别了 代码如下: #include<cstdio> #include<vector> #in ...

随机推荐

  1. Qt中的打印操作

    Qt中对打印的支持是有一个独立的printsupport模块来完成的,所以,要想在程序中使用Qt的打印功能,必须先在pro文件中添加下面这句代码: QT += printsupport在这个模块中,提 ...

  2. 20170319 ABAP 生成XML文件

    方法一:ABAP 使用method方式操作XML 转自:http://www.cnblogs.com/jiangzhengjun/p/4265595.html 方法二:STRANS 转换工具;使用st ...

  3. 【LeetCode】Insertion Sort List

    Sort a linked list using insertion sort. //用到O(N)的额外空间 public class Solution { public ListNode inser ...

  4. 如何克隆UBUNTU14.04LTS

    先对目标盘sdb做好处理,分区,格式化,挂载等操作sudo fdisk /dev/sdb1fdisk常用命令如下,m是帮助,n创建新分区,d删除分区,w保存退出.分好区后,对sdb1进行格式化和挂载: ...

  5. spring boot 整合 quartz 集群环境 实现 动态定时任务配置【原】

    最近做了一个spring boot 整合 quartz  实现 动态定时任务配置,在集群环境下运行的 任务.能够对定时任务,动态的进行增删改查,界面效果图如下: 1. 在项目中引入jar 2. 将需要 ...

  6. CORS 理解(不要那么多术语)

    摘要 谈到跨域,不论前端还是后端,多少有点谈虎色变,面试中也常会问到这些问题,浏览器和服务器端到底怎么做才能跨域,他们都做了什么? 同源 vs 跨域 同源,字面意义是相同的源头,即同一个web服务器( ...

  7. Codeforces Round #364 (Div. 1)(vp) 没什么题解就留坑待填

    我就做了前两题,第一题第一次vp就把我搞自闭跑路了,第二题第二次又把我搞自闭了 A. As Fast As Possible 细节题 #include<cstdio> #include&l ...

  8. 为 Android 平台开发一个输入法

    学习目标: 实现新的输入法 学习目的: 掌握Android输入法框架 学习收获: Android 1.5 新特色之一就是输入法框架(Input Method Framework,IMF),正是它的出现 ...

  9. reactjs的一些笔记

    1.使用虚拟DOM作为其不同的实现.同时可以由服务器node.js渲染,从而不需要过重的浏览器DOM支持.   2.虚拟DOM:在浏览器端用javascript实现了一套DOM API.用react开 ...

  10. bootstrap 学习笔记(4)---- 按钮

    平常我们自己写按钮,这次不用我们自己写 了,直接应用bootstrap中的按钮样式,就能设计出很漂亮的按钮样式.接下来就让我们一起学习吧. 1.可以作为按钮使用的标签或元素:<a>< ...