QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#594364#9238. TreeA_programmer22 168ms57336kbC++172.1kb2024-09-27 22:17:532024-09-27 22:17:53

Judging History

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

  • [2024-09-27 22:17:53]
  • 评测
  • 测评结果:22
  • 用时:168ms
  • 内存:57336kb
  • [2024-09-27 22:17:53]
  • 提交

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[0][i] = rk[f[0][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) 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;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 52ms
memory: 56596kb

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:

11XNDQnkdGXK8y3iaqfMvWKu4vqrBbz1
OK
682657448594
834060746759
104109477239
626358648483
578238337686
1248296016204
1306149385356
417846862163
365719715670
1302057740438

result:

wrong answer 3rd lines differ - on the 1st token, expected: '682654248246', found: '682657448594'

Subtask #2:

score: 0
Wrong Answer

Test #11:

score: 0
Wrong Answer
time: 0ms
memory: 41824kb

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:

11XNDQnkdGXK8y3iaqfMvWKu4vqrBbz1
OK
176151937931
634675550982
816008871871
39751811139
1038225127370
611011794939
164594820760
32459329436
128508154716
268413983118

result:

wrong answer 3rd lines differ - on the 1st token, expected: '175909322571', found: '176151937931'

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Wrong Answer

Test #33:

score: 0
Wrong Answer
time: 56ms
memory: 57336kb

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:

11XNDQnkdGXK8y3iaqfMvWKu4vqrBbz1
OK
18330265136
114566763474
5124006106
1571791376
1390674293
102887815673
12142375614
532137685
48879288671
74805047676
7047480647
58553433098
26812357642
41270292974
32111738164
8116219600
57785445925
106724578087
93323175302
42830278197
28126914429
28123568692
1525...

result:

wrong answer 3rd lines differ - on the 1st token, expected: '18330254280', found: '18330265136'

Subtask #5:

score: 0
Skipped

Dependency #4:

0%

Subtask #6:

score: 22
Accepted

Test #47:

score: 22
Accepted
time: 54ms
memory: 56720kb

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:

11XNDQnkdGXK8y3iaqfMvWKu4vqrBbz1
OK
44399242169
44212387526
45790536474
45755320631
44179830668
44674975190
45108846673
44925057561
45397405182
44060687307
44371967364
44521941379
44063691285
45721392614
44864217118
45409864033
44277589684
45749035882
45188069715
46494572380
45158343139
45090918080
...

result:

ok 

Test #48:

score: 22
Accepted
time: 82ms
memory: 53408kb

input:

ZYKrr4gCMcKeyfk6kbZU5k4ZyW3sAGT0
200000
0 1 1 0 1 5 5 5 8 9 9 11 11 11 14 14 16 17 18 19 20 19 20 20 20 19 20 27 28 28 27 27 16 29 34 34 17 1 9 5 34 41 18 1 1 16 41 47 47 47 50 51 52 50 51 29 20 52 58 58 58 58 58 59 64 64 64 64 66 69 69 69 71 69 69 71 69 73 78 78 78 81 82 83 82 83 86 87 88 86 88 86 ...

output:

11XNDQnkdGXK8y3iaqfMvWKu4vqrBbz1
OK
64951344210
64943726054
64943726054
64943916158
65047917130
64944092623
64943726054
64947401833
64961512711
64948863135
64946023604
64943726054
64943726054
64943726054
64943726054
64943726054
64944034362
64943726054
65004474595
64949289055
64943726054
64943726054
...

result:

ok 

Test #49:

score: 22
Accepted
time: 111ms
memory: 51716kb

input:

ZYKrr4gCMcKeyfk6kbZU5k4ZyW3sAGT0
200000
0 1 2 3 4 5 6 7 8 9 9 11 12 13 14 15 15 16 17 19 20 20 20 21 24 25 26 27 28 29 30 31 31 33 31 35 36 37 38 38 40 41 31 41 44 45 45 47 46 49 21 51 52 46 49 55 55 55 58 58 58 61 56 57 64 64 66 67 67 69 70 71 72 32 66 75 76 77 34 62 80 81 82 73 84 85 84 84 86 89 9...

output:

11XNDQnkdGXK8y3iaqfMvWKu4vqrBbz1
OK
37087048609
37087048609
37087048609
37491568870
37400056171
37087048609
37087048609
37087048609
37289321554
37087048609
37087048609
37241580717
37605719455
37087048609
37087048609
37092772513
37087048609
37087048609
37087048609
37087048609
37087132935
37087048609
...

result:

ok 

Test #50:

score: 22
Accepted
time: 131ms
memory: 51932kb

input:

ZYKrr4gCMcKeyfk6kbZU5k4ZyW3sAGT0
200000
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 22 25 26 27 28 29 29 28 29 33 31 35 36 37 23 37 40 41 41 43 41 44 46 47 47 42 50 42 52 53 24 30 48 51 58 59 60 61 60 63 64 65 66 67 68 68 68 71 64 63 74 74 39 77 64 79 80 81 81 81 83 85 85 87 88 89 ...

output:

11XNDQnkdGXK8y3iaqfMvWKu4vqrBbz1
OK
37264466688
37264466688
38185270274
37888938601
37264466688
37264466688
37264466688
37417874076
37482305371
37264466688
37264466688
37264466688
37264466688
37365055305
37264466688
37264466688
37264466688
37267534605
37264466688
37264466688
37264466688
37264466688
...

result:

ok 

Test #51:

score: 22
Accepted
time: 123ms
memory: 51976kb

input:

ZYKrr4gCMcKeyfk6kbZU5k4ZyW3sAGT0
200000
0 1 2 3 4 5 6 7 6 8 10 11 11 12 14 14 16 17 17 19 20 21 22 23 24 25 25 24 28 29 30 31 31 32 34 33 35 37 38 39 40 38 42 42 44 43 46 47 47 49 50 47 50 51 48 55 56 56 55 57 60 55 62 63 64 65 66 65 68 69 65 71 72 73 72 64 70 62 78 75 48 63 77 83 83 51 86 87 72 89 ...

output:

11XNDQnkdGXK8y3iaqfMvWKu4vqrBbz1
OK
37589188777
37589188777
38253325129
37589188777
37861175794
37589188777
37589188777
37589188777
37589188777
37589188777
37589188777
37589188777
37589188777
37589188777
37589188777
37589188777
38391993713
37589188777
37589188777
37589188777
37589188777
37589188777
...

result:

ok 

Test #52:

score: 22
Accepted
time: 118ms
memory: 51976kb

input:

ZYKrr4gCMcKeyfk6kbZU5k4ZyW3sAGT0
200000
0 1 2 3 4 5 6 7 8 9 10 10 12 12 11 15 15 17 18 19 20 21 22 22 23 25 26 25 12 29 30 31 30 33 33 32 36 31 38 37 40 40 42 43 44 44 46 46 44 48 49 51 51 53 54 55 56 42 58 59 59 60 58 60 64 65 66 49 65 69 56 71 72 73 73 75 72 77 78 78 75 67 65 83 83 85 86 87 83 83 ...

output:

11XNDQnkdGXK8y3iaqfMvWKu4vqrBbz1
OK
37859676645
37859676645
37859676645
38429101021
37962495021
38541502518
38053301531
38254039021
37859676645
37859676645
37859676645
38694173578
38576890284
37865028081
38589950876
37859676645
38045510633
38401370341
38325868141
37859676645
38632650734
37865145926
...

result:

ok 

Test #53:

score: 22
Accepted
time: 146ms
memory: 51728kb

input:

ZYKrr4gCMcKeyfk6kbZU5k4ZyW3sAGT0
200000
0 1 2 3 4 5 4 7 8 9 10 7 8 13 14 10 16 17 11 19 19 21 22 23 23 23 18 27 28 19 30 31 25 33 34 34 36 16 38 39 39 41 42 39 44 45 39 41 48 49 50 50 51 50 54 55 55 55 56 59 56 61 54 63 51 65 66 67 68 52 70 71 71 71 74 75 71 77 78 72 78 73 82 82 84 80 86 79 75 89 89...

output:

11XNDQnkdGXK8y3iaqfMvWKu4vqrBbz1
OK
40141707652
39209169927
38732784018
38784538671
38734930632
38732784018
39216208881
39454752487
40996902804
38732784018
38732784018
38732784018
38732784018
38732784018
39188782412
39314089162
38732784018
38732784018
39344232470
39696184584
38732784018
38732849247
...

result:

ok 

Test #54:

score: 22
Accepted
time: 168ms
memory: 53944kb

input:

ZYKrr4gCMcKeyfk6kbZU5k4ZyW3sAGT0
200000
0 1 1 3 2 5 3 7 8 0 10 4 12 11 14 15 16 7 18 19 20 21 21 23 24 23 22 27 18 29 26 19 32 30 26 35 36 37 37 39 40 41 42 43 41 45 46 47 47 48 50 51 45 53 44 40 52 57 58 59 60 61 62 36 64 65 66 35 68 49 70 71 72 73 74 72 76 77 78 71 73 74 82 83 84 83 86 75 88 89 39...

output:

11XNDQnkdGXK8y3iaqfMvWKu4vqrBbz1
OK
323701273
323701273
323701273
323701273
323701273
323701273
323701273
323701273
323701273
323701273
323701273
323701273
323701273
323701273
323701273
323701273
323701273
323701273
323701273
323701273
323701273
323701273
323701273
323701273
323701273
323701273
3237...

result:

ok 

Test #55:

score: 22
Accepted
time: 133ms
memory: 51132kb

input:

ZYKrr4gCMcKeyfk6kbZU5k4ZyW3sAGT0
200000
0 1 0 3 3 5 6 7 8 9 10 11 12 13 14 15 15 17 18 19 7 21 9 23 23 25 8 27 27 29 30 31 32 33 34 29 36 37 33 39 38 41 42 32 44 45 46 47 48 47 28 51 52 53 53 55 55 57 58 57 58 61 62 59 52 65 66 66 67 68 70 71 60 73 74 69 76 76 78 77 40 81 82 82 56 56 86 87 88 87 90 ...

output:

11XNDQnkdGXK8y3iaqfMvWKu4vqrBbz1
OK
50126121130
50126121130
51363268964
50126121130
50126121130
54997659828
50126121130
50126121130
50126121130
55056343369
50126121130
50126121130
50126121130
50126121130
50126121130
50126121130
50126121130
50126121130
50126121130
50126121130
54573881978
52388316484
...

result:

ok 

Test #56:

score: 22
Accepted
time: 115ms
memory: 50800kb

input:

ZYKrr4gCMcKeyfk6kbZU5k4ZyW3sAGT0
200000
0 1 0 3 3 5 6 7 8 9 10 11 12 13 14 15 15 17 18 19 7 21 9 23 23 25 8 27 27 29 30 31 32 33 34 29 36 37 33 39 38 41 42 32 44 45 46 47 48 47 28 51 52 53 53 55 55 57 58 57 58 61 62 59 52 65 66 66 67 68 70 71 60 73 74 69 76 76 78 77 40 81 82 82 56 56 86 87 88 87 90 ...

output:

11XNDQnkdGXK8y3iaqfMvWKu4vqrBbz1
OK
50126121130
50126121130
51363268964
50126121130
50126121130
54997659828
50126121130
50126121130
50126121130
55056343369
50126121130
50126121130
50126121130
50126121130
50126121130
50126121130
50126121130
50126121130
50126121130
50126121130
54573881978
52388316484
...

result:

ok 

Subtask #7:

score: 0
Skipped

Dependency #1:

0%