Advertisement
Guest User

Solution to G. Glad You Came

a guest
Mar 21st, 2022
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using uint = unsigned int;
  5. using ull = unsigned long long;
  6.  
  7. int const lgmax = 20;
  8. int const nmax = 100000;
  9. int rmq[1 + lgmax][1 + nmax];
  10.  
  11. uint x, y, z;
  12.  
  13. uint rng() {
  14.   x = x ^ (x << 11);
  15.   x = x ^ (x >> 4);
  16.   x = x ^ (x << 5);
  17.   x = x ^ (x >> 14);
  18.   uint w = x ^ y ^ z;
  19.   x = y;
  20.   y = z;
  21.   z = w;
  22.   return z;
  23. }
  24.  
  25. int lg(uint number) {
  26.   return 31 - __builtin_clz(number);
  27. }
  28.  
  29. ull solve() {
  30.   int n, m;
  31.   std::cin >> n >> m >> x >> y >> z;
  32.   for(int h = 0; h < lgmax; h++)
  33.     for(int i = 1; i <= n; i++)
  34.       rmq[h][i] = 0;
  35.  
  36.   for(int i = 1;i <= m; i++) {
  37.     uint l = rng() % n + 1;
  38.     uint r = rng() % n + 1;
  39.     uint val = (rng() & ((1 << 30) - 1));
  40.     if(r < l)
  41.       std::swap(l, r);
  42.     int h = lg(r - l + 1);
  43.  
  44.     if(rmq[h][l] < val)
  45.       rmq[h][l] = val;
  46.     if(rmq[h][r - (1 << h) + 1] < val)
  47.       rmq[h][r - (1 << h) + 1] = val;
  48.   }
  49.   for(int h = lgmax - 1; 1 <= h; h--)
  50.     for(int i = 1; i <= n - (1 << h) + 1; i++) {
  51.       if(rmq[h - 1][i] < rmq[h][i])
  52.         rmq[h - 1][i] = rmq[h][i];
  53.       if(rmq[h - 1][i + (1 << (h - 1))] < rmq[h][i])
  54.         rmq[h - 1][i + (1 << (h - 1))] = rmq[h][i];
  55.     }
  56.   ull result = 0;
  57.  
  58.   for(int i = 1; i <= n; i++)
  59.     result ^= (1LL * i * rmq[0][i]);
  60.   return result;
  61. }
  62.  
  63. int main() {
  64.   int t;
  65.   std::cin >> t;
  66.   for(int testcase = 1; testcase <= t; testcase++) {
  67.     std::cout << solve() << '\n';
  68.   }
  69.   return 0;
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement