QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#548103#8235. Top Clusterpengpeng_fudan#TL 4ms16132kbC++233.2kb2024-09-05 15:29:462024-09-05 15:29:47

Judging History

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

  • [2024-09-05 15:29:47]
  • 评测
  • 测评结果:TL
  • 用时:4ms
  • 内存:16132kb
  • [2024-09-05 15:29:46]
  • 提交

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], fa[N], top[N], son[N];
int pos[N];
int w[N];
int maxn;
int ans[N];
Que q[N], tmp[N];

void dfs1(int u, int _fa) {
    fa[u] = _fa;
    dep[u] = dep[_fa] + 1;
    siz[u] = 1;
    for (auto it : path[u]) {
        int v = it.first, w = it.second;
        if (v == _fa) continue;
        dis[v] = dis[u] + w;
        dfs1(v, u);
        if (siz[v] > siz[son[u]]) son[u] = v;
    }
}

void dfs2(int u, int _top) {
    top[u] = _top;
    if (son[u]) dfs2(son[u], _top);
    for (auto it : path[u]) {
        int v = it.first;
        if (v == son[u] || v == fa[u]) continue;
        dfs2(v, v);
    }
}

int lca(int x, int y) {
    while (top[x] != top[y]) {
        if (dep[top[x]] < dep[top[y]]) swap(x, y);
        x = fa[top[x]];
    }
    return dep[x] < dep[y] ? x : y;
}

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

// void solve(int l, int r, int L, int R) {
//     if (l == r) {
//         for (int i = L; i <= R; ++i) {
//             ans[q[i].id] = l;
//         }
//         return ;
//     }
//     int mid = (l + r) >> 1;
//     for (int i = l; i <= mid; ++i) {
//         if (i == 0) stk[i] = (Line){pos[i], pos[i], 0};
//         else stk[i] = merge(stk[i - 1], pos[i]);
//     }
//     for (int i = L; i <= R; ++i) tmp[i] = q[i];
//     int L1 = L, L2 = R;
//     for (int i = L; i <= R; ++i) {
//         if (Getdis(tmp[i].x, stk[mid]) <= tmp[i].k) {
//             q[L2] = tmp[i];
//             --L2;
//         }
//         else {
//             q[L1] = tmp[i];
//             ++L1;
//         }
//     }
    
//     // for (int i = L1; i <= )
//     // if (mid)
// }

Line merge(Line a, int b) {
    // if ()
    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() {
    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);
    }
    dfs1(1, 0);
    dfs2(1, 1);
    stk[0] = (Line){pos[0], pos[0], 0};
    for (int i = 1; i <= maxn; ++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);
    }
    // for (i)
    // int l = 0, r = n;
    // solve(l, r, 1, m);
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 16132kb

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
Time Limit Exceeded

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: