QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#305265#5249. BanditsLaStataleBlueWA 282ms39132kbC++174.5kb2024-01-14 23:50:342024-01-14 23:50:35

Judging History

你现在查看的是最新测评结果

  • [2024-01-14 23:50:35]
  • 评测
  • 测评结果:WA
  • 用时:282ms
  • 内存:39132kb
  • [2024-01-14 23:50:34]
  • 提交

answer

#include <cstdio>
#include <algorithm>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
constexpr int MAXN = 1e5 + 5;
constexpr int MAXR = 1e9 + 5;
#define allvec(v) v.data(), v.data() + v.size()

vector<pii> g[MAXN];

namespace lca
{
int timer = 0, tin[MAXN], tout[MAXN];
ll dist[MAXN];
constexpr int LOGMAXN = 18;
int up[MAXN][LOGMAXN];
void dfs(int n, int p)
{
	tin[n] = timer++;
	up[n][0] = p;
	for (int i = 1; i < LOGMAXN; i++)
		up[n][i] = up[up[n][i - 1]][i - 1];
	for (const auto &[a, w] : g[n])
		if (a != p)
		{
			dist[a] = dist[n] + w;
			dfs(a, n);
		}
	tout[n] = timer++;
}
void init()
{
	dist[1] = 0;
	dfs(1, 1);
}
bool is_ancestor(int u, int v)
{
	return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int lca(int u, int v)
{
	if (is_ancestor(u, v))
		return u;
	if (is_ancestor(v, u))
		return v;
	for (int i = LOGMAXN - 1; i >= 0; i--)
		if (!is_ancestor(up[u][i], v))
			u = up[u][i];
	return up[u][0];
}
ll getdist(int u, int v)
{
	return dist[u] + dist[v] - 2 * dist[lca(u, v)];
}
}    // namespace lca

namespace centroid
{
int treesize[MAXN];
bool used[MAXN];
pair<int, ll> up[MAXN];
void dfs(int n, int p)
{
	treesize[n] = 1;
	for (const auto &[a, _] : g[n])
		if (a != p && !used[a])
		{
			dfs(a, n);
			treesize[n] += treesize[a];
		}
}
int centroid(int n, int p, int sz)
{
	for (const auto &[a, _] : g[n])
		if (a != p && !used[a] && treesize[a] > sz / 2)
			return centroid(a, n, sz);
	return n;
}
void decomposition(int n, int p)
{
	dfs(n, n);
	int c = centroid(n, n, treesize[n]);
	up[c] = {p, p == -1 ? 1e15 : lca::getdist(c, p)};
	used[c] = true;
	for (const auto &[a, _] : g[c])
		if (!used[a])
			decomposition(a, c);
}
void init()
{
	decomposition(1, -1);
}
}    // namespace centroid

struct FenwickTree
{
	vector<int> tree;
	void init(int n)
	{
		tree.assign(n + 1, 0);
	}
	void update(int p, int a)
	{
		p++;
		while (p < tree.size())
		{
			tree[p] += a;
			p += p & (-p);
		}
	}
	int query(int p)
	{
		int s = 0;
		p++;
		while (p > 0)
		{
			s += tree[p];
			p -= p & (-p);
		}
		return s;
	}
};

namespace querystuff
{
vector<int> rs[MAXN];
FenwickTree ft[MAXN];
void addquery(int cn, int radius)
{
	rs[cn].push_back(radius);
}
void init(int N)
{
	for (int i = 1; i <= N; i++)
		if (rs[i].size() != 0)
		{
			vector<int> &r = rs[i];
			sort(allvec(r));
			int nsz = unique(allvec(r)) - r.data();
			r.resize(nsz);
			ft[i].init(nsz);
		}
}
void update(int cn, int radius)
{
	if (rs[cn].size() == 0)
		return;
	int l = 0, r = rs[cn].size();
	while (l + 1 != r)
	{
		int m = (l + r) / 2;
		if (rs[cn][m] <= radius)
			l = m;
		else
			r = m;
	}
	ft[cn].update(l, 1);
}
int query(int cn, int a, int b)    // [a, b)
{
	if (rs[cn].size() == 0)
		return 0;
	const auto getlt = [cn](int val) -> int
	{
		if (rs[cn][0] >= val)
			return 0;
		int l = 0, r = rs[cn].size();
		while (l + 1 != r)
		{
			int m = (l + r) / 2;
			if (rs[cn][m] < val)
				l = m;
			else
				r = m;
		}
		return ft[cn].query(l);
	};
	return getlt(b) - getlt(a);
}
};    // namespace querystuff

int main()
{
	int N;
	scanf("%d", &N);
	vector<pii> roads(N);
	for (int i = 0, u, v, w; i < N - 1; i++)
	{
		scanf("%d %d %d", &u, &v, &w);
		g[u].push_back({v, w});
		g[v].push_back({u, w});
		roads[i] = {u, v};
	}

	lca::init();
	centroid::init();

	int Q;
	scanf("%d", &Q);
	vector<pii> queries(Q);
	for (int i = 0; i < Q; i++)
	{
		char t[2];
		int a, b = -1;
		scanf("%s %d", t, &a);
		if (t[0] == '+')
			scanf("%d", &b);
		queries[i] = {a, b};
		if (t[0] == '+')
		{
			ll radius = b;
			for (int cn = a; cn != -1 && radius > 0;)
			{
				const auto [u, d] = centroid::up[cn];
				querystuff::addquery(cn, radius);
				cn = u;
				radius -= d;
			}
		}
	}

	querystuff::init(N);
	for (const auto &[a, b] : queries)
		if (b == -1)
		{
			const auto [v0, v1] = roads[a - 1];
			int start = lca::is_ancestor(v0, v1) ? v1 : v0;
			ll dist = 0;
			int ans = 0;
			for (int cn = start; cn != -1 && dist < MAXR;)
			{
				const auto [u, d] = centroid::up[cn];
				ans += querystuff::query(cn, dist, min(ll(MAXR), dist + 2 * d));
				cn = u;
				dist += d;
			}
			printf("%d\n", ans);
		}
		else
		{
			ll radius = b;
			for (int cn = a; cn != -1 && radius > 0;)
			{
				const auto [u, d] = centroid::up[cn];
				querystuff::update(cn, radius);
				cn = u;
				radius -= d;
			}
		}

	return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 282ms
memory: 39132kb

input:

100000
2670 75097 4080
87477 75802 1712
51835 36626 2883
19412 25923 5852
23976 19312 2520
82536 19514 2492
27160 66601 4483
99087 15088 3504
47050 58820 2964
37063 5696 9901
7717 1496 4891
79136 5448 4340
22575 81285 9289
96280 3803 9877
41980 32139 2855
44236 64938 3298
5983 99947 9666
95856 62545...

output:

0
0
0
1
1
4
1
1
2
3
3
6
7
7
9
9
12
11
11
8
8
9
9
8
9
8
9
8
12
9
11
11
11
13
10
14
14
9
14
13
12
19
14
16
19
19
14
15
18
21
19
23
22
20
24
25
28
27
28
31
30
31
34
35
37
34
32
35
38
36
37
40
39
40
42
38
40
48
45
47
49
52
55
52
55
55
54
56
56
56
57
59
60
62
63
62
64
65
67
64
67
64
67
67
66
66
65
66
67
...

result:

wrong answer 4th lines differ - expected: '2', found: '1'