Advertisement
ivnikkk

Untitled

Feb 14th, 2022
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1.  
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include "bits/stdc++.h"
  4. //#include "geometry.h"
  5. //#include "data_structure.h"
  6. using namespace std;
  7. using namespace chrono;
  8. #define all(a) a.begin(), a.end()
  9. #define allr(a) a.rbegin(), a.rend()
  10. #define sqrt(x) sqrtl(x)
  11. mt19937 rnd(std::chrono::high_resolution_clock::now().time_since_epoch().count());
  12. typedef long long ll;
  13. typedef double ld;
  14.  
  15. struct dsu {
  16.     vector<ll> p, siz;
  17.     dsu(ll n) {
  18.         p.resize(n), siz.resize(n);
  19.         for (ll i = 0; i < n; i++) {
  20.             p[i] = i;
  21.             siz[i] = 1;
  22.         }
  23.     }
  24.     ll find(ll v) {
  25.         if (v == p[v]) {
  26.             return v;
  27.         }
  28.         return p[v] = find(p[v]);
  29.     }
  30.     void union_set(ll a, ll b) {
  31.         a = find(a);
  32.         b = find(b);
  33.         if (a == b) {
  34.             return;
  35.         }
  36.         else {
  37.             if (siz[a] < siz[b]) {
  38.                 swap(a, b);
  39.             }
  40.             p[b] = a;
  41.             siz[a] += siz[b];
  42.         }
  43.     }
  44. };
  45. vector<pair<ll,ll>> merge(dsu &U,vector<pair<ll,ll>> a, vector<pair<ll,ll>> b) {
  46.     vector<pair<ll,ll>> res;
  47.     ll i = 0;
  48.     ll j = 0;
  49.     while (i < a.size() && j < b.size()) {
  50.         if (a[i].first <= b[j].first) {
  51.             res.push_back(a[i++]);
  52.         }
  53.         else {
  54.             for (ll k = i; k < a.size(); k++) {
  55.                 U.union_set(a[k].second, b[j].second);
  56.             }
  57.             res.push_back(b[j++]);
  58.         }
  59.     }
  60.     while (i < a.size())
  61.         res.push_back(a[i++]);
  62.     while (j < b.size())
  63.         res.push_back(b[j++]);
  64.     return res;
  65. }
  66. vector<pair<ll,ll>> msort(dsu &U,vector<pair<ll,ll>> a) {
  67.     if (a.size() <= 1)
  68.         return a;
  69.     ll k = a.size() / 2;
  70.     return merge(U,
  71.         msort(U,vector<pair<ll,ll>>(a.begin(), a.begin() + k)),
  72.         msort(U,vector<pair<ll,ll>>(a.begin() + k, a.end())));
  73. }
  74. signed main() {
  75. #ifdef _DEBUG
  76.     freopen("input.txt", "r", stdin);
  77.     freopen("output.txt", "w", stdout);
  78. #endif
  79.     srand(time(NULL));
  80.     ios_base::sync_with_stdio(false);
  81.     cin.tie(nullptr);
  82.     cout.tie(nullptr);
  83.     ll t;
  84.     cin >> t;
  85.     while (t--) {
  86.         ll n;
  87.         cin >> n;
  88.         vector<pair<ll,ll>> a(n);
  89.         dsu U(n);
  90.         for (ll i = 0; i < n; i++) {
  91.             cin >> a[i].first;
  92.             a[i].second = i;
  93.         }
  94.         msort(U, a);
  95.         set<ll> f;
  96.         for (ll i = 0; i < n; i++) {
  97.             f.insert(U.find(i));
  98.         }
  99.         cout << f.size() << endl;
  100.     }
  101.    
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement