QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#527991#8235. Top Clusters1ameseWA 838ms229652kbC++202.5kb2024-08-23 01:41:552024-08-23 01:41:55

Judging History

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

  • [2024-08-23 01:41:55]
  • 评测
  • 测评结果:WA
  • 用时:838ms
  • 内存:229652kb
  • [2024-08-23 01:41:55]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int N = 1e6 + 10;

int n, q, ptr = 0, st[N], lg[N], dx[N], dy[N], dz[N];
i64 sp[N][20], d[N];
pair<i64, int> w[N];
vector<pair<i64, int>> e[N];

void dfs(int x, int p) {
    st[x] = ++ptr;  
    sp[ptr][0] = d[x];
    for (auto y : e[x]) {
        if (y.second == p)
            continue;
        d[y.second] = d[x] + y.first;
        dfs(y.second, x);
        sp[++ptr][0] = d[x];
    }
}

i64 dist(int u, int v) {
    i64 res = d[u] + d[v];
    u = st[u], v = st[v];
    if (u > v)
        swap(u, v);
    int k = lg[v - u + 1];
    i64 frog = min(sp[u][k], sp[v - (1 << k) + 1][k]);
    return res - frog * 2;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> q;
    for (int i = 1; i <= n; i++)
        cin >> w[i].first, w[i].second = i;

    for (int i = 1; i < n; i++) {
        int u, v, l;
        cin >> u >> v >> l;
        e[u].push_back({l, v}), e[v].push_back({l, u});
    }

    dfs(1, 0);

    for (int i = 2; i <= n * 2; i++)
        lg[i] = lg[i / 2] + 1;

    int m = n * 2 - 1;
    for (int i = 1; (1 << i) <= m; i++)
        for (int j = 1; j + (1 << i) <= m + 1; j++)
            sp[j][i] = min(sp[j][i - 1], sp[j + (1 << i - 1)][i - 1]);

    sort(w + 1, w + n + 1);

    int boss = 0;
    while (boss < n && w[boss + 1].first == boss)
        ++boss;

    if (!boss) {
        for (int i = 0; i < q; i++)
            cout << "0\n";
        return 0;
    }

    dx[1] = dy[1] = w[1].second, dz[1] = 0;
    for (int i = 2; i <= boss; i++) {
        i64 frog1 = dist(dx[i - 1], w[i].second);
        i64 frog2 = dist(dy[i - 1], w[i].second);
        if (frog1 > dz[i - 1] && frog1 > frog2) {
            dx[i] = dx[i - 1];
            dy[i] = w[i].second;
            dz[i] = frog1;
        }
        else if (frog2 > dz[i - 1] && frog2 >= frog1) {
            dy[i] = dy[i - 1];
            dx[i] = w[i].second;
            dz[i] = frog2;
        } else {
            dx[i] = dx[i - 1];
            dy[i] = dy[i - 1];
            dz[i] = dz[i - 1];
        }
    }

    for (int i = 0; i < q; i++) {
        int x;
        i64 d;
        cin >> x >> d;
        int l = 0, r = boss;
        while (l != r) {
            int mid = l + r + 1 >> 1;
            if (dist(x, dx[mid]) > d || dist(x, dy[mid]) > d)
                r = mid - 1;
            else
                l = mid;
        }
        cout << l << '\n';
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 15948kb

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: 651ms
memory: 226928kb

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: 838ms
memory: 229652kb

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
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
250002
2500...

result:

wrong answer 26750th numbers differ - expected: '0', found: '3918'