QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#622623#8235. Top ClusterAlphaZeWA 1898ms71296kbC++143.0kb2024-10-08 23:23:282024-10-08 23:23:29

Judging History

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

  • [2024-10-08 23:23:29]
  • 评测
  • 测评结果:WA
  • 用时:1898ms
  • 内存:71296kb
  • [2024-10-08 23:23:28]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define ll long long

inline ll read() {
    ll x = 0, f = 0; char ch = getchar();
    while (!isdigit(ch)) f = ch == '-', ch = getchar();
    while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
    return f ? -x : x;
}

const int N = 5e5 + 10;
int n, q;
int w[N], fw[N], m;
int head[N], nex[N << 1], ver[N << 1], val[N << 1], tot = 1;
int fa[N][21], dep[N]; ll dis[N];
int d0[N], d1[N];

void add(int x, int y, int z) {
    ver[++tot] = y; val[tot] = z; nex[tot] = head[x]; head[x] = tot;
}

void dfs(int x, int father) {
    fa[x][0] = father; dep[x] = dep[father] + 1;
    for (int i = 1; i <= 20; ++i)
        fa[x][i] = fa[fa[x][i - 1]][i - 1];
    for (int i = head[x]; i; i = nex[i]) {
        int y = ver[i], z = val[i];
        if (y == father) continue;
        dis[y] = dis[x] + z;
        dfs(y, x);
    }
}

int lca(int x, int y) {
    if (dep[x] < dep[y]) swap(x, y); // dep[x] >= dep[y]
    int depth = dep[x] - dep[y];
    for (int j = 20; j >= 0; --j)
        if (depth >> j & 1) x = fa[x][j];
    if (x == y) return x;
    for (int j = 20; j >= 0; --j)
        if (fa[x][j] != fa[y][j]) x = fa[x][j], y = fa[y][j];
    return fa[x][0];
}

ll getdis(int x, int y) {
    if ((!x) || (!y)) return 0;
    int p = lca(x, y);
    // printf("x = %d, y = %d, p = %d, dis[x] = %lld, dis[y] = %lld, dis[p] = %lld; \n", x, y, p, dis[x], dis[y], dis[p]);
    return dis[x] + dis[y] - 2 * dis[p];
}

bool check(int g, int x, ll K) {
    // printf("check(%d, %d, %lld), getdis(%d, %d) = %lld, getdis(%d, %d) = %lld; \n", g, x, K, x, d0[g], getdis(x, d0[g]), x, d1[g], getdis(x, d1[g]));
    if (g == m) return false;
    if (getdis(x, d0[g]) <= K && getdis(x, d1[g]) <= K) return true;
    return false;
}

int main() {
    n = read(); q = read();
    for (int i = 1; i <= n; ++i) {
        w[i] = read() + 1;
        if (w[i] <= n) fw[w[i]] = i;
    }
    for (int i = 1; i < n; ++i) {
        int u = read(), v = read(), w = read();
        add(u, v, w); add(v, u, w);
    }
    dfs(1, 0);
    m = n + 1;
    for (int i = 1; i <= n; ++i) {
        if (!fw[i]) { m = i; break; }
    }
    for (int i = 1; i < m; ++i) {
        if (i == 1) d0[i] = fw[i];
        else if (i == 2) d0[i] = d0[i - 1], d1[i] = fw[i];
        else {
            ll dis0 = getdis(d0[i], fw[i]), dis1 = getdis(d1[i], fw[i]), dis2 = getdis(d0[i], d1[i]);
            if (dis2 > dis0 && dis2 > dis1) d0[i] = d0[i - 1], d1[i] = d1[i - 1];
            else if (dis0 > dis1) d0[i] = d0[i - 1], d1[i] = fw[i];
            else d0[i] = fw[i], d1[i] = d1[i - 1];
        }
        // printf("d0[%d] = %d, d1[%d] = %d; \n", i, d0[i], i, d1[i]);
    }
    while (q--) {
        int x = read(); ll K = read();
        int l = 1, r = m;
        while (l < r) {
            int mid = (l + r) >> 1;
            if (check(mid, x, K)) l = mid + 1;
            else r = mid;
        }
        printf("%d\n", l - 1);
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 1898ms
memory: 71296kb

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 6721st numbers differ - expected: '0', found: '9912'