QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#304896#5249. BanditsLaStataleBlueTL 0ms0kbC++174.2kb2024-01-14 05:22:272024-01-14 05:22:28

Judging History

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

  • [2024-01-14 05:22:28]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-01-14 05:22:27]
  • 提交

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 = 100005;

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;
	}
};

namespace querystuff
{
struct Qq
{
	int cn;
	ll val;
	bool operator<(const Qq &o) const
	{
		if (cn != o.cn)
			return cn < o.cn;
		return val < o.val;
	}
};
map<Qq, int> comp;
FenwickTree ft;
void addquery(int cn, ll l, ll r)
{
	comp[{cn, l}] = 0;
	comp[{cn, r + 1}] = 0;
}
void init()
{
	int c = 0;
	for (auto &[_, v] : comp)
		v = c++;
	ft.init(c);
}
void update(int cn, ll l, ll r)
{
	ft.update(comp[{cn, l}], 1);
	ft.update(comp[{cn, r + 1}], -1);
}
int query(int cn, ll dist)
{
	if (comp.size() == 0)
		return 0;
	auto u = comp.upper_bound({cn, dist});
	if (u == comp.begin())
		return 0;
	u--;
	if (u->first.cn != cn)
		return 0;
	return ft.query(u->second);
}
};    // 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, 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];
				querystuff::addquery(cn, max(0ll, radius - 2 * d + 1), radius);
				cn = u;
				radius -= d;
			}
		}
	}

	querystuff::init();
	for (const auto &[a, b] : queries)
		if (b == -1)
		{
			const auto [u, v] = roads[a - 1];
			int start = lca::is_ancestor(u, v) ? v : u;
			ll dist = 0;
			int ans = 0;
			for (int cn = start; cn != -1;)
			{
				const auto [u, d] = centroid::up[cn];
				ans += querystuff::query(cn, dist);
				dist += d;
				cn = u;
			}
			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, max(0ll, radius - 2 * d + 1), radius);
				radius -= d;
				cn = u;
			}
		}

	return 0;
}

详细

Test #1:

score: 0
Time Limit Exceeded

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: