QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#532826#8235. Top Clusterjasper166WA 688ms97588kbC++172.6kb2024-08-25 12:58:162024-08-25 12:58:17

Judging History

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

  • [2024-08-25 12:58:17]
  • 评测
  • 测评结果:WA
  • 用时:688ms
  • 内存:97588kb
  • [2024-08-25 12:58:16]
  • 提交

answer

#include "bits/stdc++.h"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;

#ifdef JASPER
#include "debug.h"
#else
#define debug(...) 166
#endif

const int N = 5e5 + 5;
const int LG = 20;

vector <pair <int, int>> adj[N];
int dep[N], up[LG][N], st[N];
int w[N];
ll dis[N];
int timer = 0;

int tour[2 * N], lg[2 * N];

int lca(int u, int v) {
    int l = st[u];
    int r = st[v];
    if (l > r)
        swap(l, r);
    int k = lg[r - l + 1];
    return (dep[up[k][l]] < dep[up[k][r - (1 << k) + 1]])? up[k][l] : up[k][r - (1 << k) + 1];
}
 
void build() {
    lg[1] = 0;
    for (int i = 2; i <= timer; i++)
        lg[i] = lg[i / 2] + 1;
    for (int i = 1; i <= timer; i++) 
        up[0][i] = tour[i];
 
    for (int k = 1; k < LG; k++)
        for (int i = 1; i + (1 << k) - 1 <= timer; i++)
            up[k][i] = (dep[up[k - 1][i]] < dep[up[k - 1][i + (1 << (k - 1))]])?
                        up[k - 1][i] : up[k - 1][i + (1 << (k - 1))];
}
 
void dfs(int u, int p) {
    st[u] = ++timer;
    tour[timer] = u;
    for (const auto &[l, v] : adj[u]) {
        if (v == p)
            continue;
        dis[v] = dis[u] + l;
        dep[v] = dep[u] + 1;
        dfs(v, u);
        tour[++timer] = u;
    }
}

ll get(int u, int v) {
	int p = lca(u, v);
	return (dis[u] + dis[v] - 2LL * dis[p]);
}

int id1[N], id2[N];

signed main() {
    cin.tie(0) -> sync_with_stdio(0);
 
 	int n, q;
	cin >> n >> q;
	for (int i = 1; i <= n; ++i) cin >> w[i];
	for (int i = 1; i < n; ++i) {
		int u, v, l;
		cin >> u >> v >> l;
		adj[u].push_back({l, v});
		adj[v].push_back({l, u});
	}

	dfs(1, 0);
	build();

	vector <int> vis(n + 1, 0);
	vector <int> ord(n);

	for (int i = 1; i <= n; ++i) {
		if (w[i] <= n) vis[w[i]] = 1;
		ord[i - 1] = i;
	}

	sort(ord.begin(), ord.end(), [] (const int i, const int j) {
		return w[i] < w[j];
	});

	int mex = 0;
	while (vis[mex]) ++mex;

	id1[0] = id2[0] = ord[0];
	for (int i = 1; i < n; ++i) {
		int u = id1[i - 1], v = id2[i - 1];
		ll D = get(u, v);
		ll D1 = get(ord[i], u);
		ll D2 = get(ord[i], v);
		if (D1 > D2) {
			if (D1 > D) v = ord[i];
		}
		else if (D2 > D) u = ord[i];
		id1[i] = u;
		id2[i] = v;
	}

	for (int _q = 1; _q <= q; ++_q) {
		int x; ll k;
		cin >> x >> k;
		if (k == 0) {
			int ans = (w[x] == 0);
			cout << ans << "\n";	
			continue;	
		}

		ll l = 0, r = mex;
		while (l < r) {
			ll m = (l + r) / 2;
			if (max(get(id1[m], x), get(id2[m], x)) <= k) {
				l = m + 1;
			} 
			else 
				r = m;
		}

		cout << l << "\n";
	}

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 33724kb

input:

5 4
3 9 0 1 2
1 2 10
3 1 4
3 4 3
3 5 2
3 0
1 0
4 6
4 7

output:

1
0
3
4

result:

ok 4 number(s): "1 0 3 4"

Test #2:

score: -100
Wrong Answer
time: 688ms
memory: 97588kb

input:

500000 500000
350828 420188 171646 209344 4 999941289 289054 79183 999948352 427544 160827 138994 192204 108365 99596 999987124 292578 2949 384841 269390 999920664 315611 163146 51795 265839 34188 999939494 145387 366234 86466 220368 357231 347706 332064 279036 173185 5901 217061 112848 37915 377359...

output:

0
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
2499...

result:

wrong answer 20939th numbers differ - expected: '4', found: '0'