#include <bits/stdc++.h>
/*
Brute to win
*/
using namespace std;
using ll = long long;
#define int long long
#define pii pair<ll, ll>
#define fi first
#define se second
const ll N = 2e5 + 5, inf = 1e18, mod = 1e9 + 7, block = 320, lim = 19;
int n, a[N];
vector <int> comp;
vector <int> pre[N];
int Left[N];
struct ST {
vector <int> st, lz;
ST (int _n) {
st.assign(4 * (_n + 1), 0);
lz.assign(4 * (_n + 1), 0);
}
void lazy(int i, int l, int r) {
if (lz[i] != 0) {
if (l < r) {
st[2 * i] = st[2 * i + 1] = lz[i];
lz[2 * i] = lz[2 * i + 1] = lz[i];
}
lz[i] = 0;
}
}
void update(int i, int l, int r, int u, int v, int val) {
if (u > r || v < l) return;
if (u <= l && r <= v) {
st[i] = val;
lz[i] = val;
lazy(i, l, r);
return;
}
int mid = (l + r) / 2;
lazy(i, l, r);
update(2 * i, l, mid, u, v, val);
update(2 * i + 1, mid + 1, r, u, v, val);
st[i] = max(st[2 * i], st[2 * i + 1]);
}
int get(int i, int l, int r, int u, int v) {
lazy(i, l, r);
if (u > r || v < l) return 0;
if (u <= l && r <= v) return st[i];
int mid = (l + r) / 2;
return max(get(2 * i, l, mid, u, v), get(2 * i + 1, mid + 1, r, u, v));
}
} st(N);
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
if (fopen(".inp", "r")) {
freopen(".inp", "r", stdin);
freopen(".out", "w", stdout);
}
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) comp.push_back(a[i]);
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
for (int i = 1; i <= n; i++) {
int x = lower_bound(comp.begin(), comp.end(), a[i]) - comp.begin();
pre[x].push_back(i);
}
for (int i = 1; i <= n; i++) {
int x = lower_bound(comp.begin(), comp.end(), a[i]) - comp.begin();
auto it = lower_bound(pre[x].begin(), pre[x].end(), i) - pre[x].begin();
if (it > 0) {
// cout << i << ' ' << pre[x][it - 1] << '\n';
--it;
if (st.get(1, 1, n, pre[x][it], pre[x][it]) == 0) {
Left[i] = pre[x][it];
st.update(1, 1, n, pre[x][it] + 1, i - 1, 1);
}
else {
Left[i] = Left[pre[x][it]];
if (st.get(1, 1, n, Left[i], Left[i]) == 1) Left[i] = 0;
if (Left[i] > 0) st.update(1, 1, n, pre[x][Left[i]] + 1, i - 1, 1);
}
}
}
// for (int i = 1; i <= n; i++) cout << Left[i] << " ";
// cout << '\n';
ST st1(n);
for (int i = 1; i <= n; i++) {
if (Left[i] > 0) st1.update(1, 1, n, Left[i], i, a[i]);
// cout << a[i] << ' ' << i << ' ' << Left[i] << '\n';
}
for (int i = 1; i <= n; i++) {
int x = st1.get(1, 1, n, i, i);
cout << (x == 0 ? a[i] : x) << '\n';
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | freopen(".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:63:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen(".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |