QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#304858#5249. BanditsLaStataleBlueRE 0ms0kbC++173.9kb2024-01-14 04:31:212024-01-14 04:31:21

Judging History

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

  • [2024-01-14 04:31:21]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-01-14 04:31:21]
  • 提交

answer

#include <cstdio>
#include <algorithm>
#include <map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
constexpr int MAXN = 100'005;

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[n])
		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;
	}
};

struct Qnode
{
	map<ll, int> comp;
	FenwickTree ft;
	void addquery(ll l, ll r)
	{
		comp[l] = 0;
		comp[r + 1] = 0;
	}
	void init()
	{
		int c = 0;
		for (auto &[_, v] : comp)
			v = c++;
		ft.init(c);
	}
	void update(ll l, ll r)
	{
		ft.update(comp[l], 1);
		ft.update(comp[r + 1], -1);
	}
	int query(ll dist)
	{
		if (comp.size() == 0)
			return 0;
		auto u = comp.upper_bound(dist);
		if (u == comp.begin())
			return 0;
		u--;
		return ft.query(u->second);
	}
};

Qnode qnodes[MAXN];

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();
	return 1;

	int Q;
	scanf("%d", &Q);
	vector<pii> queries(Q);
	for (int i = 0, a, b; i < Q; i++)
	{
		char t[2];
		scanf("%s %d", t, &a);
		b = -1;
		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];
				qnodes[cn].addquery(max(0ll, radius - 2 * d + 1), radius);
				cn = u;
				radius -= d;
			}
		}
	}

	for (int i = 1; i <= N; i++)
		qnodes[i].init();

	for (const auto &[a, b] : queries)
	{
		int start = a;
		ll dist = b;
		if (b == -1)
		{
			const auto [u, v] = roads[a - 1];
			start = lca::is_ancestor(u, v) ? v : u;
			dist = 0;
		}
		int ans = 0;
		for (int cn = start; cn != -1 && dist >= 0;)
		{
			const auto [u, d] = centroid::up[cn];
			if (b == -1)
			{
				ans += qnodes[cn].query(dist);
				dist += d;
			}
			else
			{
				qnodes[cn].update(max(0ll, dist - 2 * d + 1), dist);
				dist -= d;
			}
			cn = u;
		}

		if (b == -1)
			printf("%d\n", ans);
	}

	return 0;
}

详细

Test #1:

score: 0
Runtime Error

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:


result: