QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#648260#8235. Top ClusterCalculatelove#WA 1342ms69364kbC++142.7kb2024-10-17 17:58:322024-10-17 17:58:33

Judging History

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

  • [2024-10-17 17:58:33]
  • 评测
  • 测评结果:WA
  • 用时:1342ms
  • 内存:69364kb
  • [2024-10-17 17:58:32]
  • 提交

answer

#include <bits/stdc++.h>

template <class T>
inline void read(T &x) {
	static char s;
	static bool opt;
	while (s = getchar(), (s < '0' || s > '9') && s != '-');
	x = (opt = s == '-') ? 0 : s - '0';
	while (s = getchar(), s >= '0' && s <= '9') x = x * 10 + s - '0';
	if (opt) x = ~x + 1;
}

typedef long long s64;

const int N = 500100;

int n, Q;
int a[N], 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 Fa[N];
int anc[19][N];
int dep[N];
s64 dist[N];

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

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

        Fa[v] = u;
        dist[v] = dist[u] + w;

        dfs(v);
    }
}

int lca(int x, int y) {
    if (dep[x] > dep[y]) std::swap(x, y);
    for (int i = 18; i >= 0; i --)
        if (dep[x] <= dep[y] - (1 << i)) y = anc[i][y];
    if (x == y) return x;
    for (int i = 18; i >= 0; i --)
        if (anc[i][x] ^ anc[i][y]) x = anc[i][x], y = anc[i][y];
    return anc[0][x];
}

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

int px[N], qx[N];

int main() {
    read(n), read(Q);

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

    for (int i = 1, x, y, z; i < n; i ++) {
        read(x), read(y), read(z);
        add_edge(x, y, z), add_edge(y, x, z);
    }

    dfs(1);

    for (int i = 1; i <= n; i ++) anc[0][i] = Fa[i];
    for (int j = 1; j <= 18; j ++)
        for (int i = 1; i <= n; i ++) anc[j][i] = anc[j - 1][anc[j - 1][i]];

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

    int pX = px[1] = id[1], qX = qx[1] = id[1];
    s64 dis = 0;
    for (int i = 2; i <= p; i ++) {
        int u = id[i], tmpP, tmpQ;

        tmpP = calc(u, pX);
        if (tmpP > dis) dis = tmpP, pX = px[i - 1], qX = u;

        tmpQ = calc(u, qX);
        if (tmpQ > dis) dis = tmpQ, pX = u, qX = qx[i - 1];

        px[i] = pX, qx[i] = qX;
    }

    while (Q --) {
        int x, k;
        read(x), read(k);

        if (p == -1) {
            puts("0");
        } else {
            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: 52940kb

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: 0
Accepted
time: 1162ms
memory: 69364kb

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:

ok 500000 numbers

Test #3:

score: -100
Wrong Answer
time: 1342ms
memory: 69296kb

input:

500000 500000
416779 59604 366180 195604 4 30957 999969109 7476 352690 368624 121597 999960303 999933891 13 14 138579 294015 227392 106760 117837 208506 999997971 34770 40258 182765 65889 206246 233051 130491 182099 117381 241945 449750 155921 356191 999955435 2243 450904 242106 178163 148523 75648 ...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

wrong answer 2nd numbers differ - expected: '250002', found: '0'