洛谷P3038 [USACO11DEC]牧草种植Grass Planting
时间:2019-11-06
本文章向大家介绍洛谷P3038 [USACO11DEC]牧草种植Grass Planting,主要包括洛谷P3038 [USACO11DEC]牧草种植Grass Planting使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
树链剖分或者树上差分
树链剖分只能对点进行操作,所以把边权化为点权。树上化边权为点权的一般操作是把边权赋到深度较深的点里。
然后用树链剖分+线段树更改权值路径和+单点查询即可解决该问题。
#include <bits/stdc++.h>
#define N 4001011
#define ls l, mid, root << 1
#define rs mid + 1, r, root << 1 | 1
using namespace std;
struct edg {
int to, nex;
}e[N * 4];
int lin[N], dep[N], fat[N], siz[N], son[N], data[N], dfn[N], top[N], temp[N], cnt, n, m, tot;//data[i]表示i的点权。
int tree[N << 2], lazy[N << 2];
inline void add(int f, int t)//默认边权为0
{
e[++cnt].nex = lin[f];
e[cnt].to = t;
lin[f] = cnt;
}
void dfs1(int now, int fa)
{
dep[now] = dep[fa] + 1;
fat[now] = fa;
siz[now] = 1;
for (int i = lin[now]; i; i = e[i].nex)
{
int to = e[i].to;
if (to == fa) continue;
dfs1(to, now);
siz[now] += siz[to];
if (siz[to] > siz[son[now]])
son[now] = to;
}
}
void dfs2(int now, int tes) //求出dfs序, 树链剖分求lca
{
dfn[now] = ++tot;//dfn是dfs序
top[now] = tes;
if (!son[now]) return;
dfs2(son[now], tes);
for (int i = lin[now]; i; i = e[i].nex)//找重链
{
int to = e[i].to;
if (to == fat[now] || to == son[now]) continue;
dfs2(to, to);
}
}
inline void pushup(int root) {tree[root] = tree[root << 1] + tree[root << 1 | 1];}
void build(int l, int r, int root)
{
if (l == r)
{
tree[root] = 0;
return;
}
int mid = (l + r) >> 1;
build(ls), build(rs);
pushup(root);
}
void pushdown(int l, int r, int root)
{
int mid = (l + r) >> 1;
if (lazy[root])
{
lazy[root << 1] += lazy[root];
lazy[root << 1 | 1] += lazy[root];
tree[root << 1] += lazy[root] * (mid - l + 1);
tree[root << 1 | 1] += lazy[root] * (r - mid);
lazy[root] = 0;
}
}
int query(int pos, int l, int r, int root)
{
if (l == r)
return tree[root];
int res = 0, mid = (l + r) >> 1;
pushdown(l, r, root);
if (pos <= mid)
res += query(pos, ls);
else
res += query(pos, rs);
return res;
}
void update(int ql, int qr, int add, int l, int r, int root)
{
if (ql <= l && r <= qr)
{
tree[root] += (r - l + 1) * add;
lazy[root] += add;
return;
}
pushdown(l, r, root);
int mid = (l + r) >> 1;
if (ql <= mid)
update(ql, qr, add, ls);
if (qr > mid)
update(ql, qr, add, rs);
pushup(root);
}
void U(int x, int y)
{
while (top[x] != top[y])//找他们的lca
{
if (dep[top[x]] < dep[top[y]]) swap(x, y);
update(dfn[top[x]], dfn[x], 1, 1, n, 1);
x = fat[top[x]];
}
if (dep[x] > dep[y])
swap(x, y);
update(dfn[x] + 1, dfn[y], 1, 1, n, 1);
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1, a, b; i < n; i++) {scanf("%d%d", &a, &b), add(a, b), add(b, a);}
dfs1(1, 0);
dfs2(1, 1);
build(1, n, 1);
while (m--)
{
char flag; int a, b;
cin >> flag >> a >> b;
if (flag == 'P')
U(a, b);
else
{
if (dep[a] < dep[b]) swap(a, b);
printf("%d\n", query(dfn[a], 1, n, 1));
}
}
return 0;
}
原文地址:https://www.cnblogs.com/liuwenyao/p/11804051.html
- SQL复习之为数据库用户赋予权限
- linux下syscall函数,SYS_gettid,SYS_tgkill
- 数据库收缩数据文件的尝试(三)(r11笔记第22天)
- 再议-Golang语言MessageBox用法实例
- linux下通过go语言获得系统进程cpu使用情况的方法
- Golang语言版ssh口令破解工具 --必须亲自敲代码,否则看了白看
- MySQL中insert语句没有响应的问题分析(r11笔记第21天)
- MySQL级联复制中的数据同步(第二篇)(r11笔记第21天)
- Golang语言 - 以任意类型的slices作为输入参数
- HDUOJ-------The Hardest Problem Ever
- Golang语言--【社区推荐阅读】 fmt用法
- 一个SQL性能问题的优化探索(一)(r11笔记第33天)
- HDUOJ-----1074 Integer Inquiry
- PXE无人值守安装Linux
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Linux基础第五课——用户管理
- Linux基础第六课——grep|awk|sort|uniq
- Python向上取整,向下取整以及四舍五入函数
- 使用requests模块post payload请求
- 解决HTTP status code is not handled or not allowed
- Day8.函数那些事儿
- mac下更改Jupyter notebook工作目录
- Scrapy中将item字段转为简体or繁体
- Mac OS 安装Fiddler
- Python requests模块解析XML
- python requests提示警告InsecureRequestWarning
- python进程池:multiprocessing.pool
- AttributeError: 'list' object has no attribute 'keys'
- raise ValueError("Cannot convert {0!r} to Excel".format(value))
- ImportError: cannot import name 'Process' from 'multiprocessing'