QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#657222#8235. Top ClusterCalculateloveWA 354ms118180kbC++142.8kb2024-10-19 14:24:042024-10-19 14:24:05

Judging History

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

  • [2024-10-19 14:24:05]
  • 评测
  • 测评结果:WA
  • 用时:354ms
  • 内存:118180kb
  • [2024-10-19 14:24:04]
  • 提交

answer

#include <bits/stdc++.h>

typedef long long s64;

const int N = 500100;

int n, Q;
int a[N];

int id[N];
bool cmp(int x, int y) {
    return a[x] < a[y];
}

int tot, head[N], ver[N * 2], edge[N * 2], Next[N * 2];
void add_edge(int u, int v, int w) {
    ver[++ tot] = v;    edge[tot] = w;    Next[tot] = head[u];    head[u] = tot;
}

int dep[N];
s64 dist[N];

int Fir[N];
int eul_len, eul[N * 2];

void dfs(int u, int fu) {
    dep[u] = dep[fu] + 1;

    eul[++ eul_len] = u, Fir[u] = eul_len;

    for (int i = head[u]; i; i = Next[i]) {
        int v = ver[i], w = edge[i];
        if (v == fu) continue;

        dist[v] = dist[u] + w;
        dfs(v, u), eul[++ eul_len] = u;
    }
}

namespace eul_ST {
    int logx[N * 2];
    int f[20][N * 2];

    int great(int x, int y) {
        return dep[x] < dep[y] ? x : y;
    }

    void init() {
        logx[0] = -1;
        for (int i = 1; i <= eul_len; i ++) logx[i] = logx[i >> 1] + 1;

        for (int i = 1; i <= eul_len; i ++) f[0][i] = eul[i];
        for (int j = 1; j <= 19; j ++)
            for (int i = 1; i <= eul_len - (1 << j) + 1; i ++)
                f[j][i] = great(f[j - 1][i], f[j - 1][i + (1 << (j - 1))]);
    }

    int query(int l, int r) {
        int k = logx[r - l + 1];
        return great(f[k][l], f[k][r - (1 << k) + 1]);
    }
}

int lca(int x, int y) {
    int l = Fir[x], r = Fir[y]; if (l > r) std::swap(l, r);
    return eul_ST::query(l, r);
}

s64 calc(int x, int y) {
    return dist[x] + dist[y] - 2 * dist[lca(x, y)];
}

int px[N], qx[N];

int main() {
    scanf("%d%d", &n, &Q);

    for (int i = 1; i <= n; i ++)
        scanf("%d", &a[i]);

    for (int i = 1; i <= n; i ++)
        id[i] = i;
    std::sort(id + 1, id + 1 + n, cmp);

    for (int i = 1, u, v, w; i < n; i ++) {
        scanf("%d%d%d", &u, &v, &w);
        add_edge(u, v, w), add_edge(v, u, w);
    }

    dfs(1, 0);

    eul_ST::init();

    int p = 0;
    for (int i = 1; i <= n + 1; i ++)
        if (a[id[i]] != i - 1) {
            p = i - 2;
            break;
        }

    int P, Q; s64 max_dist = 0;
    px[0] = P = id[1], qx[0] = Q = id[1];
    for (int i = 1; i <= p; i ++) {
        int u = id[i]; s64 tmp_dist;
        
        tmp_dist = calc(u, px[i - 1]);
        if (tmp_dist > max_dist) max_dist = tmp_dist, P = px[i - 1], Q = u;

        tmp_dist = calc(u, qx[i - 1]);
        if (tmp_dist > max_dist) max_dist = tmp_dist, P = u, Q = qx[i - 1];

        px[i] = P, qx[i] = Q;
    }

    while (Q --) {
        int x; s64 k;
        scanf("%d%lld", &x, &k);

        int l = 0, r = p + 1;
        while (l < r) {
            int mid = (l + r + 1) >> 1;
            if (calc(x, px[mid]) <= k && calc(x, qx[mid]) <= k) l = mid; else r = mid - 1;
        }

        printf("%d\n", l);
    }

    return 0;
}

詳細信息

Test #1:

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

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: 354ms
memory: 118180kb

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 Answer contains longer sequence [length = 500000], but output contains 157226 elements