QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#771764#8235. Top ClusterbillfWA 420ms126660kbC++143.3kb2024-11-22 15:31:492024-11-22 15:31:50

Judging History

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

  • [2024-11-22 15:31:50]
  • 评测
  • 测评结果:WA
  • 用时:420ms
  • 内存:126660kb
  • [2024-11-22 15:31:49]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 5e5 + 10, L = 21;
int n, q, fth[N], a[N], b[N], dep[N], stg[N * 2], lm, in[N], ord[N * 2];
int tot, to[N * 2], nxt[N * 2], fst[N], wei[N * 2], up;
LL dis[N];
void add(int ui, int vi, int wi)
{
    to[++tot] = vi, wei[tot] = wi, nxt[tot] = fst[ui];
    fst[ui] = tot;
}
struct ST
{
    int f[L][N * 2], Lg[N * 2], *ay;
    void build(int *ary, int ln)
    {
        ay = ary;
        Lg[0] = -1;
        for (int i = 1; i <= ln; i++)
            Lg[i] = Lg[i >> 1] + 1;
        int p1, p2;
        iota(f[0] + 1, f[0] + ln + 1, 1);
        for (int j = 1; 1 << j <= ln; j++)
            for (int i = 1; i + (1 << j) - 1 <= ln; i++)
            {
                p1 = f[j - 1][i], p2 = f[j - 1][i + (1 << j - 1)];
                if (ary[p1] > ary[p2])
                    f[j][i] = p2;
                else
                    f[j][i] = p1;
            }
    }
    int ask(int l, int r)
    {
        if (l > r)
            return 0;
        int ln = Lg[r - l + 1], p1 = f[ln][l], p2 = f[ln][r - (1 << ln) + 1];
        if (ay[p1] > ay[p2])
            return p2;
        else
            return p1;
    }
} rq;
void dfs(int x, int fa)
{
    fth[x] = fa;
    dep[x] = dep[fa] + 1;
    in[x] = ++tot;
    ord[tot] = x;
    for (int i = fst[x]; i; i = nxt[i])
    {
        int y = to[i], z = wei[i];
        if (y == fa)
            continue;
        dis[y] = dis[x] + z;
        dfs(y, x);
        ord[++tot] = x;
    }
}
LL dist(int x, int y)
{
    if (in[x] > in[y])
        swap(x, y);
    return dis[x] + dis[y] - 2 * dis[ord[rq.ask(in[x], in[y])]];
}
struct chain
{
    int x, y;
    LL d;
    LL dit()
    {
        return d = dist(x, y);
    }
    friend chain operator+(const chain a, const int b)
    {
        int mx;
        chain c, d, e;
        c = d = e = a;
        c.y = b;
        d.x = b;
        mx = max(c.dit(), max(d.dit(), e.dit()));
        if (c.d == mx)
            return c;
        if (d.d == mx)
            return d;
        return e;
    }
} pre[N];
int main()
{
    cin >> n >> q;
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", a + i);
        if (a[i] > n)
            continue;
        b[a[i]] = i;
    }
    for (int i = 0; i <= n; i++)
        if (!b[i])
        {
            up = i;
            break;
        }
    if (!up)
    {
        while (q--)
            puts("0");
        return 0;
    }
    for (int i = 1; i < n; i++)
    {
        int u, v, w;
        scanf("%d%d%d", &u, &v, &w);
        add(u, v, w), add(v, u, w);
    }
    tot = 0;
    dfs(1, 0);
    for (int i = 1; i <= tot; i++)
        stg[i] = dep[ord[i]];
    rq.build(stg, tot);
    pre[0].x = pre[0].y = b[0];
    pre[0].d = 0;
    for (int i = 1; i < up; i++)
        pre[i] = pre[i - 1] + b[i];
    while (q--)
    {
        int x, l = 0, r = up - 1, mid;
        LL y;
        scanf("%d%lld", &x, &y);
        while (l < r)
        {
            mid = l + r >> 1;
            if (max(dist(x, pre[mid].x), dist(x, pre[mid].y)) > y)
                r = mid;
            else
                l = mid + 1;
        }
        if (max(dist(x, pre[l].x), dist(x, pre[l].y)) <= y)
            l++;
        printf("%d\n", l);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 395ms
memory: 124016kb

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: 420ms
memory: 126660kb

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 57911th numbers differ - expected: '1349', found: '250002'