QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#594362#9238. TreeA_programmer0 0ms0kbC++172.1kb2024-09-27 22:10:042024-09-27 22:10:05

Judging History

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

  • [2024-09-27 22:10:05]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2024-09-27 22:10:04]
  • 提交

answer

#include "tree.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> vi;
const int maxn = 2e5 + 5;

int n, dfn[maxn], siz[maxn], id[maxn], rk[maxn], cnt;
int f[18][maxn], h[18][maxn], w[maxn];
ll suf[maxn], val[maxn], suml;
vi g[maxn];

struct BIT
{
    int c[maxn];
    void add(int x, int d) { for (; x < maxn; x += (x & -x)) c[x] += d; }
    int sum(int x) { if (!x) return 0; int ans = 0; for (; x; x -= (x & -x)) ans += c[x]; return ans; }
    int que(int u) { return sum(dfn[u] + siz[u] - 1) - sum(dfn[u] - 1); }
}bit;

void dfs(int u)
{
    dfn[u] = ++cnt, siz[u] = 1;
    for (int v : g[u]) dfs(v), siz[u] += siz[v];
}

void init(vi P, vi W)
{
    n = P.size(); cnt = 0;
    for (int i = 1; i <= n; i++) bit.c[i] = 0;
    for (int i = 1; i <= n; i++) suf[i] = val[i] = 0, g[i].clear(); suml = 0;
    for (int i = 1; i <= n; i++)
    {
        id[i] = i, w[i] = W[i - 1], f[0][i] = P[i - 1] + 1;
        if (i > 1) g[P[i - 1] + 1].emplace_back(i);
    }
    sort(id + 1, id + n + 1, [&](int a, int b) { return w[a] < w[b]; }); dfs(1);
    for (int i = 1; i <= n; i++) rk[id[i]] = i;

    for (int i = 1; i <= n; i++) h[i][0] = rk[i];
    for (int j = 1; j <= 17; j++)
        for (int i = 1; i <= n; i++)
            f[j][i] = f[j - 1][f[j - 1][i]], h[j][i] = min(h[j - 1][i], h[j - 1][f[j - 1][i]]);
    
    for (int i = 1; i <= n; i++) if (!g[i].size()) suml += w[i], bit.add(dfn[i], 1);
    for (int i = 1; i <= n; i++)
    {
        int u = id[i], nw = u;
        if (!g[u].size()) continue;
        for (int j = 17; ~j; j--) if (h[j][nw] >= i && rk[f[j][nw]] >= i) nw = f[j][nw];
        int x = bit.que(nw), y = bit.que(u) - 1;
        val[x] += w[u] - w[f[0][nw]]; val[x - y] -= w[u] - w[f[0][nw]];
        bit.add(dfn[u], -y); if (f[0][nw]) bit.add(dfn[f[0][nw]], y);
    }

    for (int i = n - 1; i; i--) val[i] += val[i + 1];
    suf[n + 1] = 0; for (int i = n; i; i--) suf[i] = suf[i + 1] + val[i];
}

ll query(int l, int r)
{
    int x = min(n + 1, (r / l) + 1);
    return val[x] * (r % l) + (suml + suf[x]) * l;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 0
Runtime Error

input:

ZYKrr4gCMcKeyfk6kbZU5k4ZyW3sAGT0
200000
0 0 2 2 4 5 4 5 8 9 10 9 8 10 14 15 14 15 18 19 20 21 18 22 21 24 24 27 22 27 30 31 31 33 30 19 20 33 38 38 40 41 41 43 44 43 44 47 48 49 50 49 50 53 54 53 48 54 58 58 60 60 62 62 64 64 66 66 67 67 70 71 72 71 72 75 70 75 78 78 80 81 80 81 84 85 86 86 88 89 90...

output:


result:


Subtask #2:

score: 0
Runtime Error

Test #11:

score: 0
Runtime Error

input:

ZYKrr4gCMcKeyfk6kbZU5k4ZyW3sAGT0
2000
0 0 1 1 4 4 6 7 6 7 10 10 12 12 14 15 14 15 18 19 19 21 18 21 24 24 26 27 26 27 30 30 32 32 34 34 36 37 38 39 39 41 37 38 36 41 46 47 48 47 48 51 51 53 54 54 56 56 58 58 60 61 61 63 64 64 66 67 66 67 70 71 72 72 74 75 76 76 75 74 70 77 63 60 77 85 85 87 87 89 89...

output:


result:


Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Runtime Error

Test #33:

score: 0
Runtime Error

input:

ZYKrr4gCMcKeyfk6kbZU5k4ZyW3sAGT0
200000
0 1 0 1 2 2 6 6 7 7 10 11 11 13 14 13 14 17 10 17 20 21 22 22 23 21 20 23 28 29 28 29 32 33 34 32 33 34 38 39 39 40 42 42 44 45 46 47 48 45 46 48 52 53 53 54 56 56 58 58 60 61 62 63 63 65 61 66 62 66 70 71 71 72 72 75 60 65 75 79 52 44 70 47 40 54 79 87 87 89 ...

output:


result:


Subtask #5:

score: 0
Skipped

Dependency #4:

0%

Subtask #6:

score: 0
Runtime Error

Test #47:

score: 0
Runtime Error

input:

ZYKrr4gCMcKeyfk6kbZU5k4ZyW3sAGT0
200000
0 1 0 1 4 5 5 7 7 9 9 11 12 13 13 11 14 12 14 19 20 19 20 23 24 25 26 26 28 28 30 31 32 33 33 35 35 30 32 31 37 41 42 43 24 44 46 46 48 49 50 51 51 53 54 55 55 56 56 48 50 54 44 59 49 25 59 67 68 67 68 71 71 72 72 75 76 76 78 79 80 37 80 83 83 85 86 85 79 41 8...

output:


result:


Subtask #7:

score: 0
Skipped

Dependency #1:

0%