题目描述

Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional roads, such that there is exactly one path between any two pastures. Bessie, a cow who loves her grazing time, often complains about how 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 (1 <= M <= 100,000).

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!

给出一棵n个节点的树,有m个操作,操作为将一条路径上的边权加一或询问某条边的权值。

输入输出格式

输入格式:

  • 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 describes whether or not FJ is planting grass or simply querying. This is followed by two space-separated integers A_i and B_i (1 <= A_i, B_i <= N) which describe FJ's action or query.

输出格式:

  • Lines 1..???: Each line has the answer to a query, appearing in the same order as the queries appear in the input.

输入输出样例

输入样例#1:

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
输出样例#1:

2
1
2 思路:
  裸树剖; 来,上代码:
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> #define maxn 100005 using namespace std; struct TreeNodeType {
int l,r,dis,mid,flag;
};
struct TreeNodeType tree[maxn<<]; struct EdgeType {
int to,next;
};
struct EdgeType edge[maxn<<]; int if_z,n,m,cnt,deep[maxn],f[maxn],size[maxn];
int top[maxn],id[maxn],head[maxn]; char Cget; inline void in(int &now)
{
now=,if_z=,Cget=getchar();
while(Cget>''||Cget<'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
now*=if_z;
} void search_1(int now,int fa)
{
int pos=cnt++;
deep[now]=deep[fa]+,f[now]=fa;
for(int i=head[now];i;i=edge[i].next)
{
if(edge[i].to==fa) continue;
search_1(edge[i].to,now);
}
size[now]=cnt-pos;
} void search_2(int now,int chain)
{
int pos=;
top[now]=chain,id[now]=++cnt;
for(int i=head[now];i;i=edge[i].next)
{
if(edge[i].to==f[now]) continue;
if(size[edge[i].to]>size[pos]) pos=edge[i].to;
}
if(pos==) return ;
search_2(pos,chain);
for(int i=head[now];i;i=edge[i].next)
{
if(edge[i].to==f[now]||edge[i].to==pos) continue;
search_2(edge[i].to,edge[i].to);
}
} void tree_build(int now,int l,int r)
{
tree[now].l=l,tree[now].r=r;
if(l==r) return ;
tree[now].mid=(l+r)>>;
tree_build(now<<,l,tree[now].mid);
tree_build(now<<|,tree[now].mid+,r);
} void tree_change(int now,int l,int r)
{
if(tree[now].l==l&&tree[now].r==r)
{
tree[now].dis+=r-l+;
tree[now].flag++;
return ;
}
if(tree[now].flag)
{
tree[now<<].dis+=tree[now].flag*(tree[now].mid-tree[now].l+);
tree[now<<|].dis+=tree[now].flag*(tree[now].r-tree[now].mid);
tree[now<<].flag+=tree[now].flag,tree[now<<|].flag+=tree[now].flag;
tree[now].flag=;
}
if(l>tree[now].mid) tree_change(now<<|,l,r);
else if(r<=tree[now].mid) tree_change(now<<,l,r);
else
{
tree_change(now<<,l,tree[now].mid);
tree_change(now<<|,tree[now].mid+,r);
}
tree[now].dis=tree[now<<].dis+tree[now<<|].dis;
} int tree_query(int now,int l,int r)
{
if(tree[now].l==l&&tree[now].r==r) return tree[now].dis;
if(tree[now].flag)
{
tree[now<<].dis+=tree[now].flag*(tree[now].mid-tree[now].l+);
tree[now<<|].dis+=tree[now].flag*(tree[now].r-tree[now].mid);
tree[now<<].flag+=tree[now].flag,tree[now<<|].flag+=tree[now].flag;
tree[now].flag=;
}
if(l>tree[now].mid) return tree_query(now<<|,l,r);
else if(r<=tree[now].mid) return tree_query(now<<,l,r);
else return tree_query(now<<,l,tree[now].mid)+tree_query(now<<|,tree[now].mid+,r);
} int main()
{
in(n),in(m);int u,v;
for(int i=;i<n;i++)
{
in(u),in(v);
edge[++cnt].to=v,edge[cnt].next=head[u],head[u]=cnt;
edge[++cnt].to=u,edge[cnt].next=head[v],head[v]=cnt;
}
char type;
cnt=,search_1(,);
cnt=,search_2(,);
tree_build(,,n);
while(m--)
{
cin>>type;in(u),in(v);
if(type=='P')
{
while(top[u]!=top[v])
{
if(deep[top[u]]<deep[top[v]]) swap(u,v);
tree_change(,id[top[u]],id[u]);
u=f[top[u]];
}
if(u==v) continue;
if(deep[u]>deep[v]) swap(u,v);
tree_change(,id[u]+,id[v]);
}
else
{
int pos=;
while(top[u]!=top[v])
{
if(deep[top[u]]<deep[top[v]]) swap(u,v);
pos+=tree_query(,id[top[u]],id[u]);
u=f[top[u]];
}
if(u==v)
{
printf("%d\n",pos);
continue;
}
if(deep[u]>deep[v]) swap(u,v);
printf("%d\n",pos+tree_query(,id[u]+,id[v]));
}
}
return ;
}

AC日记——[USACO11DEC]牧草种植Grass Planting 洛谷 P3038的更多相关文章

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

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

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

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

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

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

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

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

  5. [USACO11DEC]牧草种植Grass Planting

    图很丑.明显的树链剖分,需要的操作只有区间修改和区间查询.不过这里是边权,我们怎么把它转成点权呢?对于E(u,v),我们选其深度大的节点,把边权扔给它.因为这是树,所以每个点只有一个父亲,所以每个边权 ...

  6. 【LuoguP3038/[USACO11DEC]牧草种植Grass Planting】树链剖分+树状数组【树状数组的区间修改与区间查询】

    模拟题,可以用树链剖分+线段树维护. 但是学了一个厉害的..树状数组的区间修改与区间查询.. 分割线里面的是转载的: ----------------------------------------- ...

  7. 树链剖分【p3038】[USACO11DEC]牧草种植Grass Planting

    表示看不太清. 概括题意 树上维护区间修改与区间和查询. 很明显树剖裸题,切掉,细节处错误T了好久 TAT 代码 #include<cstdio> #include<cstdlib& ...

  8. 洛谷P3038 牧草种植Grass Planting

    思路: 首先,这道题的翻译是有问题的(起码现在是),查询的时候应该是查询某一条路径的权值,而不是某条边(坑死我了). 与平常树链剖分题目不同的是,这道题目维护的是边权,而不是点权,那怎么办呢?好像有点 ...

  9. AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128

    题目描述 Farmer John has installed a new system of  pipes to transport milk between the  stalls in his b ...

随机推荐

  1. ERROR 1045 (28000): Access denied for user 'xxx'@'localhost' (using password: YES) MYSQL 新建用户 无法登录 问题解决方法

    使用mysql ,出现新建账户无法登录问题 查看 user列表中,有部分账户没有设置密码,将全部重新设置一遍密码,然后还是无法登录. 使用命令 update user set password=pas ...

  2. 【Python学习之六】高阶函数1(map、reduce、filter、sorted)

    1.map map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回.示例: >>> def ...

  3. http客户端与浏览器的区别

    两者区别:浏览器对http响应头会进行特定处理(如自动读取本地缓存.设置cookie等),而http客户端(如crul)可能没有像浏览器那样的处理,某些封装程度高的http客户端,可能会有. 同一个文 ...

  4. drf分页器

    drf分页器 1.第一种分页: 类似于django中的分页 2.第二种分页: 偏移分页 3.第三种分页: 加密分页(查询速度快) 无法跳跃 基本参数 from rest_framework.pagin ...

  5. Python基础-面向对象初识--类

    什么是类 具有相似功能和属性的一类实物 什么是对象 类的具体体现,具体到一个 面向对象的优势 1.类是一组相似功能的集合,使组织结构更加清晰和规范化 2.研究面向对象要有上帝的思维,用面向对象设计程序 ...

  6. 函数名&函数名取地址

    有时看到如下的代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 /*****************************/ #includ ...

  7. 光学字符识别OCR-8 综合评估

    数据验证 尽管在测试环境下模型工作良好,但是实践是检验真理的唯一标准.在本节中,我们通过自己的模型,与京东的测试数据进行比较验证.         衡量OCR系统的好坏有两部分内容:(1)是否成功地圈 ...

  8. php生成Exeple表demo

    <?php require "./PHPExcel.php"; $dir=dirname(__FILE__); $objPHPExcel=new PHPExcel(); $o ...

  9. maya 2014帮助手册中 三维概念讲解

    maya 2014 帮助手册中   三维概念讲解 多边形简介 三个或更多的边,   顶点    边    面  组成 经常使用三边形或四边形来建模   n边形不常用 单个多边形称为面   多个面连接到 ...

  10. 12章 应用spary制作高级网页组件

    比较复杂的用户界面,tab面板  伸缩面板和折叠面板等组件.   tab面板和菜单实际上分为两种           一种是切换各个tab页中的内容时并不刷新浏览器窗口,仅仅是被隐藏起来了  利用ja ...