QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#548184#8235. Top Clusterpengpeng_fudan#WA 676ms413116kbC++232.8kb2024-09-05 16:16:472024-09-05 16:16:47

Judging History

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

  • [2024-09-05 16:16:47]
  • 评测
  • 测评结果:WA
  • 用时:676ms
  • 内存:413116kb
  • [2024-09-05 16:16:47]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long

using namespace std;

const int N = 1e6 + 10;

struct Line {
    int x, y, dis;
};

struct Que {
    int x, k, id;
};

int n, m;
Line stk[N];
vector<pair<int, int>> path[N];
int dis[N], dep[N], siz[N];
// vector<pair<int, int>>
int tim; 
pair<int, int> dfn[N];
pair<int, int> minn[22][N];
int st[N], ed[N];
int pos[N];
int w[N];
int maxn;
int ans[N];
int lg[N];
Que q[N], tmp[N];

void dfs(int u, int _fa) {
    dep[u] = dep[_fa] + 1;
    dfn[++tim] = make_pair(dep[u], u);
    st[u] = tim;
    for (auto it : path[u]) {
        int v = it.first, w = it.second;
        if (v == _fa) continue;
        dis[v] = dis[u] + w;
        dfs(v, u);
        dfn[++tim] = make_pair(dep[u], u);
        ed[u] = tim;
    }
}

int lca(int x, int y) {
    if (st[x] > st[y]) swap(x, y);
    int l = st[x], r = st[y];
    int k = lg[r - l + 1];
    return min(minn[k][l], minn[k][r - (1 << k) + 1]).second;

}

int getdis(int x, int y) {
    int l = lca(x, y);
    return dis[x] + dis[y] - 2 * dis[l];
}

Line merge(Line a, int b) {
    int dis1 = getdis(a.x, b);
    int dis2 = getdis(a.y, b);
    if (dis1 < a.dis && dis2 < a.dis) return a;
    if (dis1 > dis2) {
        a.y = b;
    }
    else {
        a.x = b;
    }
    return a;
}

signed main() {
    lg[1] = 0;
    for (int i = 2; i <= 1000000; ++i) {
        lg[i] = lg[i >> 1] + 1;
    }
    scanf("%lld%lld", &n, &m);
    for (int i = 1; i <= n; ++i) {
        scanf("%lld", &w[i]);
        if (w[i] <= n) {
            pos[w[i]] = i;
        }
    }
    for (int i = 0; i <= n + 2; ++i) {
        if (!pos[i]) {
            maxn = i;
            break;
        }
    }
    for (int i = 1; i < n; ++i) {
        int u, v, l;
        scanf("%lld%lld%lld", &u, &v, &l);
        path[u].emplace_back(v, l);
        path[v].emplace_back(u, l);
    }
    dfs(1, 0);
    // for (int k = )
    for (int i = 1; i <= tim; ++i) minn[0][i] = dfn[i];
    for (int k = 1; k <= 20; ++k) {
        for (int i = 1; i <= tim - (1 << k) + 1; ++i) {
            minn[k][i] = min(minn[k - 1][i], minn[k - 1][i + (1 << (k - 1))]);
        }
    }
    // for (int i = 1; i <= n; ++i) {
    //     for (int j = 1; j <= n; ++j) {
    //         cerr << lca(i, j) << ' ' ;
    //     }
    //     cerr << '\n';
    // }
    stk[0] = (Line){pos[0], pos[0], 0};
    for (int i = 1; i <= maxn - 1; ++i) {
        stk[i] = merge(stk[i - 1], pos[i]);
    }
    for (int i = 1; i <= m; ++i) {
        int x, k;
        scanf("%lld%lld", &x, &k);
        int l = 0, r = maxn;
        while (l < r) {
            int mid = (l + r) >> 1;
            if (max(getdis(x, stk[mid].x), getdis(x, stk[mid].y)) <= k) l = mid + 1;
            else r = mid;
        }
        printf("%lld\n", l);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 32556kb

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: 676ms
memory: 413116kb

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 36936th numbers differ - expected: '14', found: '17'