fork download
  1. #include <bits/stdc++.h>
  2. #define ll int
  3. #define el "\n"
  4. #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  5. #define __ROOT__ int main()
  6. #define fi first
  7. #define se second
  8. #define M 1000000007
  9. #define MAXN 1000001
  10. #define GIOIHAN 1000001
  11. #define BLOCK_SIZE 425
  12. #define MAX_NODE 1001001
  13. #define ALPHA_SIZE 26
  14. #define compare(v) sort((v).begin(), (v).end()); (v).erase(unique((v).begin(), (v).end()), (v).end()); // dùng để nén sort mảng compare
  15. using namespace std;
  16. ll n, q ;
  17. ll arr[MAXN], sz[MAXN], par[MAXN];
  18. vector<ll>res;
  19. void init() {
  20. cin>>n ;
  21. for(ll i = 1 ; i < MAXN ; i ++ ) sz[i] = 1, par[i] = i ;
  22. }
  23. ll find_set(ll a ) {
  24. if(par[a] == a) return a;
  25. return par[a] = find_set(par[a]);
  26. }
  27. void union_set(ll x, ll y ) {
  28. x = find_set(x) ;
  29. y = find_set(y);
  30. if(x== y ) return ;
  31. else {
  32. // if(sz[x]< sz[y]) swap(x,y) ;
  33. par[y] = x;
  34. // sz[x] += sz[y];
  35. }
  36. }
  37. void solve() {
  38. for(ll i = 1; i <= n ; i ++ ){
  39. ll x; cin>>x;
  40. x = find_set(x) ;
  41. res.push_back(x);
  42. union_set( ((x+1) % n == 0 ? n : (x+1)%n ) ,x);
  43. }
  44. for(ll i = 0 ; i < res.size( ) ; i ++ ) cout<<res[i]<< " " ;
  45. }
  46.  
  47. __ROOT__ {
  48. fast;
  49. init();
  50. solve();
  51. }
  52.  
Success #stdin #stdout 0.01s 12308KB
stdin
Standard input is empty
stdout
Standard output is empty