이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <stdio.h>
#include <string.h>
#define N 400000
long long max(long long a, long long b) { return a > b ? a : b; }
long long ss[N + 1];
int xx[N], yy[N], dd[N]; long long dp[N]; int pp[N], qq[N], hh[N], tt[N];
int n, m;
int cross(int i, int j) {
int lower = -1, upper = m - yy[j] + 1;
while (upper - lower > 1) {
int d = (lower + upper) / 2;
if (dp[i] - ss[yy[i] + d] > dp[j] - ss[yy[j] + d])
upper = d;
else
lower = d;
}
return lower;
}
void merge(int i, int j) {
int u, v, d;
u = tt[i], v = hh[j];
while (1) {
d = cross(u, v);
if (u != hh[i] && cross(pp[u], u) <= d)
u = pp[u];
else if (v != tt[j] && cross(v, qq[v]) >= d)
v = qq[v];
else {
qq[u] = v, pp[v] = u;
break;
}
}
hh[j] = hh[i];
}
long long query(int i, int d) {
while (hh[i] != tt[i] && cross(hh[i], qq[hh[i]]) >= d)
hh[i] = qq[hh[i]];
return dp[hh[i]] - ss[yy[hh[i]] + d];
}
int main() {
static int aa[N], ii[N], ww[N], qu[N];
int cnt, i, i_, j, a, x, y, d;
long long z;
scanf("%d", &n);
memset(ii, -1, n * sizeof *ii);
for (i = 0; i < n; i++) {
scanf("%d", &aa[i]), aa[i]--;
if (aa[i] >= 0)
ii[aa[i]] = i;
}
for (a = 0; a < n; a++)
scanf("%d", &ww[a]);
m = 0;
for (a = 0; a < n; a++)
if ((i = ii[a]) != -1)
xx[i] = m;
else
ss[m + 1] = ss[m] + ww[a], m++;
if (m == n) {
for (i = 0; i < n; i++)
printf("%lld ", ss[n] - ss[n - 1 - i]);
printf("\n");
return 0;
}
for (i = 0; i < n; i++)
pp[i] = qq[i] = -1, hh[i] = tt[i] = i;
cnt = 0, y = 0, a = -1;
for (j = 0; j < n; j++) {
if (aa[j] == -2)
y++;
else if (a > aa[j])
aa[j] = -1;
else {
a = aa[j];
yy[j] = y, dd[j] = xx[j] - yy[j];
if (dd[j] < 0)
aa[j] = -1;
}
if (aa[j] >= 0) {
i_ = -1;
while (cnt && dd[i = qu[cnt - 1]] < dd[j]) {
if (i_ == -1)
i_ = i;
else
merge(i, i_);
cnt--;
}
dp[j] = cnt ? dp[i] - ss[xx[i]] : -ss[dd[j]];
if (i_ != -1)
dp[j] = max(dp[j], query(i_, dd[j])), merge(i_, j);
dp[j] += ss[xx[j]] + ww[aa[j]];
qu[cnt++] = j;
}
x = m, d = x - y;
z = 0;
for (i = j; i >= 0; i--) {
if (aa[i] < 0)
continue;
if (dd[i] >= d)
break;
z = max(z, dp[i] + ss[x] - ss[yy[i] + d]);
}
if (i >= 0)
z = max(z, dp[i] + ss[x] - ss[xx[i]]);
else
z = max(z, ss[x] - ss[d]);
printf("%lld ", z);
}
printf("\n");
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.c: In function 'main':
Main.c:55:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
Main.c:58:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%d", &aa[i]), aa[i]--;
| ^~~~~~~~~~~~~~~~~~~
Main.c:63:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | scanf("%d", &ww[a]);
| ^~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |