QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#499773#8235. Top Clustercz_yxxWA 2962ms92284kbC++113.0kb2024-07-31 19:09:132024-07-31 19:09:14

Judging History

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

  • [2024-07-31 19:09:14]
  • 评测
  • 测评结果:WA
  • 用时:2962ms
  • 内存:92284kb
  • [2024-07-31 19:09:13]
  • 提交

answer

// sis puella oier
#include <bits/stdc++.h>
using namespace std;
// #define int long long
typedef long long ll;
ll read(){
    ll xx = 0, f = 1; char ch = getchar();
    for (;!isdigit(ch); ch = getchar())
        f = (ch == '-' ? -1 : 1);
    for (; isdigit(ch); ch = getchar())
        xx = (xx << 3) + (xx << 1) + ch - '0';
    return xx * f;
}
const int N = 500100;
int n, q;
struct edge{
    int v, w;
};
vector <edge> e[N];
int a[N], dep[N], fa[19][N];
void dfs(int x, int fath){
    fa[0][x] = fath; for (int i = 1; i <= 18; ++i)fa[i][x] = fa[i - 1][fa[i - 1][x]];
    for (edge v : e[x])if (v.v != fath)dep[v.v] = dep[x] + v.w, dfs(v.v, x);
}
int lca(int x, int y){
    if (dep[x] < dep[y])swap(x, y);
    for (int i = 18; i >= 0; --i)if (dep[fa[i][x]] >= dep[y])x = fa[i][x];
    if (x == y)return x;
    for (int i = 18; i >= 0; --i)if (fa[i][x] != fa[i][y])x = fa[i][x], y = fa[i][y];
    return fa[0][x];
}
int dis(int x, int y){
    return dep[x] + dep[y] - 2 * dep[lca(x, y)];
}
edge qu[N];
int id[N], idl[N], idr[N], ans[N];
int dd[2][N];
void solve(int l, int r, int nl, int nr){
    if (nl > nr)return ;
    // cout<<l<<" "<<r<<endl;
    if (l == r){
        for (int i = nl; i <= nr; ++i)ans[id[i]] = l;
        return ;
    }
    int mid = (l + r) >> 1, cntl = 0, cntr = 0;
    // cout<<mid<<" :: "<<dd[0][mid]<<" "<<dd[1][mid]<<" : "<<qu[id[nl]].v<<endl;
    // cout<<max(dis(dd[0][mid], qu[id[nl]].v), dis(dd[1][mid], qu[id[nl]].v))<<endl;
    for (int i = nl; i <= nr; ++i)if (max(dis(dd[0][mid], qu[id[i]].v), dis(dd[1][mid], qu[id[i]].v)) > qu[id[i]].w)
        idl[++cntl] = id[i];
    else idr[++cntr] = id[i];
    for (int i = 1; i <= cntl; ++i)id[nl + i - 1] = idl[i];
    for (int i = 1; i <= cntr; ++i)id[nl + cntl + i - 1] = idr[i];
    solve(l, mid, nl, nl + cntl - 1), solve(mid + 1, r, nl + cntl, nr);
}
int vis[N];
signed main(){
    n = read(), q = read();
    for (int i = 1; i <= n; ++i){
        a[i] = read(); if (a[i] < N)vis[a[i]] = i;
    }
    // for (int i = 0; i <= n; ++i)cout<<vis[i]<<" ";cout<<endl;
    int mx = 0; while(vis[mx])++mx;
    for (int i = 1, u, v, w; i < n; ++i)u = read(), v = read(), w = read(),
    e[u].push_back(edge{v, w}), e[v].push_back(edge{u, w});
    dep[1] = 0, dfs(1, 0);
    dd[0][0] = vis[0], dd[1][0] = vis[0];
    for (int i = 1; i < mx; ++i){
        int xx = dis(vis[i], dd[0][i - 1]), yy = dis(vis[i], dd[1][i - 1]),
        zz = dis(dd[0][i - 1], dd[1][i - 1]);
        if (xx >= yy && xx >= zz)dd[0][i] = vis[i], dd[1][i] = dd[0][i - 1];
        if (yy >= xx && yy >= zz)dd[0][i] = vis[i], dd[1][i] = dd[1][i - 1];
        if (zz >= xx && zz >= yy)dd[0][i] = dd[0][i - 1], dd[1][i] = dd[1][i - 1];
    }
    // for (int i = 0; i < mx; ++i)cout<<dd[0][i]<<" "<<dd[1][i]<<endl; cout<<endl;
    for (int i = 1; i <= q; ++i)qu[i].v = read(), qu[i].w = read(), id[i] = i;
    solve(0, mx, 1, q);
    for (int i = 1; i <= q; ++i)printf("%d\n", ans[i]);
    return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 2962ms
memory: 92284kb

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: 2038ms
memory: 92272kb

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:

250002
0
250002
250002
250002
250002
250002
250002
0
0
0
0
250002
0
0
0
250002
0
250002
0
0
250002
250002
0
250002
0
250002
0
0
250002
0
27
0
0
0
0
27
250002
250002
0
250002
0
0
0
250002
0
0
27
27
0
0
0
0
250002
250002
250002
250002
250002
250002
0
0
0
0
250002
250002
250002
0
250002
250002
250002
0...

result:

wrong answer 1st numbers differ - expected: '0', found: '250002'