Advertisement
MathQ_

Untitled

Mar 9th, 2021
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. const int k = 316;
  2.  
  3. struct sqrtd {
  4.     int n;
  5.  
  6.     vector<int> a;
  7.     vector<int> next;
  8.     vector<int> cnt;
  9.  
  10.     sqrtd(vector<int> b) {
  11.         n = b.size();
  12.         a = b;
  13.         next.resize(n, 0);
  14.         cnt.resize(n, 0);
  15.         for (int i = 0; i < n; ++i) {
  16.             if (cnt[i]) continue;
  17.             int j = i;
  18.             while (j / k < i / k + 1 && j < n) {
  19.                 cnt[i]++; j += a[j];
  20.             }
  21.             next[i] = j;
  22.             j = i + a[i]; int c = 1;
  23.             while (j / k < i / k + 1 && j < n) {
  24.                 cnt[j] = cnt[i] - c;
  25.                 next[j] = next[i];
  26.                 j += a[j];
  27.                 ++c;
  28.             }
  29.         }
  30.     }
  31.  
  32.     void update(int j, int m) {
  33.         int block = j / k;
  34.         a[j] = m;
  35.         for (int i = block * k; i <= j; ++i) {
  36.             next[i] = 0;
  37.             cnt[i] = 0;
  38.         }
  39.  
  40.         for (int i = block * k; i <= j; ++i) {
  41.             if (cnt[i]) continue;
  42.             int j = i;
  43.             while (j / k < i / k + 1 && j < n) {
  44.                 cnt[i]++; j += a[j];
  45.             }
  46.             next[i] = j;
  47.             j = i + a[i]; int c = 1;
  48.             while (j / k < i / k + 1 && j < n) {
  49.                 cnt[j] = cnt[i] - c;
  50.                 next[j] = next[i];
  51.                 j += a[j];
  52.                 ++c;
  53.             }
  54.         }
  55.     }
  56.  
  57.     pair<int, int> get(int j) {
  58.         int ans = 0;
  59.         while (next[j] < a.size()) {
  60.             ans += cnt[j];
  61.             j = next[j];
  62.         }
  63.         while (j + a[j] < n) {
  64.             j += a[j];
  65.             ++ans;
  66.         }
  67.         return {j + 1, ans + 1};
  68.     }
  69. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement