QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#276393#7616. Jump GraphAllTheWayNorth#WA 3ms14072kbC++142.0kb2023-12-05 20:46:042023-12-05 20:46:04

Judging History

This is the latest submission verdict.

  • [2023-12-05 20:46:04]
  • Judged
  • Verdict: WA
  • Time: 3ms
  • Memory: 14072kb
  • [2023-12-05 20:46:04]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int p[300010];
int rt, son[300010][2];
int ST[300010][20], lg2[300010];
int pmax(int a, int b) {
    return p[a] > p[b] ? a : b;
}
void buildST() {
    lg2[0] = -1;
    for (int i = 1; i <= n; i++) lg2[i] = lg2[i >> 1] + 1;
    for (int i = n; i > 0; i--) {
        ST[i][0] = i;
        for (int j = 1; i + (1 << j) - 1 <= n; j++)
            ST[i][j] = pmax(ST[i][j - 1], ST[i + (1 << j - 1)][j - 1]);
    }
}
int calc(int a, int b) {
    int k = lg2[b - a + 1];
    return pmax(ST[a][k], ST[b - (1 << k) + 1][k]);
}
int build(int l, int r) {
    if (l > r) return 0;
    int pos = calc(l, r);
    son[pos][0] = build(l, pos - 1);
    son[pos][1] = build(pos + 1, r);
    return pos;
}
int sze[300010];
ll f[300010][2], g[300010][2];
void dfs1(int cur) {
    if (cur == 0) return;
    dfs1(son[cur][0]);
    dfs1(son[cur][1]);
    sze[cur] = sze[son[cur][0]] + sze[son[cur][1]] + 1;
    f[cur][0] = f[son[cur][0]][0] + f[son[son[cur][1]][0]][0] + f[son[son[cur][1]][1]][1] + sze[son[cur][1]];
    f[cur][1] = f[son[cur][1]][1] + f[son[son[cur][0]][1]][1] + f[son[son[cur][0]][0]][0] + sze[son[cur][0]];
    g[cur][0] = f[son[cur][1]][1] + g[son[cur][0]][0] + sze[son[cur][0]];
    g[cur][1] = f[son[cur][0]][0] + g[son[cur][1]][1] + sze[son[cur][1]];
}
ll ans[300010], a[300010];
void dfs2(int cur) {
    if (cur == 0) return;
    a[son[cur][0]] += sze[son[cur][1]] * 2 + 1 + g[son[cur][1]][1] + a[cur];
    a[son[cur][1]] += sze[son[cur][0]] * 2 + 1 + g[son[cur][0]][0] + a[cur];
    ans[cur] = a[cur] 
             + sze[son[cur][1]] + g[son[cur][1]][1]
             + sze[son[cur][0]] + g[son[cur][0]][0];
    dfs2(son[cur][0]);
    dfs2(son[cur][1]);
}
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", p + i);
    buildST();
    rt = build(1, n);
    dfs1(rt);
    dfs2(rt);
    for (int i = 1; i <= n; i++) printf("%lld%c", ans[i], " \n"[i == n]);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 13880kb

input:

6
1 6 3 2 5 4

output:

11 7 7 7 6 8

result:

ok single line: '11 7 7 7 6 8'

Test #2:

score: 0
Accepted
time: 2ms
memory: 14072kb

input:

2
1 2

output:

1 1

result:

ok single line: '1 1'

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 11836kb

input:

36
9 29 1 3 14 31 24 21 10 18 22 16 8 7 15 12 17 19 25 28 27 34 11 6 32 4 20 13 2 35 23 26 33 36 30 5

output:

99 96 97 97 98 84 79 77 77 77 71 71 70 70 69 71 71 76 80 91 104 94 114 114 110 112 110 110 110 107 136 136 137 136 168 168

result:

wrong answer 1st lines differ - expected: '92 89 90 90 91 78 73 71 71 71 ...110 107 136 136 137 136 168 168', found: '99 96 97 97 98 84 79 77 77 77 ...110 107 136 136 137 136 168 168'