#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
using ull=unsigned long long;
using ll=long long;
using pii=pair<int,int>;
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
const int mod=1e9+7;
#define OVL(x,s) for(auto y:x) cout<<y<<s; cout<<"\n";
template <typename T> istream& operator>>(istream& is, vector<T> &a) {
copy_n(istream_iterator<T>(is), a.size(), a.begin()); return is;}
#ifdef IOI
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
#else
#define dbg(...) 1337;
#endif
#define pb push_back
#define F first
#define S second
#define all(v) v.begin(),v.end()
const int maxn = 2e5+100;
map<int,set<int>> mp;
int arr[maxn];
int ans[maxn];
int par[maxn];
int c[maxn];
int find(int node){
return node==par[node]?node:par[node]=find(par[node]);
}
void merge(int u,int v){
u = find(u);
v = find(v);
if(u==v) return;
par[u] = v;
}
signed main(){
for(int i = 0;i<maxn;i++){
par[i] = i;
}
ios_base::sync_with_stdio(0);cin.tie(0);
int n;
cin>>n;
int nxt[n+1];
memset(nxt,0,sizeof(nxt));
for(int i=1;i<=n;i++){
cin>>arr[i];
nxt[i] = i;
}
for(int i=1;i<=n;i++){
if(mp[arr[i]].empty()){
mp[arr[i]].insert(i);
continue;
}
dbg(arr[i],mp[arr[i]])
if(mp[arr[i]].empty()){
dbg("here")
break;
}
int j = *mp[arr[i]].rbegin();
int k = j;
while(k<=i){
int kk = find(k);
dbg(kk)
if(i==kk) break;
if(mp[arr[kk]].find(kk)!=mp[arr[kk]].end()){
dbg("here",kk,arr[kk],mp[arr[kk]])
mp[arr[kk]].erase(kk);
dbg("here2",kk,arr[kk],mp[arr[kk]])
}
merge(kk,i);
dbg(k,kk)
k = kk+1;
}
mp[arr[i]].insert(i);
}
for(int i=1;i<=n;i++){
cout<<arr[find(i)]<<endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |