fork download
  1. /*
  2. * Author: Geeza
  3. */
  4.  
  5. #include <bits/stdc++.h>
  6.  
  7. #define ld long double
  8. #define ll long long
  9. #define pb push_back
  10. #define fin(a, n) for(int i = a; i < n; i++)
  11. #define fjn(a, n) for(int j = a; j < n; j++)
  12. #define all(a) a.begin(),a.end()
  13. #define allr(a) a.rbegin(),a.rend()
  14. #define FAST ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
  15.  
  16. using namespace std;
  17.  
  18. const double PI = acos(-1);
  19. const int N = 100000;
  20. const ll oo = 0x3f3f3f3f3f3f3f3f;
  21. const int MOD = 1000000007, inf = 1e6;
  22.  
  23. string di[] = {"D","L", "U", "R", "UL", "UR", "DL", "DR"};
  24. int dx[] = {+1, +0, +0, -1, -1, -1, +1, +1};
  25. int dy[] = {+0, -1, +1, +0, -1, +1, -1, +1};
  26. char dc[] = {'D', 'L', 'R', 'U'};
  27.  
  28. struct DSU {
  29. vector<ll> parent, size, id;
  30. DSU(ll n) : parent(n + 1), size(n + 1, 1), id(n+1, -1){
  31. iota(parent.begin(), parent.end(), 0);
  32. }
  33.  
  34. ll find(ll x) {
  35. if (x == parent[x]) return x;
  36. return parent[x] = find(parent[x]);
  37. }
  38.  
  39. bool unite(ll x, ll y) {
  40. x = find(x);
  41. y = find(y);
  42. if (x == y) return false;
  43. if (size[x] < size[y]) swap(x, y);
  44. parent[y] = x;
  45. size[x] += size[y];
  46. return true;
  47. }
  48. };
  49.  
  50. void solve() {
  51. ll n, q; cin >> n >> q;
  52. DSU dsu(n);
  53. vector<ll> v(n);
  54. map<ll, set<ll>> mp;
  55. map<ll, ll> idx;
  56. fin(0, n) cin >> v[i], mp[v[i]].insert(i), idx[v[i]] = i;
  57.  
  58. for (auto [x, s] : mp) {
  59. ll l = -1;
  60. for (auto i : s) {
  61. if (l == -1) l = i;
  62. dsu.unite(i, l);
  63. }
  64. dsu.id[dsu.find(l)] = l;
  65. }
  66.  
  67. fin(0, q) {
  68. int t; cin >> t;
  69. if (t == 1) {
  70. ll x, y; cin >> x >> y;
  71. if (idx.find(x) == idx.end()) continue;
  72. ll lx = dsu.find(idx[x]);
  73. if (idx.find(y) == idx.end()) idx[y] = idx[x];
  74. idx.erase(x);
  75. ll ly = dsu.find(idx[y]);
  76. dsu.unite(lx, ly);
  77. }
  78. else {
  79. ll x; cin >> x; --x;
  80. cout << v[dsu.find(x)] << "\n";
  81. }
  82. }
  83. }
  84.  
  85. int main() {
  86. FAST;
  87. #ifndef ONLINE_JUDGE
  88. freopen("input.txt","r",stdin);
  89. freopen("output.txt","w",stdout);
  90. #endif
  91. int tt = 1, c = 1; cin >> tt;
  92. while(tt--){
  93. cout << "Case " << c++ << ":\n";
  94. solve();
  95. }
  96. return 0;
  97. }
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
Case 1: