QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#276372 | #7616. Jump Graph | AllTheWayNorth# | WA | 2ms | 12060kb | C++14 | 1.7kb | 2023-12-05 20:27:08 | 2023-12-05 20:27:09 |
Judging History
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];
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[cur][1]][0] + sze[son[cur][1]];
f[cur][1] = f[son[cur][1]][1] + f[son[cur][0]][1] + sze[son[cur][0]];
}
ll ans[300010], a[300010];
void dfs2(int cur) {
if (cur == 0) return;
a[son[cur][0]] += sze[son[cur][1]] * 2 + 1 + f[son[cur][1]][0] + a[cur];
a[son[cur][1]] += sze[son[cur][0]] * 2 + 1 + f[son[cur][0]][1] + a[cur];
ans[cur] = a[cur]
+ sze[son[cur][1]] + f[son[cur][1]][0]
+ sze[son[cur][0]] + f[son[cur][0]][1];
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: 0ms
memory: 11816kb
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: 11788kb
input:
2 1 2
output:
1 1
result:
ok single line: '1 1'
Test #3:
score: -100
Wrong Answer
time: 2ms
memory: 12060kb
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:
101 98 99 99 100 87 81 79 79 79 73 72 71 71 70 72 72 77 83 94 107 100 120 120 116 118 116 116 116 113 142 142 143 143 175 175
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: '101 98 99 99 100 87 81 79 79 7...116 113 142 142 143 143 175 175'