#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_set>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
long long ans = 0;
void sort3(int& a0, int& a1, int& a2) {
if (a0 > a1) swap(a0, a1);
if (a1 > a2) swap(a1, a2);
if (a0 > a1) swap(a0, a1);
}
void sort4(int& a0, int& a1, int& a2, int& a3) {
if (a0 > a1) swap(a0, a1);
if (a2 > a3) swap(a2, a3);
if (a0 > a2) swap(a0, a2);
if (a1 > a3) swap(a1, a3);
if (a1 > a2) swap(a1, a2);
}
void check(int i, int j, int k, vector<int>& a) {
vector<int> x = { j - i, k - i, k - j };
vector<int> y = { a[i], a[j], a[k] };
sort3(x[0], x[1], x[2]);
sort3(y[0], y[1], y[2]);
#pragma GCC unroll 3
for (int i = 0; i < 3; ++i)
if (x[i] != y[i]) return;
++ans;
}
void findi(int j, int k, vector<int>& a) {
if ((k < 0) || ((k >= a.size()))) return;
if ((j < 0) || ((j >= a.size()))) return;
vector<int> op = { j - a[j],
k - a[k], j - a[k], k - a[j] };
sort4(op[0], op[1], op[2], op[3]);
op.erase(unique(op.begin(), op.end()), op.end());
for (auto i : op)
if ((i >= 0) && (i < a.size()))
if ((a[i] == (k - j)) && (a[j] == (k - i)) && (a[k] == (j - i)))
check(i, j, k, a);
}
void findk(int i, int j, vector<int>& a) {
if ((j < 0) || ((j >= a.size()))) return;
if ((i < 0) || ((i >= a.size()))) return;
vector<int> op = { i + a[i],
j + a[i], i + a[j], j + a[j] };
sort4(op[0], op[1], op[2], op[3]);
op.erase(unique(op.begin(), op.end()), op.end());
for (auto k : op)
if ((k >= 0) && (k < a.size()))
if ((a[i] == (k - j)) && (a[j] == (k - i)) && (a[k] == (j - i)))
check(i, j, k, a);
}
long long count_triples(vector<int> a) {
int n = a.size();
ans = 0;
for (int x = 0; x < n; ++x)
for (auto y : { x - a[x], x + a[x] })
if (!((y < 0) || ((y >= a.size())))) {
vector<int> op = { x - a[x], x + a[x], y + a[y], y - a[y],
y + a[x], y - a[x], x + a[y], x - a[y] };
sort(op.begin(), op.end());
op.erase(unique(op.begin(), op.end()), op.end());
for (auto z : op) {
if ((z < 0) || ((z >= a.size()))) continue;
vector<int> op = { x, y, z };
sort3(op[0], op[1], op[2]);
int b = 0, sb = 0;
for (int i = 0; i < 3; ++i)
for (auto q : { op[i] + a[op[i]], op[i] - a[op[i]] })
if ((q == op[0]) || (q == op[1]) || (q == op[2])) {
b = op[i], sb = q;
break;
}
if ((x == b) && (y == sb)) {
int i = op[0], j = op[1], k = op[2];
if(!((a[i] == (k - j)) && (a[j] == (k - i)) && (a[k] == (j - i))))
check(op[0], op[1], op[2], a);
}
}
}
vector<vector<int>> x(n + n), y(n + n);
for (int i = 0; i < n; ++i)
x[a[i] - i + n].push_back(i);
for (int i = 0; i < n; ++i)
y[a[i] + i].push_back(i);
for (int j = 0; j < n; ++j) {
if (x[a[j] - j + n].size() < y[a[j] + j].size())
for (auto i : x[a[j] - j + n])
findk(i, j, a);
else
for (auto k : y[a[j] + j])
findi(j, k, a);
}
return (int)ans;
}
vector<int> construct_range(int M, int K) {
vector<int> a = { 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 }; return a;
}
//int main() { cout << count_triples({1, 1, 2, 1, 1, 2}); }
# | 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... |