#ifndef rtgsp
#include "triples.h"
#endif // rtgsp
#include<bits/stdc++.h>
#define ll long long
#define task ""
using namespace std;
const int maxn = 2e5 + 2;
int n, h[maxn], res, row[maxn], col[maxn];
vector<int> cr, cc, r[maxn], c[maxn];
ll count_triples(vector<int> H)
{
    res = 0; cr.clear(); cc.clear();
    n = (int)H.size();
    for (int i = 0; i < n; i++)
    {
        h[i] = H[i];
        r[i].clear();
        c[i].clear();
        cr.push_back(h[i] - i);
        cc.push_back(h[i] + i);
    }
    sort(cr.begin(), cr.end());
    cr.resize(unique(cr.begin(), cr.end()) - cr.begin());
    sort(cc.begin(), cc.end());
    cc.resize(unique(cc.begin(), cc.end()) - cc.begin());
    for (int i = 0; i < n; i++)
    {
        int x = lower_bound(cr.begin(), cr.end(), h[i] - i) - cr.begin();
        r[x].push_back(i);
        row[i] = x;
    }
    for (int i = n - 1; i >= 0; i--)
    {
        int x = lower_bound(cc.begin(), cc.end(), h[i] + i) - cc.begin();
        c[x].push_back(i);
        col[i] = x;
    }
    for (int i = 0; i < n - 2; i++)
    {
        int k = h[i] + i;
        if (k < n)
        {
            int j = k - h[k];
            if (j > i && h[j] == j - i) res++;
            if (h[k] + i != k - h[k])
            {
                j = h[k] + i;
                if (j < k && h[j] == k - j) res++;
            }
        }
        int j = h[i] + i;
        if (j < n - 1)
        {
            int k = h[j] + i;
            if (k > j && k < n && h[k] == k - j) res++;
        }
    }
    for (int k = 2; k < n; k++)
    {
        int i = k - h[k];
        if (i >= 0)
        {
            int j = h[i] + i;
            if (j < k && h[j] == k - j) res++;
            if (k - h[i] != h[i] + i)
            {
                j = k - h[i];
                if (j > i && h[j] == j - i) res++;
            }
        }
    }
    for (int j = 1; j < n - 1; j++)
    {
        if (r[row[j]].size() < c[col[j]].size())
        {
            for (int i : r[row[j]])
            {
                if (i == j) break;
                int k = h[i] + j;
                if (k < n && h[j] == k - i && h[k] == j - i && h[k] != k - j) res++;
            }
        }
        else
        {
            for (int k : c[col[j]])
            {
                if (k == j) break;
                int i = j - h[k];
                if (i >= 0 && h[i] == k - j && h[i] != j - i && h[j] == k - i) res++;
            }
        }
    }
    return res;
}
vector<int> construct_range(int M, int K)
{
    return {};
}
#ifdef rtgsp
int main()
{
    freopen(task".in", "r", stdin);
    //freopen(task".OUT", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    string s;
    int N;
    vector<int> H;
    H.clear();
    cin >> s >> N >> N;
    for (int i = 0; i < N; i++)
    {
        int x;
        cin >> x;
        H.push_back(x);
    }
    cout << count_triples(H);
}
#endif // rtgsp
| # | 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... |