QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#226374 | #7327. Median on Binary Tree | PPP# | TL | 0ms | 13796kb | C++17 | 2.4kb | 2023-10-25 21:40:02 | 2023-10-25 21:40:02 |
Judging History
answer
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long ll;
typedef long double ld;
int n;
const int maxN = 2e5 + 10;
int w[maxN];
vector<int> dp[maxN];
vector<int> sub[maxN];
int ans[maxN];
void solve() {
for (int i = 1; i <= n; i++) {
cin >> w[i];
}
for (int i = n; i >= 1; i--) {
sub[i].emplace_back(w[i]);
for (int j : {2 * i, 2 * i + 1}) {
if (j > n) continue;
for (int p : sub[j]) {
if (p == n + 1) continue;
sub[i].emplace_back(p);
}
}
sort(sub[i].begin(), sub[i].end());
sub[i].emplace_back(n + 1);
dp[i].resize(sub[i].size());
int pt_l = 0;
int pt_r = 0;
const int INF = -1e9;
int Q = 0;
// cout << i << " HI " << endl;
for (int p : sub[i]) {
int S = -INF;
if (w[i] >= p) S = 1;
else S = -1;
if (n >= 2 * i) {
while (sub[2 * i][pt_l] < p) pt_l++;
assert(pt_l < sub[2 * i].size());
S += dp[2 * i][pt_l];
}
// cout << " CHECK " << n << " " << i << endl;
if (n >= 2 * i + 1) {
// cout << pt_r << " " << sub[2 * i + 1].back() << " ??? " << " " << dp[2 * i + 1].size() << endl;
while (sub[2 * i + 1][pt_r] < p) {
pt_r++;
}
// cout << pt_r << " HI " << endl;
assert(pt_r < sub[2 * i + 1].size());
S += dp[2 * i + 1][pt_r];
// cout << " GG " << endl;
}
// cout << " CHECK3 " << n << " " << i << endl;
// cout << S << " " << p << " ??? " << endl;
if (S >= 1) {
ans[S - 1] = max(ans[S - 1], p);
}
S = max(S, 0);
dp[i][Q] = S;
Q++;
}
}
for (int i = n - 2; i >= 0; i--) ans[i] = max(ans[i], ans[i + 1]);
ans[0] = n;
for (int i = 0; i < n; i++) cout << ans[i] << " ";
cout << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
while (cin >> n) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 13796kb
input:
5 1 2 3 4 5 10 9 10 4 2 3 5 7 1 8 6
output:
5 2 2 1 1 10 9 5 4 4 3 3 2 2 1
result:
ok 15 numbers
Test #2:
score: -100
Time Limit Exceeded
input:
10 4 10 7 9 5 3 1 2 6 8 10 7 10 2 4 9 8 3 1 5 6 10 1 5 8 3 6 7 2 4 10 9 10 7 9 3 6 2 1 10 5 4 8 10 7 1 9 2 4 5 3 6 8 10 10 2 7 5 9 10 3 1 4 8 6 10 9 8 1 2 6 10 5 4 3 7 10 3 6 1 9 10 8 2 7 4 5 10 7 3 8 10 4 1 5 2 6 9 10 6 10 7 5 2 9 1 4 3 8 10 6 8 5 10 9 4 2 1 3 7 10 4 3 5 9 1 2 8 7 10 6 10 7 5 10 8 ...