LOJ6119 「2017 山东二轮集训 Day7」国王
题意
给定一颗树,每个点有权值 \(1\) 和 \(-1\),称一条路径是好的当且仅当路径上所有点的权值和为 \(0\)。
求连续编号区间 \([l, r]\) 使得两个点都在 \([l, r]\) 的好路径比两个点都不在 \([l, r]\) 的好路径数严格多的方案数。
\(n \le 10 ^ 5\)。
Sol
两个端点都在区间内不好做,设一个区间的权值为 \(f_{[l, r]}\)。
因此答案为 \(\sum [f_{[l, r]} > f_{[1, l - 1] \cup [r + 1, n]}]\)。
集中注意力,考虑至少一个端点在区间内的情况,发现好像两边可以约掉!
具体地,至少一个端点在 \([l, r]\) 的方案数 等于 \(f_{[l, r]}\) 加上 有一个端点在 \([l, r]\) 一个端点在 \([1, l- 1] \cup [r + 1, n]\) 的方案数,于是直接约掉了。
考虑我们现在可以求出什么,设 \(g_i\) 表示一个端点为 \(i\) 的合法路径数,这个东西可以简单使用点分治求得。
最后因为合法区间具有单调性,直接双指针计算最小的合法区间即可。
复杂度 \(O(n \log n)\)。
Code
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <bitset>
#define ll long long
#define pii pair <int, int>
using namespace std;
#ifdef ONLINE_JUDGE
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;
#endif
int read() {
int p = 0, flg = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') flg = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
p = p * 10 + c - '0';
c = getchar();
}
return p * flg;
}
void write(ll x) {
if (x < 0) {
x = -x;
putchar('-');
}
if (x > 9) {
write(x / 10);
}
putchar(x % 10 + '0');
}
bool _stmer;
#define fi first
#define se second
const int N = 1e5 + 5, M = 2e5 + 5;
namespace G {
array <int, N> fir;
array <int, M> nex, to;
int cnt = 1;
void add(int x, int y) {
cnt++;
nex[cnt] = fir[x];
to[cnt] = y;
fir[x] = cnt;
}
} //namespace G
array <int, N> len, siz;
bitset <N> vis;
void dfs1(int x, int fa) {
siz[x] = 1;
for (int i = G::fir[x]; i; i = G::nex[i]) {
if (vis[G::to[i]] || G::to[i] == fa) continue;
dfs1(G::to[i], x), siz[x] += siz[G::to[i]];
}
}
pii rt;
void dfs2(int x, int fa, int Rt) {
int tp = 0;
for (int i = G::fir[x]; i; i = G::nex[i]) {
if (vis[G::to[i]] || G::to[i] == fa) continue;
dfs2(G::to[i], x, Rt), tp = max(tp, siz[G::to[i]]);
}
tp = max(tp, siz[Rt] - siz[x]);
if (tp < rt.fi) rt = make_pair(tp, x);
}
array <int, M> isl;
array <int, N> dis;
void dfs3(int x, int pl, int fa) {
isl[dis[x]] += pl;
for (int i = G::fir[x]; i; i = G::nex[i]) {
if (vis[G::to[i]] || G::to[i] == fa) continue;
dis[G::to[i]] = dis[x] + len[G::to[i]];
dfs3(G::to[i], pl, x);
}
}
array <int, N> f;
void dfs4(int x, int fa, int sum) {
f[x] += isl[1e5 - sum];
for (int i = G::fir[x]; i; i = G::nex[i]) {
if (vis[G::to[i]] || G::to[i] == fa) continue;
dfs4(G::to[i], x, sum + len[G::to[i]]);
}
}
void solve(int x) {
dis[x] = 1e5 + len[x];
dfs3(x, 1, 0);
f[x] += isl[1e5];
for (int i = G::fir[x]; i; i = G::nex[i])
if (!vis[G::to[i]])
dfs3(G::to[i], -1, x), dfs4(G::to[i], x, len[G::to[i]]), dfs3(G::to[i], 1, x);
dfs3(x, -1, 0);
}
void divide(int x) {
rt = make_pair(2e9, 0);
dfs1(x, 0), dfs2(x, 0, x);
x = rt.se, vis[x] = 1, solve(x);
for (int i = G::fir[x]; i; i = G::nex[i])
if (!vis[G::to[i]]) divide(G::to[i]);
}
bool _edmer;
int main() {
cerr << (&_stmer - &_edmer) / 1024.0 / 1024.0 << "MB\n";
int n = read();
for (int i = 1, x; i <= n; i++)
x = read(), len[i] = x ? 1 : -1;
for (int i = 2, x, y; i <= n; i++)
x = read(), y = read(), G::add(x, y), G::add(y, x);
divide(1);
ll res = 0, sum = 0, ans = 0;
for (int i = 1; i <= n; i++) res += f[i];
for (int i = 1, lst = 1; i <= n; i++) {
sum += f[i];
while (lst < i && sum > res - sum) sum -= f[lst++];
ans += lst - 1;
}
write(ans), puts("");
return 0;
}