洛谷 P3066 [USACO12DEC]
作者:互联网
洛谷 P3066 [USACO12DEC]
Description
- 给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于l的点有多少个。
Input
* Line 1: 2 integers, N and L (1 <= N <= 200,000, 1 <= L <= 10^18)
* Lines 2..N: The ith line contains two integers p_i and l_i. p_i (1 <= p_i < i) is the first pasture on the shortest path between pasture i and the barn, and l_i (1 <= l_i <= 10^12) is the length of that path.
Output
- Lines 1..N: One number per line, the number on line i is the number
pastures that can be reached from pasture i by taking roads that lead
strictly farther away from the barn (pasture 1) whose total length does
not exceed L.
Sample Input
4 5 1 4 2 3 1 5
Sample Output
3 2 1 1
题解:
- 倍增 + 树剖。
- 正着想比较复杂。不妨倒着想。
- 考虑每个点x能产生的贡献,即在l范围内尽量向上跳。跳到的点y和x之间这一条路径上的每个点答案都++。查询就输出每个点的答案就行。
- 跳用倍增维护,修改 + 查询用树剖维护。
#include <iostream>
#include <cstdio>
#include <cmath>
#define N 200005
#define int long long
using namespace std;
struct T {int l, r, val, tag;} t[N * 4];
struct E {int next, to, dis;} e[N];
int n, l, num, dex, logMax;
int h[N], dep[N], size[N], top[N], son[N], dfn[N], fat[N];
int f[N][25], dis[N][25];
int read()
{
int x = 0; char c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
return x;
}
void add(int u, int v, int w)
{
e[++num].next = h[u];
e[num].to = v;
e[num].dis = w;
h[u] = num;
}
void dfs1(int x, int fa, int de)
{
size[x] = 1, dep[x] = de, fat[x] = fa;
int maxSon = 0;
for(int i = h[x]; i != 0; i = e[i].next)
{
dfs1(e[i].to, x, de + 1);
f[e[i].to][0] = x;
dis[e[i].to][0] = e[i].dis;
size[x] += size[e[i].to];
if(size[e[i].to] > maxSon)
{
maxSon = size[e[i].to];
son[x] = e[i].to;
}
}
}
void dfs2(int x, int head)
{
top[x] = head, dfn[x] = ++dex;
if(!son[x]) return;
dfs2(son[x], head);
for(int i = h[x]; i != 0; i = e[i].next)
if(e[i].to != son[x])
dfs2(e[i].to, e[i].to);
}
void build(int p, int l, int r)
{
t[p].l = l, t[p].r = r;
if(l == r) return;
int mid = l + r >> 1;
build(p << 1, l, mid), build(p << 1 | 1, mid + 1, r);
}
void down(int p)
{
int s1 = p << 1, s2 = p << 1 | 1;
t[s1].tag += t[p].tag, t[s2].tag += t[p].tag;
t[s1].val += (t[s1].r - t[s1].l + 1) * t[p].tag;
t[s2].val += (t[s2].r - t[s2].l + 1) * t[p].tag;
t[p].tag = 0;
}
void upd(int p, int l, int r, int add)
{
if(t[p].l >= l && t[p].r <= r)
{
t[p].tag += add;
t[p].val += (t[p].r - t[p].l + 1) * add;
return;
}
if(t[p].tag) down(p);
int mid = t[p].l + t[p].r >> 1;
if(l <= mid) upd(p << 1, l, r, add);
if(r > mid) upd(p << 1 | 1, l, r, add);
t[p].val = t[p << 1].val + t[p << 1 | 1].val;
}
void updLink(int x, int y)
{
while(top[x] != top[y])
{
if(dep[top[x]] < dep[top[y]]) swap(x, y);
upd(1, dfn[top[x]], dfn[x], 1);
x = fat[top[x]];
}
if(dep[x] > dep[y]) swap(x, y);
upd(1, dfn[x], dfn[y], 1);
}
void cal(int x)
{
int u = x;
int tot = 0;
for(int i = logMax; i >= 0; i--)
if(tot + dis[x][i] <= l)
{
tot += dis[x][i];
x = f[x][i];
}
int v = (x == 0 ? 1 : x);
updLink(u, v);
}
int ask(int p, int l, int r)
{
if(t[p].l >= l && t[p].r <= r) return t[p].val;
if(t[p].tag) down(p);
int mid = t[p].l + t[p].r >> 1, res = 0;
if(l <= mid) res += ask(p << 1, l, r);
if(r > mid) res += ask(p << 1 | 1, l, r);
return res;
}
int askLink(int x, int y)
{
int res = 0;
while(top[x] != top[y])
{
if(dep[top[x]] < dep[top[y]]) swap(x, y);
res += ask(1, dfn[top[x]], dfn[x]);
x = fat[top[x]];
}
if(dep[x] > dep[y]) swap(x, y);
res += ask(1, dfn[x], dfn[y]);
return res;
}
signed main()
{
freopen("P3066.in", "r", stdin);
freopen("P3066.out", "w", stdout);
cin >> n >> l, logMax = (int)log2(n);
for(int i = 2; i <= n; i++)
{
int u = read(), w = read();
add(u, i, w);
}
dfs1(1, 0, 1), dfs2(1, 1);
for(int j = 1; j <= logMax; j++)
for(int i = 1; i <= n; i++)
{
f[i][j] = f[f[i][j - 1]][j - 1];
dis[i][j] = dis[i][j - 1] + dis[f[i][j - 1]][j - 1];
}
build(1, 1, n);
for(int i = 1; i <= n; i++) cal(i);
for(int i = 1; i <= n; i++)
printf("%lld\n", askLink(i, i));
return 0;
}
标签:USACO12DEC,洛谷,int,res,son,dfn,P3066,include 来源: https://www.cnblogs.com/BigYellowDog/p/11775687.html