QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#779704#8235. Top Clusterliuyz11RE 0ms32020kbC++143.8kb2024-11-24 21:18:362024-11-24 21:18:38

Judging History

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

  • [2024-11-24 21:18:38]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:32020kb
  • [2024-11-24 21:18:36]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(x, l, r) for(int x = l; x <= r; x++)
#define repd(x, r, l) for(int x = r; x >= l; x--)
#define clr(x, y) memset(x, y, sizeof(x))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define SZ(x) ((int)x.size())
#define int long long
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;
const int MAXN = 5e5 + 5;
int cnt, tot;
int head[MAXN], nxt[MAXN * 2], to[MAXN * 2], value[MAXN * 2];
int dfn[MAXN * 20], depth[MAXN * 20], pos[MAXN], sum[MAXN], f[MAXN];
int lg[MAXN * 20], st[MAXN][35], rev[MAXN][35];

pii res[MAXN];

void init(){
    cnt = tot = 0;
    clr(head, -1);
}

void addedge(int u, int v, int w){
    nxt[cnt] = head[u];
    head[u] = cnt;
    to[cnt] = v;
    value[cnt] = w;
    cnt++;
}

void dfs(int u, int dep){
    dfn[++tot] = u;
    depth[tot] = dep;
    pos[u] = tot;
    for(int e = head[u]; e != -1; e = nxt[e]){
        int v = to[e];
        if(!pos[v]){
            sum[v] = sum[u] + value[e];
            dfs(v, dep + 1);
            dfn[++tot] = u;
            depth[tot] = dep;
        }
    }
}

int lca(int l, int r){
    int k = lg[r - l + 1];
    return st[l][k] < st[r + 1 - (1 << k)][k] ? rev[l][k] : rev[r + 1 - (1 << k)][k];
}

int query(int x, int y){
    int rt = lca(min(pos[x], pos[y]), max(pos[x], pos[y]));
    // printf("    %d %d %d\n", x, y, rt);
    return sum[x] + sum[y] - 2 * sum[rt];
}

int check(int x, int k, int mid){
    if(res[mid].fi == -1 || res[mid].se == -1) return 0;
    int ans = 0;
    if(res[mid].fi) ans = max(ans, query(x, res[mid].fi));
    if(res[mid].se) ans = max(ans, query(x, res[mid].se));
    // printf("%d %d %d : %d %d : %d\n", x, k, mid, res[mid].fi, res[mid].se, ans);
    // printf("%d %d\n", )
    return ans <= k;
}

signed main(){
    int n, q;
    scanf("%lld%lld", &n, &q);
    clr(f, -1);
    rep(i, 1, n){
        int x;
        scanf("%lld", &x);
        if(x < n) f[x] = i;
    }
    init();
    rep(i, 1, n - 1){
        int u, v, w;
        // printf("%lld\n", i);
        scanf("%lld%lld%lld", &u, &v, &w);
        addedge(u, v, w);
        addedge(v, u, w);
    }
    dfs(1, 1);
    // puts("xxxx");
    rep(i, 2, tot + 1) lg[i] = lg[i >> 1] + 1;
    rep(i, 1, tot) st[i][0] = depth[i], rev[i][0] = dfn[i];
    rep(j, 1, lg[tot]){
        rep(i, 1, tot - (1 << j) + 1){
            if(st[i][j - 1] < st[i + (1 << (j - 1))][j - 1]){
                st[i][j] = st[i][j - 1];
                rev[i][j] = rev[i][j - 1];
            }
            else{
                st[i][j] = st[i + (1 << (j - 1))][j - 1];
                rev[i][j] = rev[i + (1 << (j - 1))][j - 1];
            }
        }
    }
    res[0] = mp(f[0], 0);
    res[1] = mp(f[0], f[1]);
    rep(i, 2, n - 1){
        if(res[i - 1].fi == -1 || res[i - 1].se == -1 || f[i] == -1){
            res[i].fi = res[i].se = -1;
            continue;
        }
        int s1 = query(res[i - 1].fi, res[i - 1].se),
        s2 = query(res[i - 1].fi, f[i]),
        s3 = query(res[i - 1].se, f[i]);
        if(s1 >= s2 && s1 >= s3) res[i] = res[i - 1];
        else if(s2 >= s1 && s2 >= s3) res[i] = mp(res[i - 1].fi, f[i]);
        else res[i] = mp(res[i - 1].se, f[i]);
    }
    // rep(i, 0, 3) printf("%d : %d %d\n", i, res[i].fi, res[i].se);
    // puts("xxx");
    while(q--){
        int x, k;
        scanf("%lld%lld", &x, &k);
        int l = 0, r = n - 1, ans = -1;
        while(l <= r){
            int mid = (l + r) >> 1;
            if(check(x, k, mid)){
                ans = mid;
                l = mid + 1;
            }
            else r = mid - 1;
        }
        printf("%lld\n", ans + 1);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
Runtime Error

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:


result: