//#include "triples.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
long long count_triples(vector<int> H) {
vector<int> h;
for(auto a:H) h.pb(a);
set<vector<int>> ST;
int n = H.size();
long long cnt = 0;
// pattern 1: j = i + H[i], k = j + H[j]
for(int i = 0; i < n; ++i) {
int j = i + h[i];
if (j < n) {
int k = j + h[j];
if (k < n && i < j && j < k) {
multiset<int> st = {H[i], H[j], H[k]};
if (st.find(j - i) != st.end()) st.erase(st.find(j - i));
if (st.find(k - i) != st.end()) st.erase(st.find(k - i));
if (st.find(k - j) != st.end()) st.erase(st.find(k - j));
if (st.empty()) ST.insert({i, j, k});
}
}
}
// pattern 2: j = i + H[i], k = i + H[j]
for(int i = 0; i < n; ++i) {
int j = i + h[i];
if (j < n) {
int k = i + h[j];
if (k < n && i < j && j < k) {
multiset<int> st = {H[i], H[j], H[k]};
if (st.find(j - i) != st.end()) st.erase(st.find(j - i));
if (st.find(k - i) != st.end()) st.erase(st.find(k - i));
if (st.find(k - j) != st.end()) st.erase(st.find(k - j));
if (st.empty()) ST.insert({i, j, k});
}
}
}
// pattern 3: k = i + H[i], j = k - H[k]
for(int i = 0; i < n; ++i) {
int k = i + h[i];
if (k < n) {
int j = k - h[k];
if (j >= 0 && i < j && j < k) {
multiset<int> st = {H[i], H[j], H[k]};
if (st.find(j - i) != st.end()) st.erase(st.find(j - i));
if (st.find(k - i) != st.end()) st.erase(st.find(k - i));
if (st.find(k - j) != st.end()) st.erase(st.find(k - j));
if (st.empty()) ST.insert({i, j, k});
}
}
}
// pattern 4: k = i + H[i], j = i + H[k]
for(int i = 0; i < n; ++i) {
int k = i + h[i];
if (k < n) {
int j = i + h[k];
if (j < n && i < j && j < k) {
multiset<int> st = {H[i], H[j], H[k]};
if (st.find(j - i) != st.end()) st.erase(st.find(j - i));
if (st.find(k - i) != st.end()) st.erase(st.find(k - i));
if (st.find(k - j) != st.end()) st.erase(st.find(k - j));
if (st.empty()) ST.insert({i, j, k});
}
}
}
// pattern 5: i = j - H[j], k = j + H[i]
for(int j = 0; j < n; ++j) {
int i = j - h[j];
if (i >= 0) {
int k = j + h[i];
if (k < n && i < j && j < k) {
multiset<int> st = {H[i], H[j], H[k]};
if (st.find(j - i) != st.end()) st.erase(st.find(j - i));
if (st.find(k - i) != st.end()) st.erase(st.find(k - i));
if (st.find(k - j) != st.end()) st.erase(st.find(k - j));
if (st.empty()) ST.insert({i, j, k});
}
}
}
// pattern 6: k = j + H[j], i = k - H[k]
for(int j = 0; j < n; ++j) {
int k = j + h[j];
if (k < n) {
int i = k - h[k];
if (i >= 0 && i < j && j < k) {
multiset<int> st = {H[i], H[j], H[k]};
if (st.find(j - i) != st.end()) st.erase(st.find(j - i));
if (st.find(k - i) != st.end()) st.erase(st.find(k - i));
if (st.find(k - j) != st.end()) st.erase(st.find(k - j));
if (st.empty()) ST.insert({i, j, k});
}
}
}
// --- Missing symmetric case: j = i + H[k], k = i + H[j]
// That is equivalent to j + H[j] == k + H[k]. Group indices by s = idx + H[idx]
// and iterate pairs within each group to find (j,k) with same s.
// (This recovers triples like (1,3,4) in the sample.)
vector<vector<int>> groups(2 * n + 5);
for (int idx = 0; idx < n; ++idx) {
int s = idx + h[idx];
groups[s].pb(idx);
}
for (const auto &vec : groups) {
int L = vec.size();
for (int a = 0; a < L; ++a) {
int j = vec[a];
for (int b = a + 1; b < L; ++b) {
int k = vec[b];
int i = k - h[j]; // from k = i + H[j] -> i = k - H[j]
if (i >= 0 && i < j && j < k) {
multiset<int> st = {H[i], H[j], H[k]};
if (st.find(j - i) != st.end()) st.erase(st.find(j - i));
if (st.find(k - i) != st.end()) st.erase(st.find(k - i));
if (st.find(k - j) != st.end()) st.erase(st.find(k - j));
if (st.empty()) ST.insert({i, j, k});
}
}
}
}
cnt = (long long)ST.size();
return cnt;
}
vector<int> construct_range(int M, int K) {
}
Compilation message (stderr)
triples.cpp: In function 'std::vector<int> construct_range(int, int)':
triples.cpp:138:1: warning: no return statement in function returning non-void [-Wreturn-type]
138 | }
| ^| # | 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... |