This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int N;
int A[300010], B[300010];
ll psum[2][300010], ans[300010];
ll calc(int t, int l, int r) {
return psum[t][r] - (l == 0? 0 : psum[t][l - 1]);
}
struct BIT {
vector<pii> tree;
void init() {
tree = vector<pii>(4*N);
build(0, N - 1, 1);
}
void build(int l, int r, int n) {
if(l == r) {
tree[n] = pii(B[l], l);
return;
}
int m = (l + r)>>1;
build(l, m, 2*n);
build(m + 1, r, 2*n + 1);
tree[n] = min(tree[2*n], tree[2*n + 1]);
}
pii quer(int a, int b, int l, int r, int n) {
if(b < l || r < a) return pii(1e9, 1e9);
if(a <= l && r <= b) return tree[n];
int m = (l + r)>>1;
pii L = quer(a, b, l, m, 2*n);
pii R = quer(a, b, m + 1, r, 2*n + 1);
return min(L, R);
}
} bit;
void solve(int l, int r, ll add) {
if(l > r) return;
pii t = bit.quer(l, r, 0, N - 1, 1);
solve(l, t.second - 1, add + ((t.second - l) % 2? calc((t.second % 2) ^ 1, t.second, r) : calc(t.second % 2, t.second, r)));
solve(t.second + 1, r, add + ((r - t.second) % 2? calc((t.second % 2) ^ 1, l, t.second) : calc(t.second % 2, l, t.second)));
//cout << "solve : " << l << ' ' << r << ' ' << t.second << ' ' << add << endl;
ans[t.second] = add + B[t.second];
while(l < r) {
//cout << l << ' ' << r << ' ' << ans[t.second] << endl;
pii t1 = bit.quer(l, t.second - 1, 0, N - 1, 1);
pii t2 = bit.quer(t.second + 1, r, 0, N - 1, 1);
if(t1 < t2) {
ans[t.second] += (r - t1.second) % 2? calc((t1.second % 2) ^ 1, l, t1.second) : calc(t1.second % 2, l, t1.second);
l = t1.second + 1;
}
else {
ans[t.second] += (t2.second - l) % 2? calc((t2.second % 2) ^ 1, t2.second, r) : calc(t2.second % 2, t2.second, r);
r = t2.second - 1;
}
}
}
int main() {
scanf("%d", &N);
for(int i = 0; i < N; i++) {
scanf("%d", &A[i]);
}
int p = 0;
for(int i = 0; i < N; i++) {
if(A[p] > A[i]) p = i;
}
for(int i = 0; i < N; i++) {
B[i] = A[(i + p) % N];
}
for(int i = 0; i < N; i++) {
for(int j = 0; j < 2; j++) {
psum[j][i] = i % 2 == j? B[i] : 0;
if(i) psum[j][i] += psum[j][i - 1];
}
}
int l = 1, r = N - 1, t = 0;
ans[0] = B[0];
while(l <= r) {
if(B[l] >= B[r]) {
if(t) ans[0] += B[l];
l++;
}
else {
if(t) ans[0] += B[r];
r--;
}
t ^= 1;
}
bit.init();
solve(1, N - 1, N % 2? B[0] : 0);
for(int i = 0; i < N; i++) {
printf("%lld\n", ans[ (i + N - p) % N ]);
}
}
Compilation message (stderr)
cake.cpp: In function 'int main()':
cake.cpp:68:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
^
cake.cpp:71:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &A[i]);
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |