#include "triples.h"
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
#define pb push_back
#define st first
#define nd second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) a.begin(), a.end()
set<pair<pii, int>> S;
pair<pii, int> sor(int a, int b, int c) {
  int x = min({a, b, c});
  int z = max({a, b, c});
  int y = a + b + c - x - z;
  return {{x, y}, z};
}
long long count_triples(std::vector<int> T) {
  int n = T.size();
  ll ans = 0;
  rep(i, n) {
    int j = i + T[i];
    int k[4];
    if (j < n) {
      k[0] = i + T[j];
      k[1] = i - T[j];
      k[2] = j + T[j];
      k[3] = j - T[j];
      rep(c, 4) {
        if (i == j || i == k[c] || j == k[c]) continue;
        if (k[c] < 0 || k[c] >= n) continue;
        int x = T[i];
        int y = T[j];
        int z = T[k[c]];
        int a = abs(i - j);
        int b = abs(i - k[c]);
        int d = abs(j - k[c]);
        if ((x + y + z) != (2 * max({x, y, z}))) continue;
        if (sor(a, b, d) != sor(x, y, z)) continue;
        S.insert(sor(i, j, k[c]));
      }
    }
    j = i - T[i];
    if (j >= 0) {
      k[0] = i + T[j];
      k[1] = i - T[j];
      k[2] = j + T[j];
      k[3] = j - T[j];
      rep(c, 4) {
        if (i == j || i == k[c] || j == k[c]) continue;
        if (k[c] < 0 || k[c] >= n) continue;
        int x = T[i];
        int y = T[j];
        int z = T[k[c]];
        int a = abs(i - j);
        int b = abs(i - k[c]);
        int d = abs(j - k[c]);
        if ((x + y + z) != (2 * max({x, y, z}))) continue;
        if (sor(a, b, d) != sor(x, y, z)) continue;
        S.insert(sor(i, j, k[c]));
      }
    }
  }
  ans = (long long) S.size();
  // for (auto p: S) {
  //   cout << p.st.st << " " << p.st.nd << " " << p.nd << '\n';
  // }
  vector<int> jakie[2 * n + 1];
  vector<int> jakie2[2 * n + 1];
  int iter[n];
  int iter2[n];
  rep(i, n) {
    int x = T[i] - i + n;
    iter[i] = jakie[x].size();
    jakie[x].pb(i);
  }
  for (int i = n - 1; i >= 0; i--) {
    int x = T[i] + i;
    iter2[i] = jakie2[x].size();
    jakie2[x].pb(i);
  }
  rep(i, n) {
    int x = T[i] - i + n;
    int y = T[i] + i;
    int sz = jakie[x].size();
    int sz1 = jakie[y].size();
    if (sz <= sz1) {
      for (int it = iter[i] - 1; it >= 0; it--) {
        int j = jakie[x][it];
        int k = i + T[j];
        if (k < n && ((T[k] + T[j]) == T[i])) {
          if (T[k] == T[j]) continue;
          ans++;
        }
      }
    }
    else {
      for (int it = iter2[i] - 1; it >= 0; it--) {
        int k = jakie2[y][it];
        int j = i - T[k];
        if (j >= 0 && ((T[k] + T[j]) == T[i])) {
          if (T[k] == T[j]) continue;
          ans++;
        }
      }
    }
  }
  return ans;
}
const int MAXN = 1000000;
int jakie[MAXN];
int poms = 0;
vector<int> vec;
bool spr(int x) {
    if (jakie[x + poms] != -1) {
        return false;
    }
    for (auto y: vec) {
        int z = x + poms + y;
        if (jakie[z] != -1) return false;
    }
    return true;
}
void dod(int x) {
    for (auto y: vec) {
        jakie[x + y + poms] = x - y;
    }
}
std::vector<int> construct_range(int M, int K) {
  srand(55851);
  vector<int> pom = {5, 2, 3, 3, 1, 2, 1, 4, 3, 2, 7, 6, 5, 6, 3, 4, 1, 2, 1, 3};
  vector<int> pom2;
  vector<int> pom3;
  vector<int> pom4;
  if (true) {
    pom2 = {41, 8, 29, 38, 5, 46, 13, 34, 43, 10, 51, 38, 39, 48, 5, 16, 85, 44, 13, 46, 21, 28, 29, 18, 25, 26, 61, 22, 23, 20, 31, 72, 33, 28, 15, 16, 5, 64, 65, 66, 21, 10, 23, 44, 71, 4, 5, 58, 17, 76, 1, 10, 53, 12, 49, 6, 5, 8, 7, 28, 41, 42, 43, 2, 23, 5, 35, 36, 37, 18, 35, 30, 31, 32, 13, 70, 11, 26, 27, 114, 115, 20, 21, 22, 19, 20, 25, 5, 57, 2, 25, 102, 103, 52, 7, 8, 97, 98, 47, 94, 95, 92, 1, 42, 1, 40, 87, 88, 7, 6, 5, 164, 81, 82, 11, 10, 29, 76, 77, 160, 5, 72, 71, 72, 21, 20, 153, 66, 67, 64, 65, 172, 143, 160, 161, 10, 143, 138, 165, 140, 53, 54, 137, 150, 135, 5, 147, 148, 45, 44, 45, 152, 143, 122, 149, 124, 137, 138, 117, 186, 119, 142, 189, 132, 115, 114, 207, 136, 127, 24, 109, 122, 131, 122, 215, 176, 203, 126, 117, 114, 115, 208, 121, 96, 119, 166, 187, 82, 157, 190, 85, 160, 87, 152, 1, 108, 155, 148, 147, 96, 95, 150, 171, 100, 99, 174, 145, 166, 143, 94, 169, 162, 161, 194, 165, 164, 129, 156, 127, 154, 159, 124, 157, 122, 79, 120, 125, 124, 123, 178, 143, 38, 259, 146, 173, 40, 43, 42, 135, 134, 139, 138, 137, 50, 51, 246, 195, 142, 55, 54, 241, 190, 127, 188, 153, 236, 151, 132, 259, 114, 231, 230, 117, 254, 177, 160, 225, 122, 249, 76, 221, 220, 79, 78, 243, 21, 215, 2, 213, 238, 93, 12, 13, 90, 233, 10, 93, 8, 16, 228, 117, 226, 97, 148, 1, 5, 195, 194, 193, 4, 107, 106, 215, 188, 21, 51, 45, 38, 183, 208, 207, 206, 33, 38, 42, 36, 201, 124, 173, 26, 25, 52, 29, 28, 21, 77, 47, 24, 74, 50, 26, 186, 57, 40, 39, 81, 43, 42, 35, 85, 20, 38, 149, 148, 72, 5, 43, 75, 3, 95, 57, 56, 98, 49, 9, 162, 161, 85, 86, 14, 56, 89, 11, 59, 60, 14, 94, 74, 73, 97, 19, 31, 69, 69, 70, 121, 3, 73, 106, 28, 111, 8, 78, 108, 5, 106, 30, 22, 116, 33, 17, 51, 99, 98, 90, 141, 45, 94, 119, 43, 44, 36, 98, 47, 100, 112, 111, 34, 52, 7, 107, 55, 56, 11, 160, 26, 9, 25, 73, 3, 4, 70, 20, 21, 17, 5, 24, 64, 72, 13, 14, 86, 59, 17, 83, 56, 14, 13, 22, 135, 51, 25, 26, 1, 22, 72, 4, 3, 69, 42, 34, 9, 8, 64, 12, 13, 12, 55, 42, 9, 44, 39, 55, 21, 20, 109, 25, 17, 44, 67, 20, 29, 28, 31, 30, 25, 8, 27, 34, 38, 56, 31, 22, 17, 16, 19, 18, 3, 86, 47, 46, 5, 8, 43, 10, 79, 116, 7, 76, 75};
  }
  vector<int> ans;
  int sz = pom3.size();
  if (M >= 5000) {
    int tr[M + 100000];
    int k = (floor(sqrt(2 * M)));
    int a = 0;
    vector<int> jakier; 
    rep(i, k) {
        a += (rand() % (i + 1) + 1);
        tr[a - 1] = a;
        jakier.pb(a);
    }
    vector<pii> jakier2;
    for (auto x: jakier) {
        if (x == a) break;
        tr[a - 1 + x] = a - x;
        jakier2.pb({a - x, a - 1 + x});
    }
    for (auto x: jakier) {
        int i = x - 1;
        if (x == a) continue;
        for (auto p: jakier2) {
            int j = p.nd;
            int y = p.st;
            int k = (i + j + x - y)/2;
            int z = (i + x + y - j)/2;
            tr[k] = z;
        }
    }
    // cout  << 1 << '\n';
    // cout << MAXN << '\n';
    jakier.clear();
      jakier2.clear();
      a = 0;
      rep(i, k) {
          a += (rand() % (i + 1) + 1);
          a = min(a, M - 1);
          tr[M - a] = a;
          jakier.pb(a);
      }
      for (auto x: jakier) {
          if (x == a) break;
          if ((a - 1 + x) < M) {
              tr[M - (a - 1 + x)] = a - x;
          }
          jakier2.pb({a - x, a - 1 + x});
      }
      for (auto x: jakier) {
          int i = x - 1;
          if (x == a) continue;
          for (auto p: jakier2) {
              int j = p.nd;
              int y = p.st;
              int k = (i + j + x - y)/2;
              int z = (i + x + y - j)/2;
              if (k < M && k >= 0) {
                  tr[M - k] = z;
              }
          }
      }
    int cnt = 0;
    rep(i, M) {
        if (tr[i] == 0) {
            cnt++;
            tr[i] = 1;
        }
        ans.pb(tr[i]);
        // cout << tr[i] << " ";
    }
    // cout << '\n';
    return ans;
  }
  else if (M == 20) {
    int il = M/20;
    rep(i, il) {
      for (auto x: pom) {
        ans.pb(x);
      }
    }
  }
  else {
    int il = M/500;
    int c = 0;
    rep(i, il) {
      for (auto x: pom2) {
        ans.pb(x);
      }
      // reverse(all(pom2));
    }
  }
  return ans;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |