CF1575I Illusions of the Desert
prologue
还是太菜了,这个 154 行的树剖 20min 才敲完。
analysis
首先,处理这个给到我们的这个式子。
\]
我们可以分类讨论:
\(a > 0, b > 0\):
显然 \(a + b > a - b\), 所以上式等于 $a + b \Rightarrow | a | + | b | $\(a > 0, b < 0 \iff a < 0, b > 0\)
这个时候,我们只需要讨论 \(a > 0, b < 0\) 的情况就好了。我们将 \(b\) 的符号拿出来,\(\Rightarrow a - (- | b |) \Rightarrow a + | b | \Rightarrow | a | + | b |\)\(a < 0, b < 0\)
操作同上, $ | -(| a | + | b |) | \Rightarrow | a | + | b | $。
综上:
\]
对于一条路径的询问,我们把每两个点之间的距离拿出来,不断累加,就可以知道,这一段路径的长度就是下式。(其中 \(dis _ {u \to v}\) 表示从 \(u\) 到 \(v\) 的距离, \(sum _ {u \to v}\) 表示从 \(u\) 到 \(v\) 的点值的绝对值之和,\(w_u\) 和 \(w_v\) 表示这两个点的点权)
\]
之后就是正常实现一个树链剖分和线段树即可。
code time
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rl register ll
const ll N = 1e5 + 10, M = N << 1;
ll n, m;
ll tot, ne[M], e[M], h[N], w[N];
ll son[N], top[N], id[N], cnt, fa[N], dep[N], nw[N], sz[N];
struct node
{
ll l, r;
ll dis;
}tr[N << 2];
inline void add(ll a, ll b)
{
ne[++tot] = h[a], h[a] = tot, e[tot] = b;
}
inline void dfs1(ll u, ll fath, ll depth)
{
fa[u] = fath, dep[u] = depth, sz[u] = 1;
for(rl i=h[u]; ~i; i = ne[i])
{
ll v = e[i];
if(v == fath) continue;
dfs1(v, u, depth + 1);
sz[u] += sz[v];
if(sz[son[u]] < sz[v]) son[u] = v;
}
}
inline void dfs2(ll u, ll t)
{
id[u] = ++ cnt, nw[cnt] = w[u], top[u] = t;
if(!son[u]) return ;
dfs2(son[u], t);
for(rl i=h[u]; ~i; i = ne[i])
{
ll v = e[i];
if(v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
}
inline void pushup(ll u)
{
tr[u].dis = abs(tr[u << 1].dis) + abs(tr[u << 1 | 1].dis);
}
inline void build(ll u, ll l, ll r)
{
tr[u] = {l, r, abs(nw[r])};
if(l == r) return ;
ll mid = l + r >> 1;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
pushup(u);
}
inline void update(ll u, ll l, ll r, ll k)
{
if(tr[u].l == l && tr[u].l == tr[u].r)
{
tr[u].dis = k;
return ;
}
ll mid = tr[u].l + tr[u].r >> 1;
if(l <= mid) update(u << 1, l, r, k);
if(r > mid) update(u << 1 | 1, l, r, k);
pushup(u);
}
inline ll query(ll u, ll l, ll r)
{
ll res = 0;
if(l <= tr[u].l && r >= tr[u].r) return tr[u].dis;
ll mid = tr[u].l + tr[u].r >> 1;
if(l <= mid) res += query(u << 1, l, r);
if(r > mid) res += query(u << 1 | 1, l, r);
return res;
}
inline void upd_point(ll u, ll k)
{
update(1, id[u], id[u], k);
}
inline ll query_path(ll u, ll v)
{
ll res = - query(1, id[u], id[u]) - query(1, id[v], id[v]);
while(top[u] != top[v])
{
if(dep[top[u]] < dep[top[v]]) swap(u, v);
res += 2 * query(1, id[top[u]], id[u]);
u = fa[top[u]];
}
if(dep[u] < dep[v]) swap(u, v);
res += 2 * query(1, id[v], id[u]);
return res;
}
int main()
{
// freopen("1.in", "r", stdin), freopen("1.out", "w", stdout);
cin >> n >> m;
memset(h, -1, sizeof h);
for(rl i=1; i <= n; ++ i) cin >> w[i];
for(rl i=1; i < n; ++ i)
{
ll a, b;
cin >> a >> b;
add(a, b), add(b, a);
}
dfs1(1, -1, 1);
dfs2(1, 1);
build(1, 1, n);
while(m -- )
{
ll t, u, v;
cin >> t >> u >> v;
if(t == 1) upd_point(u, abs(v));
else cout << query_path(u, v) << endl;
}
return 0;
}
CF1575I Illusions of the Desert的更多相关文章
- 【poj2728】Desert King
[poj2728]Desert King 题意 最优比率生成树. http://blog.csdn.net/ophunter_lcm/article/details/10113817 分析 Dinke ...
- hdu-5703 Desert(水题)
题目链接: Desert Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Pr ...
- poj 2728 Desert King (最小比例生成树)
http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissio ...
- POJ 2728 Desert King
Description David the Great has just become the king of a desert country. To win the respect of his ...
- Desert King(最优比率生成树)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22717 Accepted: 6374 Desc ...
- POJ 2728 Desert King 最优比率生成树
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20978 Accepted: 5898 [Des ...
- Desert King POJ - 2728(最优比率生产树/(二分+生成树))
David the Great has just become the king of a desert country. To win the respect of his people, he d ...
- POJ 2728 Desert King (01分数规划)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions:29775 Accepted: 8192 Descr ...
- poj2728 Desert King【最优比率生成树】【Prim】【0/1分数规划】
含[最小生成树Prim]模板. Prim复杂度为$O(n^2),适用于稠密图,特别是完全图的最小生成树的求解. Desert King Time Limit: 3000MS Memory Li ...
- 21 Survival of Desert Life 沙漠生命的延续
Survival of Desert Life 沙漠生命的延续 ① Some desert animals can survive the very strong summer heat and dr ...
随机推荐
- CF1442D Sum
题意 有 \(n\) 个不降的非负整数数组,每个数组可以不取或取一个前缀,总共要取 \(k\) 个元素,问取到的和最大多少. 题解 结论题,但是想到结论还不会. 首先,我们只会有一个数组没选完,其它要 ...
- 怎么把 session 中的实体类转换回来
例子 : 如上比如user user1=new user(): user1.id=1: user1.name="张三": session["user1"]=us ...
- langchain:Prompt在手,天下我有
目录 简介 好的prompt 什么是prompt template 在langchain中创建prompt template Chat特有的prompt template 总结 简介 prompts是 ...
- Centos 7安装JDK1.8
# 安装 yum install -y java-1.8.0-openjdk* # 添加环境变量 vim /etc/profile export JAVA_HOME=/usr/lib/jvm/java ...
- [Spring+SpringMVC+Mybatis]框架学习笔记(六):Spring_AspectJ实现AOP
第6章 Spring_AspectJ实现AOP 6.1 什么是AspectJ 对于AOP的这种编程思想,有很多框架或者组件进行了实现,spring实现AOP就是其中的一种. AspectJ也实现了AO ...
- C语言循环坑 -- continue的坑
文章目录 前言 一.continue语法 1.continue的作用 2.语法 二.大坑项目 题目 分析 正确写法 三.进坑调试 第一种 第二种 总结 前言 在使用continue和break时,会出 ...
- DolphinScheduler3.1.7离线手册
DolphinScheduler3.1.7 DolphinScheduler简介 Apache DolphinScheduler 是一个分布式易扩展的可视化DAG工作流任务调度开源系统.适用于企业级场 ...
- DataGridView 控件分页
在使用Winform开发桌面应用时,工具箱预先提供了丰富的基础控件,利用这些基础控件可以开展各类项目的开发.但是或多或少都会出现既有控件无法满足功能需求的情况,或者在开发类似项目时,我们希望将具有相同 ...
- Go函数可以返回多个值
1 package main 2 3 import "fmt" 4 5 func swap(x, y string) (string, string){ 6 return y,x ...
- Django2.2:UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 9737: illegal multibyte sequence
报错截图: 解决方案: 打开django/views下的debug.py文件,转到line331行: with Path(CURRENT_DIR, 'templates', 'technical_50 ...