murugappan_s

RRAHSEM - Editorial

Oct 15th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. #define ld long double
  4. #define pb push_back
  5. #define x first
  6. #define y second
  7. #define fastread ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  8. #define PI (atan(1)*4)
  9. #define mp make_pair
  10. using namespace std;
  11.  
  12.  
  13.  
  14.  
  15. const int maxn = 2e5 + 1;
  16.  
  17. int N = maxn;
  18. int component[maxn], weight[maxn];
  19.  
  20. inline int root(int x) {
  21.     while (component[x] != x) {
  22.         component[x] = component[component[x]];
  23.         x = component[x];
  24.     }
  25.     return x;
  26. }
  27.  
  28. inline bool same(int x, int y) {
  29.     return root(x) == root(y);
  30. }
  31.  
  32. inline void merge(int x, int y) {
  33.     if (same(x, y))
  34.         return;
  35.     x = root(x);
  36.     y = root(y);
  37.     if (weight[x] < weight[y])
  38.         swap(x, y);
  39.     weight[x] += weight[y];
  40.     component[y] = x;
  41. }
  42.  
  43.  
  44. //end of DSU
  45.  
  46. //mapper
  47. int ptr;
  48.  
  49. int freq[maxn], grouped[maxn], n, groups, pos1, pos2;
  50. ll val1, val2, shift[60];
  51.  
  52.  
  53. struct trie {
  54.     trie* nxt[10];
  55.     int val;
  56.     trie() {
  57.         for (register int i = 0; i < 10; i++)
  58.             nxt[i] = NULL;
  59.         val = 0;
  60.     }
  61. };
  62.  
  63. trie* rt;
  64.  
  65.  
  66. inline int getval(register ll num) {
  67.     trie* node = rt;
  68.     int nxt;
  69.     while (num) {
  70.         nxt = num % 10;
  71.         num /= 10;
  72.         if (node->nxt[nxt] == NULL)
  73.             return 0;
  74.         node = node->nxt[nxt];
  75.         if (num == 0)
  76.             return node->val;
  77.     }
  78.     return 0;
  79. }
  80.  
  81. inline void putval(register ll num, int val) {
  82.     trie* node = rt;
  83.     int nxt;
  84.     while (num) {
  85.         nxt = num % 10;
  86.         num /= 10;
  87.         if (node->nxt[nxt] == NULL)
  88.             node->nxt[nxt] = new trie();
  89.         node = node->nxt[nxt];
  90.     }
  91.     node->val = val;
  92. }
  93.  
  94. int main()
  95. {
  96.     fastread;
  97.     shift[0] = 1;
  98.     for (int i = 1; i < 60; i++)
  99.         shift[i] = 2 * shift[i - 1];
  100.     rt = new trie();
  101.     cin >> n;
  102.     for (int i = 1; i <= n; i++) {
  103.         component[i] = i;
  104.         weight[i] = 1;
  105.     }
  106.     for (int i = 0; i < n; i++) {
  107.         cin >> val1;
  108.         pos1 = getval(val1);
  109.         if (pos1 == 0) {
  110.             ptr++;
  111.             pos1 = ptr;
  112.             putval(val1, ptr);
  113.             freq[pos1]++;
  114.             groups++;
  115.  
  116.         }
  117.         else if (grouped[pos1]) {
  118.             cout << groups << '\n';
  119.             continue;
  120.         }
  121.         else {
  122.             freq[pos1]++;
  123.             groups++;
  124.             cout << groups << '\n';
  125.             continue;
  126.         }
  127.         for (int j = 0; j < 60; j++) {
  128.             val2 = val1 ^ shift[j];
  129.             pos2 = getval(val2);
  130.             if (pos2 == 0 || same(pos1, pos2))
  131.                 continue;
  132.             groups -= freq[pos2];
  133.             grouped[pos2] = 1;
  134.             grouped[pos1] = 1;
  135.             freq[pos2] = 1;
  136.             freq[pos1] = 1;
  137.             merge(pos1, pos2);
  138.         }
  139.         cout << groups << '\n';
  140.     }
  141.     return 0;
  142. }
Add Comment
Please, Sign In to add comment