QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#556834#6737. Neighbourhood251SecWA 6436ms70132kbC++144.4kb2024-09-10 21:19:442024-09-10 21:19:44

Judging History

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

  • [2024-09-10 21:19:44]
  • 评测
  • 测评结果:WA
  • 用时:6436ms
  • 内存:70132kb
  • [2024-09-10 21:19:44]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int B = 800;
struct Edge {
	int v, nxt;
} e[400005];
int head[200005], len = 1;
void Insert(int u, int v) {
	e[++len] = { v, head[u] };
	head[u] = len;
}
int n, q;
int dpt[200005], lt[200005], rt[200005], cti, st[20][200005], prt[200005];
ll we[200005];
namespace BIT {
	ll f[200005];
	void M(int i, ll w) { for (; i <= n; i += i & -i) f[i] += w; }
	ll Q(int i) { ll res = 0; for (; i; i &= i - 1) res += f[i]; return res; }
	void Modify(int u, ll w) { M(lt[u], w), M(rt[u] + 1, -w); }
	ll Query(int u) { return Q(lt[u]); }
}
void DFS(int u, int fa) {
	st[0][lt[u] = ++cti] = prt[u] = fa;
	for (int i = head[u]; i; i = e[i].nxt) {
		int v = e[i].v;
		if (v == fa) continue;
		dpt[i >> 1] = v;
		DFS(v, u);
		BIT::Modify(v, we[i >> 1]);
	}
	rt[u] = cti;
}
int PMin(int u, int v) { return lt[u] < lt[v] ? u : v; }
void PreST() {
	for (int j = 1; j < 20; j++) {
		for (int i = 1; i + (1 << j) - 1 <= n; i++) {
			st[j][i] = PMin(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
		}
	}
}
int LCA(int u, int v) {
	if (u == v) return u;
	if ((u = lt[u]) > (v = lt[v])) swap(u, v);
	int x = __lg(v - u++);
	return PMin(st[x][u], st[x][v - (1 << x) + 1]);
}
int ubn[200005], dbn[200005];
vector<int> clu[200005], bn;
namespace TopCluster {
	int ccl[200005], ccnt, st[200005], top;
	void AddCl(int u, int v) {
		if (!v) v = ccl[ccnt];
		for (int i = 1; i <= ccnt; i++) {
			int r = ccl[i];
			ubn[r] = u, dbn[r] = v, clu[v].push_back(r);
		}
		ccnt = 0;
	}
	int wai[200005], rec[200005], ctp[200005];
	void DFS(int u, int fa) {
		ctp[u] = top, wai[u] = 1;
		int bcnt = 0;
		for (int i = head[u]; i; i = e[i].nxt) {
			int v = e[i].v;
			if (v == fa) continue;
			st[++top] = v;
			DFS(v, u);
			wai[u] += wai[v];
			if (rec[v]) rec[u] = rec[v], bcnt++;
		}
		if (wai[u] > B || bcnt > 1 || !fa) {
			wai[u] = 0, rec[u] = u;
			int j = ctp[u] + 1, cnt = 0, cdn = 0;
			for (int i = head[u]; i; i = e[i].nxt) {
				int v = e[i].v;
				if (v == fa) continue;
				if (cnt + wai[v] > B || (rec[v] && cdn)) {
					for (; j < ctp[v] && j <= top; j++) ccl[++ccnt] = st[j];
					AddCl(u, cdn), cdn = cnt = 0;
				}
				cnt += wai[v];
				if (rec[v]) cdn = rec[v];
			}
			for (; j <= top; j++) ccl[++ccnt] = st[j];
			if (u == 1) ccl[++ccnt] = 1;
			AddCl(u, cdn);
			top = ctp[u];
		}
	}
}
ll du[200005], dd[200005];
void ReDFS(int u, int fa, int col, ll dis, ll *a) {
	if (dbn[u] == col) a[u] = dis;
	else if (u != ubn[col]) return;
	for (int i = head[u]; i; i = e[i].nxt) {
		int v = e[i].v;
		if (v == fa) continue;
		ReDFS(v, u, col, dis + we[i >> 1], a);
	}
}
vector<int> pu[200005], pd[200005];
void Modify(int x, ll y) {
	BIT::Modify(dpt[x], y - we[x]);
	we[x] = y;
	int b = dbn[dpt[x]], a = ubn[b];
	ReDFS(a, 0, b, 0, du), ReDFS(b, 0, b, 0, dd);
	sort(pu[b].begin(), pu[b].end(), [](int x, int y) {
		return du[x] < du[y];
	});
	sort(pd[b].begin(), pd[b].end(), [](int x, int y) {
		return dd[x] < dd[y];
	});
}
ll D(int u, int v) {
	return BIT::Query(u) + BIT::Query(v) - 2 * BIT::Query(LCA(u, v));
}
int Query(int x, ll y) {
	int ans = 0;
	for (auto u : clu[dbn[x]]) {
		if (D(u, x) <= y) ans++;
	}
	for (auto u : bn) {
		if (u == dbn[x]) continue;
		ll da = D(ubn[u], x), db = D(u, x);
		if (da < db) {
			du[n + 1] = y - da;
			ans += upper_bound(pu[u].begin(), pu[u].end(), n + 1, [](int x, int y) {
				return du[x] < du[y];
			}) - pu[u].begin();
		}
		else {
			dd[n + 1] = y - db;
			ans += upper_bound(pd[u].begin(), pd[u].end(), n + 1, [](int x, int y) {
				return dd[x] < dd[y];
			}) - pd[u].begin();
		}
	}
	return ans;
}
int main() {
	scanf("%d%d", &n, &q);
	for (int i = 1, u, v, w; i < n; i++) {
		scanf("%d%d%d", &u, &v, &w);
		Insert(u, v), Insert(v, u);
		we[i] = w;
	}
	DFS(1, 0), PreST(), TopCluster::DFS(1, 0);
	for (int i = 1; i <= n; i++) {
		bn.push_back(dbn[i]);
		pu[i] = pd[i] = clu[i];
	}
	sort(bn.begin(), bn.end());
	bn.erase(unique(bn.begin(), bn.end()), bn.end());
	for (int x : bn) {
		int b = x, a = ubn[b];
		ReDFS(a, 0, b, 0, du), ReDFS(b, 0, b, 0, dd);
		sort(pu[b].begin(), pu[b].end(), [](int x, int y) {
			return du[x] < du[y];
		});
		sort(pd[b].begin(), pd[b].end(), [](int x, int y) {
			return dd[x] < dd[y];
		});
	}
	while (q--) {
		int op, x, y; scanf("%d%d%d", &op, &x, &y);
		if (op == 1) Modify(x, y);
		else printf("%d\n", Query(x, y));
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 34084kb

input:

3 7
1 2 3
2 3 1
2 2 1
2 1 3
2 3 4
1 1 1
2 2 1
2 1 0
2 3 1

output:

2
2
3
3
1
2

result:

ok 6 numbers

Test #2:

score: -100
Wrong Answer
time: 6436ms
memory: 70132kb

input:

200000 200000
1 2 146181238
2 3 45037818
3 4 176924060
4 5 969365276
5 6 683948619
6 7 356268194
7 8 871634797
8 9 630254480
9 10 283061123
10 11 204904965
11 12 838381393
12 13 516373812
13 14 253862710
14 15 223572474
15 16 114452997
16 17 145251056
17 18 905638436
18 19 375445402
19 20 549829545
...

output:

3
0
0
7
0
7
4
4
2
6
0
0
10
0
0
3
0
0
1
0
0
0
0
0
0
0
10
0
7
0
0
0
1
8
2
0
0
0
4
3
0
0
6
0
2
2
0
8
1
6
0
3
0
0
3
0
3
5
3
7
0
3
1
0
11
6
12
3
3
5
6
0
8
0
4
6
1
1
0
0
5
0
7
0
0
5
8
2
0
2
5
0
0
9
2
0
0
3
5
0
0
6
0
0
0
0
0
0
8
0
11
0
0
3
4
0
0
6
0
2
0
2
3
0
0
0
1
2
0
0
0
7
0
0
6
8
0
0
1
7
11
0
4
5
1
3
1
...

result:

wrong answer 1st numbers differ - expected: '219', found: '3'