【LG1600】[NOIP2016]天天爱跑步

题面

洛谷

题解

考虑一条路径\(S\rightarrow T\)是如何给一个观测点\(x\)造成贡献的,

一种是从\(x\)的子树内出来,另外一种是从\(x\)的子树外进去。

令\(S,T\)的最近公共祖先为\(lca\),那么这条路径可表示为\(S\rightarrow lca\rightarrow T\)(如果\(lca=S\;or\;T\)可以特判)。

考虑两种情况如何贡献,

首先在\(S\rightarrow lca\)上的点,需要满足\(dep_S-dep_x=w_x\),

而对于\(lca\rightarrow T\)上的点,需要满足\((dep_S-dep_{lca})+(dep_x-dep_{lca})=w_x\Leftrightarrow dep_S-2dep_{lca}=w_x-dep_x\)。

这样的话,对于一条路径,我们可以拆成两条分别对其进行差分,在用一颗线段树在其对应位置上\(\pm 1\),然后线段树合并在对应位置上查即可。

具体实现细节详见代码。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
return w * data;
}
const int MAX_N = 3e5 + 5;
struct Graph { int to, next; } e[MAX_N << 1];
int fir[MAX_N], e_cnt;
void clearGraph() { memset(fir, -1, sizeof(fir)); e_cnt = 0; }
void Add_Edge(int u, int v) { e[e_cnt] = (Graph){v, fir[u]}, fir[u] = e_cnt++; }
int fa[MAX_N], top[MAX_N], dep[MAX_N], size[MAX_N], son[MAX_N];
void dfs1(int x) {
size[x] = 1, dep[x] = dep[fa[x]] + 1;
for (int i = fir[x]; ~i; i = e[i].next) {
int v = e[i].to; if (v == fa[x]) continue;
fa[v] = x, dfs1(v), size[x] += size[v];
if (size[son[x]] < size[v]) son[x] = v;
}
}
void dfs2(int x, int tp) {
top[x] = tp;
if (son[x]) dfs2(son[x], tp);
for (int i = fir[x]; ~i; i = e[i].next) {
int v = e[i].to; if (v == son[x] || v == fa[x]) continue;
dfs2(v, v);
}
}
int LCA(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
x = fa[top[x]];
}
return dep[x] < dep[y] ? x : y;
}
struct Path { int s, t, lca; } p[MAX_N];
int N, M, w[MAX_N], ans[MAX_N];
vector<int> Add1[MAX_N], Del1[MAX_N], Add2[MAX_N], Del2[MAX_N];
struct Node { int ls, rs, v; } t[MAX_N << 6];
int rt1[MAX_N], rt2[MAX_N], tot;
void insert(int &o, int l, int r, int pos, int op) {
if (!o) o = ++tot;
t[o].v += op;
if (l == r) return ;
int mid = (l + r) >> 1;
if (pos <= mid) insert(t[o].ls, l, mid, pos, op);
else insert(t[o].rs, mid + 1, r, pos, op);
}
int merge(int x, int y, int l, int r) {
if (!x || !y) return x | y;
if (l == r) return t[x].v += t[y].v, x;
int mid = (l + r) >> 1;
t[x].ls = merge(t[x].ls, t[y].ls, l, mid);
t[x].rs = merge(t[x].rs, t[y].rs, mid + 1, r);
return t[x].v = t[t[x].ls].v + t[t[x].rs].v, x;
}
int query(int o, int l, int r, int pos) {
if (!o) return 0;
if (l == r) return t[o].v;
int mid = (l + r) >> 1;
if (pos <= mid) return query(t[o].ls, l, mid, pos);
else return query(t[o].rs, mid + 1, r, pos);
}
void Dfs(int x) {
for (int i = fir[x]; ~i; i = e[i].next) {
int v = e[i].to; if (v == fa[x]) continue;
Dfs(v);
rt1[x] = merge(rt1[x], rt1[v], -N, N << 1);
rt2[x] = merge(rt2[x], rt2[v], -N, N << 1);
}
for (int i : Add1[x]) insert(rt1[x], -N, N << 1, i, 1);
for (int i : Add2[x]) insert(rt2[x], -N, N << 1, i, 1);
for (int i : Del1[x]) insert(rt1[x], -N, N << 1, i, -1);
for (int i : Del2[x]) insert(rt2[x], -N, N << 1, i, -1);
ans[x] = query(rt1[x], -N, N << 1, w[x] + dep[x]) + query(rt2[x], -N, N << 1, w[x] - dep[x]);
}
int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
clearGraph();
N = gi(), M = gi();
for (int i = 1; i < N; i++) {
int u = gi(), v = gi();
Add_Edge(u, v), Add_Edge(v, u);
}
dfs1(1), dfs2(1, 1);
for (int i = 1; i <= N; i++) w[i] = gi();
for (int i = 1; i <= M; i++) {
int s = gi(), t = gi(), lca = LCA(s, t);
int d1 = dep[s], d2 = -dep[s];
if (lca == t) { Add1[s].push_back(d1), Del1[fa[lca]].push_back(d1); continue; }
if (lca == s) { Add2[t].push_back(d2), Del2[fa[lca]].push_back(d2); continue; }
d2 = dep[s] - 2 * dep[lca];
Add1[s].push_back(d1), Del1[fa[lca]].push_back(d1);
Add2[t].push_back(d2), Del2[lca].push_back(d2);
}
Dfs(1);
for (int i = 1; i <= N; i++) printf("%d ", ans[i]);
putchar('\n');
return 0;
}

【LG1600】[NOIP2016]天天爱跑步的更多相关文章

  1. [NOIp2016]天天爱跑步 线段树合并

    [NOIp2016]天天爱跑步 LG传送门 作为一道被毒瘤出题人们玩坏了的NOIp经典题,我们先不看毒瘤的"动态爱跑步"和"天天爱仙人掌",回归一下本来的味道. ...

  2. [Noip2016]天天爱跑步 LCA+DFS

    [Noip2016]天天爱跑步 Description 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.?天天爱跑步?是一个养成类游戏,需要玩家每天按时上线,完成打卡任 ...

  3. NOIP2016天天爱跑步 题解报告【lca+树上统计(桶)】

    题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.«天天爱跑步»是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个游戏的地图可以看作一一棵包含 nn个 ...

  4. BZOJ4719 [Noip2016]天天爱跑步

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  5. noip2016天天爱跑步

    题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.«天天爱跑步»是一个养成类游戏,需要玩家每天按时上线,完成打卡任务. 这个游戏的地图可以看作一一棵包含 个结点 ...

  6. bzoj 4719: [Noip2016]天天爱跑步

    Description 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.?天天爱跑步?是一个养成类游戏,需要 玩家每天按时上线,完成打卡任务.这个游戏的地图可以看作一一 ...

  7. NOIP2016 天天爱跑步 80分暴力

    https://www.luogu.org/problem/show?pid=1600 题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.«天天爱跑步»是一个养 ...

  8. 4719: [Noip2016]天天爱跑步

    Time Limit: 40 Sec Memory Limit: 512 MB Submit: 1986 Solved: 752 [Submit][Status][Discuss] Descripti ...

  9. NOIP2016 天天爱跑步 线段树合并_桶_思维题

    竟然独自想出来了,好开心 Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r&q ...

随机推荐

  1. 物联网架构成长之路(31)-EMQ基于HTTP权限验证

    看过之前的文章就知道,我之前是通过搞插件,或者通过里面的MongoDB来进行EMQ的鉴权登录和权限验证.但是前段时间发现,还是通过HTTP WebHook 方式来调用鉴权接口比较适合实际使用.还是实现 ...

  2. HTML连载31-制作一个百度首页

    一. 我们制作一个百度首页作为练习,可直接复制该代码保存后缀名为.html来查看 <!DOCTYPE html> <html lang="en"> < ...

  3. sqlplus简单使用

    登录 C:\Users\inmeditation>sqlplus 请输入用户名: scott 输入口令: 查看当前行长 SQL> show linesize; linesize 80 查看 ...

  4. Practical Go: Real world advice for writing maintainable Go programs

    转自:https://dave.cheney.net/practical-go/presentations/qcon-china.html?from=timeline   1. Guiding pri ...

  5. linq 大数据 sql 查询及分页优化

    前提: 需要nuget   PredicateLib   0.0.5: SqlServer  2008R2 (建议安装 64 位): .net 4.5 或以上: 当前电脑配置: I7 4核  3.6G ...

  6. Sitecore 8.2 渠道简介

    渠道是联系人通过广告系列或面对面与您的品牌互动时所使用的路径.联系人可以通过手机上的应用与您的品牌互动,点击社交网络上的广告访问您的网站,或访问实体店购买商品.使用Sitecore体验平台,您可以使用 ...

  7. Visual Studio Code 小记

    1. 改变语言 如图: 2. 设置皮肤 如图: 3. Visual Studio Code关闭右侧预览功能 4. 关闭预览模式 5. VS Code 优秀的主题 a. Atom One Dark Th ...

  8. axios和fetch区别对比

    axios axios({ method: 'post', url: '/user/12345', data: { firstName: 'Fred', lastName: 'Flintstone' ...

  9. .NET使用本地outlook客户端发送邮件

    1.添加Microsoft.Office.Interop.Outlook引用 2.封装发送邮件方法 using System; using System.Configuration; using Sy ...

  10. 配置IIS网站可以下载.apk/.ipa文件

    添加 扩展名是:.apk MIMI类型是:application/vnd.android.package-archive 扩展名是:.ipa MIMI类型是:application/iphone