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;
const int N = 200005, inf = 1e9;
int n, a[N];
vector<int> dt[N];
void upd (int I, int J, int V) {
while(dt[I].size() <= J) dt[I].push_back(0);
dt[I][J] = max(dt[I][J], V);
}
int get (int I, int V) {
for(int i=0;i<dt[I].size();i++) {
if(V < dt[I][i]) return i;
}
return inf;
}
void calc (int I) {
if(2*I > n) {
upd(I, 0, inf);
return;
}
calc(2*I);
if(2*I+1 > n) {
upd(I, 0, a[2*I]); upd(I, 1, inf);
return;
}
calc(2*I+1);
int A = min(a[2*I], a[2*I+1]), B = a[2*I]+a[2*I+1]-A, F = 0;
upd(I, 0, A);
if(a[2*I] < a[2*I+1]) {
for(auto &T : dt[2*I]) dt[I].push_back(T);
return;
}
for(int i=0, j=0; i<dt[2*I].size() && j<dt[2*I+1].size(); ) {
int P = dt[2*I][i], Q = dt[2*I+1][j], R = min(P, Q);
if(!F) {
if(B < R) {
F = 1 + (i > j);
upd(I, min(i, j)+1, B);
}
else upd(I, min(i, j)+1, R);
}
if(F) upd(I, (F-1?i:j)+1, R);
P < Q ? i++ : j++;
}
for(int i=1;i<dt[I].size();i++) {
dt[I][i] = max(dt[I][i], dt[I][i-1]);
}
}
void solve (int I) {
if(2*I > n) {
return;
}
if(2*I+1 > n) {
if(a[2*I] < a[I]) swap(a[2*I], a[I]);
return;
}
if(a[I] < a[2*I] && a[I] < a[2*I+1]) {
return;
}
if(a[2*I] < a[I] && a[2*I] < a[2*I+1]) {
swap(a[2*I], a[I]); return;
}
int A = min(a[I], a[2*I]), B = a[I]+a[2*I]-A;
a[I] = a[2*I+1];
if(get(2*I, A) <= get(2*I+1, A)) {
a[2*I] = A; a[2*I+1] = B;
}
else {
a[2*I] = B; a[2*I+1] = A;
}
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%d",&a[i]);
}
calc(1);
for(int i=1;i<=n;i++) {
solve(i);
printf("%d ",a[i]);
}
}
Compilation message (stderr)
swap.cpp: In function 'void upd(int, int, int)':
swap.cpp:9:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(dt[I].size() <= J) dt[I].push_back(0);
^
swap.cpp: In function 'int get(int, int)':
swap.cpp:14:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<dt[I].size();i++) {
^
swap.cpp: In function 'void calc(int)':
swap.cpp:39:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0, j=0; i<dt[2*I].size() && j<dt[2*I+1].size(); ) {
^
swap.cpp:39:41: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0, j=0; i<dt[2*I].size() && j<dt[2*I+1].size(); ) {
^
swap.cpp:52:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=1;i<dt[I].size();i++) {
^
swap.cpp: In function 'int main()':
swap.cpp:84:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
^
swap.cpp:86:20: 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |