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

4 6
1 4
2 4
3 4
P 2 3
P 1 3
Q 3 4
P 1 4
Q 2 4
Q 1 4

Sample Output

2
1
2

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

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

答案是否定的!!

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

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

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

代码:

 #include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
const int N=;
int n,m,cnt,now,pre[N],f[N],nxt[N],h[N],top[N],id[N],size[N],dep[N],sum;
struct oo{int a,b,v,lazy;}s[N*];
char p[];
void dfs(int x)
{
size[x]=;
for(int i=h[x];i;i=nxt[i])
{
if(pre[i]==f[x])continue;
dep[pre[i]]=dep[x]+;
f[pre[i]]=x;
dfs(pre[i]);
size[x]+=size[pre[i]];
}
}
void dfs2(int x,int f)
{
int k=;
id[x]=++cnt;
top[x]=f;
for(int i=h[x];i;i=nxt[i])
if(size[pre[i]]>size[k]&&dep[pre[i]]>dep[x])k=pre[i];
if(!k)return ;
dfs2(k,f);
for(int i=h[x];i;i=nxt[i])
if(dep[pre[i]]>dep[x]&&pre[i]!=k)
dfs2(pre[i],pre[i]);
}
void ins(int x,int y)
{
pre[++now]=y;
nxt[now]=h[x];
h[x]=now;
}
void build(int x,int l,int r)
{
s[x].a=l,s[x].b=r;
if(l==r)return ;
build(x<<,l,l+r>>);
build(x<<|,(l+r>>)+,r);
}
void pushdown(int x)
{
int l=x<<,r=x<<|;
s[l].lazy+=s[x].lazy,s[r].lazy+=s[x].lazy;
s[l].v+=s[x].lazy*(s[l].b-s[l].a+);
s[r].v+=s[x].lazy*(s[r].b-s[r].a+);
s[x].lazy=;
}
void get(int x,int l,int r)
{
if(s[x].lazy)pushdown(x);
if(s[x].a>=l&&r>=s[x].b)
sum+=s[x].v;
else
{
int mid=s[x].a+s[x].b>>;
if(l<=mid)get(x<<,l,r);
if(r>mid)get(x<<|,l,r);
}
}
void qsum(int x,int y)
{
sum=;
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]])swap(x,y);
get(,id[top[x]],id[x]);
x=f[top[x]];
}
if(id[x]>id[y])swap(x,y);
get(,id[x]+,id[y]);
}
void change(int x,int l,int r)
{
if(s[x].lazy)pushdown(x);
if(l<=s[x].a&&r>=s[x].b)
{
s[x].v+=s[x].b-s[x].a+;
s[x].lazy++;
return ;
}
int mid=s[x].a+s[x].b>>;
if(l<=mid)change(x<<,l,r);
if(r>mid)change(x<<|,l,r);
s[x].v=s[x<<].v+s[x<<|].v;
}
void qchange(int x,int y)
{
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]])swap(x,y);
change(,id[top[x]],id[x]);
x=f[top[x]];
}
if(id[x]>id[y])swap(x,y);
change(,id[x]+,id[y]);
}
int main()
{
scanf("%d",&n);
scanf("%d",&m);
for(int i=,x,y;i<n;i++)
{
scanf("%d%d",&x,&y);
ins(x,y);ins(y,x);
}
dfs();dfs2(,);
build(,,n);
for(int i=,x,y;i<=m;i++)
{
scanf("%s%d%d",p+,&x,&y);
if(p[]=='Q')
{
qsum(x,y);
printf("%d\n",sum);
}
if(p[]=='P')qchange(x,y);
}
}

[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. wireshark 学习 1

    wireshark 调试WiFi 安装之后的启动脚步. #!/bin/bash iw dev mon0 del iw dev wlp3s0 interface add mon0 type monito ...

  2. (linux)struct inode 和 struct file

    转自:http://www.cnblogs.com/QJohnson/archive/2011/06/24/2089414.html 1.struct inode──字符设备驱动相关的重要结构介绍 内 ...

  3. 解决访问google的问题

    我用的是360安全浏览器. 1.点击扩展,搜索goole 2.可以看到第一个就是谷歌访问助手 3.下载安装 4.点击永久激活之后它会提示你怎么做 5.大概是让你修改主页,修改主页是在选项里,第一个基本 ...

  4. CSU - 1551 Longest Increasing Subsequence Again —— 线段树/树状数组 + 前缀和&后缀和

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1551 题意: 给出一段序列, 删除其中一段连续的子序列(或者不删), 使得剩下的序列 ...

  5. Zookeeper实现负载均衡原理

    先玩个正常的,好玩的socket编程: 服务端: 首先公共的这个Handler: package com.toov5.zkDubbo; import java.io.BufferedReader; i ...

  6. Java 8新特性之旅:使用Stream API处理集合

    在这篇“Java 8新特性教程”系列文章中,我们会深入解释,并通过代码来展示,如何通过流来遍历集合,如何从集合和数组来创建流,以及怎么聚合流的值. 在之前的文章“遍历.过滤.处理集合及使用Lambda ...

  7. linux系统配置之网络配置(centos)

    CentOS---网络配置详解 一.配置文件详解在RHEL或者CentOS等Redhat系的Linux系统里,跟网络有关的主要设置文件如下: /etc/host.conf         配置域名服务 ...

  8. slim.flatten——将输入扁平化但保留batch_size,假设第一维是batch

    slim.flatten(inputs,outputs_collections=None,scope=None) (注:import tensorflow.contrib.slim as slim) ...

  9. BeginPaint/EndPaint(CPaintDC)与GetDC(CClientDC)的区别

    在OnPaint函数中,用CClientDC dc(this)代替CPaintDC(this)后,界面不断闪烁. 说明:CClientDC是对GetDC的使用封装, CPaintDC是对BeginPa ...

  10. GC及其作用

    Java GC 是垃圾回收机制,自动内存管理和垃圾清扫机制,释放内存中的资源和垃圾