Advertisement
Guest User

Untitled

a guest
May 19th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string>
  4. #include <set>
  5. #include <map>
  6. #include <algorithm>
  7. #include <bitset>
  8. #include <queue>
  9. #include <math.h>
  10. #include <stack>
  11. #include <vector>
  12. #include <string.h>
  13. #include <random>
  14.  
  15. typedef long long ll;
  16.  
  17. const ll MOD = 1e9 + 7, INF = 1e18;
  18.  
  19. using namespace std;
  20.  
  21. ll n, f[200000];
  22.  
  23. set < pair <int, int> > ans;
  24.  
  25. ll p[200000], sz[200000];
  26.  
  27. vector <ll> d[200000];
  28.  
  29. pair <ll, ll> min_edge[200000];
  30.  
  31. ll numbers[7000000];
  32.  
  33. ll find (ll x)
  34. {
  35.     if (p[x] == x) return x;
  36.     else return p[x] = find (p[x]);
  37. }
  38.  
  39. void unite (ll a, ll b)
  40. {
  41.     a = find (a);
  42.     b = find (b);
  43.  
  44.     if (a == b) return;
  45.  
  46.     if (sz[a] < sz[b])
  47.     {
  48.         unite (b, a);
  49.         return;
  50.     }
  51.  
  52.     p[b] = a;
  53.     sz[a] += sz[b];
  54. }
  55.  
  56. struct node
  57. {
  58.     int num;
  59.     int next[2];
  60. };
  61.  
  62. vector <node> a;
  63.  
  64. ll ptr = 0;
  65.  
  66. void add (ll ind)
  67. {
  68.     ll cur = 0, x = f[ind];
  69.  
  70.     for (ll i = 29; i >= 0; i--)
  71.     {
  72.         ll s;
  73.         if (x & (1LL << i)) s = 1;
  74.         else s = 0;
  75.  
  76.         if (a[cur].next[s] == 0)
  77.         {
  78.             a.push_back ({0, 0, 0});
  79.             a[cur].next[s] = ++ptr;
  80.         }
  81.  
  82.         cur = a[cur].next[s];
  83.  
  84.         a[cur].num++;
  85.     }
  86.  
  87.     numbers[cur] = ind;
  88. }
  89.  
  90. void remove (ll ind)
  91. {
  92.  
  93.     ll cur = 0, x = f[ind];
  94.  
  95.     for (ll i = 29; i >= 0; i--)
  96.     {
  97.         ll s;
  98.         if (x & (1LL << i)) s = 1;
  99.         else s = 0;
  100.  
  101.         cur = a[cur].next[s];
  102.  
  103.         a[cur].num--;
  104.     }
  105. }
  106.  
  107. pair <ll, ll> find_xor (ll ind)
  108. {
  109.     ll cur = 0, ans = 0, x = f[ind];
  110.  
  111.     for (ll i = 29; i >= 0; i--)
  112.     {
  113.         ll s;
  114.  
  115.         if (x & (1LL << i)) s = 1;
  116.         else s = 0;
  117.  
  118.         if (!a[cur].next[s] || a[a[cur].next[s]].num == 0)
  119.         {
  120.             cur = a[cur].next[1 - s];
  121.             ans = ans * 2 + 1 - s;
  122.         }
  123.         else
  124.         {
  125.             cur = a[cur].next[s];
  126.             ans = ans * 2 + s;
  127.         }
  128.     }
  129.  
  130.     return make_pair (ans ^ f[ind], numbers[cur]);
  131. }
  132.  
  133. int main ()
  134. {
  135.     cin >> n;
  136.  
  137.     a.push_back ({0, 0, 0});
  138.  
  139.     set <ll> dist;
  140.  
  141.     for (ll i = 0; i < n; i++)
  142.     {
  143.         scanf ("%lld", &f[i]);
  144.  
  145.         dist.insert (f[i]);
  146.     }
  147.  
  148.     n = 0;
  149.  
  150.     for (auto it : dist)
  151.     {
  152.         f[n] = it;
  153.         add (n);
  154.         n++;
  155.     }
  156.  
  157.     for (ll i = 0; i < n; i++)
  158.     {
  159.         p[i] = i;
  160.         sz[i] = 1;
  161.     }
  162.  
  163.     ll sum = 0;
  164.  
  165.     while (sz[find (0)] != n)
  166.     {
  167.         ans.clear ();
  168.         for (ll i = 0; i < n; i++)
  169.             d[i].clear ();
  170.  
  171.  
  172.         for (ll i = 0; i < n; i++)
  173.         {
  174.             d[find (i)].push_back (i);
  175.             min_edge[find (i)] = {INF, INF};
  176.         }
  177.  
  178.         for (ll i = 0; i < n; i++)
  179.         {
  180.             ll l = 0;
  181.             if (d[i].empty ()) continue;
  182.  
  183.             for (ll x : d[i])
  184.                 remove (x);
  185.  
  186.             for (ll x : d[i])
  187.             {
  188.                 auto g = find_xor (x);
  189.                 if (min_edge[i] > g)
  190.                 {
  191.                     min_edge[i] = g;
  192.                     l = x;
  193.                 }
  194.             }
  195.  
  196.             for (ll x : d[i])
  197.                 add (x);
  198.  
  199.             ans.insert ({min (l, min_edge[i].second), max (l, min_edge[i].second)});
  200.         }
  201.  
  202.         for (auto it : ans)
  203.         {
  204.             sum += f[it.first] ^ f[it.second];
  205.             unite (it.first, it.second);
  206.         }
  207.     }
  208.  
  209.     cout << sum;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement