#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
ll res = 0;
void add(ll i, ll j, ll k) {res ++;}
inline ll packKey(int x, int y) {
return ( (ll)(uint32_t)x << 32 ) | (uint32_t)y;
}
ll count(vector<int> a, vector<int> b) {
int n = (int)a.size();
if (n == 0) return 0;
unordered_map<ll, int> cntPair;
cntPair.reserve(n * 2);
for (int i = 0; i < n; ++i) {
cntPair[ packKey(a[i], b[i]) ] += 1;
}
unordered_map<int, vector<pair<int,int>>> listA;
listA.reserve(cntPair.size());
for (auto &it : cntPair) {
ll key = it.first;
int freq = it.second;
int x = (int)(key >> 32);
int y = (int)(key & 0xffffffff);
listA[x].push_back({y, freq});
}
auto getCnt = [&](int x, int v)->int {
ll key = packKey(x, v);
auto it = cntPair.find(key);
if (it == cntPair.end()) return 0;
return it->second;
};
ll total = 0;
for (auto &it : cntPair) {
ll key = it.first;
int f_xy = it.second;
int x = (int)(key >> 32);
int y = (int)(key & 0xffffffff);
auto itLy = listA.find(y);
if (itLy == listA.end()) continue;
auto &Lx = listA[x];
auto &Ly = itLy->second;
ll dot = 0;
if (Lx.size() <= Ly.size()) {
for (auto &p : Lx) {
int v = p.first;
int cxv = p.second;
int cyv = getCnt(y, v);
if (cyv) dot += (ll)cxv * cyv;
}
} else {
for (auto &p : Ly) {
int v = p.first;
int cyv = p.second;
int cxv = getCnt(x, v);
if (cxv) dot += (ll)cxv * cyv;
}
}
total += (ll)f_xy * dot;
}
return total;
}
long long count_triples(std::vector<int> H) {
// #TH: a[k] max
for (int k = 2; k < H.size(); k ++) {
int i = k - H[k]; if (i < 0 || H[i] >= H[k]) continue; int j1 = k - H[i], j2 = i + H[i];
if (j1 < 0 || H[j1] >= H[k] || H[j1] != j1 - i) j1 = -1;
if (j2 >= k || H[j2] >= H[k] || H[j2] != k - j2) j2 = -1;
if (j1 != -1) add(i, j1, k); if (j2 != -1 && j1 != j2) add(i, j2, k);
}
// #TH: a[i] max
for (int i = 0; i + 2 < H.size(); i ++) {
int k = i + H[i]; if (k >= H.size() || H[k] >= H[i]) continue; int j1 = k - H[k], j2 = i + H[k];
if (j1 < 0 || H[j1] >= H[i] || H[j1] != j1 - i) j1 = -1;
if (j2 >= k || H[j2] >= H[i] || H[j2] != k - j2) j2 = -1;
if (j1 != -1) add(i, j1, k); if (j2 != -1 && j1 != j2) add(i, j2, k);
}
// #TH: a[j] max, j - i = a[i]
for (int i = 0; i + 2 < H.size(); i ++) {
int j = i + H[i]; if (j >= H.size() || H[j] <= H[i]) continue; int k = i + H[j];
if (k >= H.size() || H[k] >= H[j] || k - H[k] != j || k - j == H[i]) k = -1;
if (k != -1) add(i, j, k);
}
// #TH: a[j] max, j - i = a[k]
vector<int> a, b;
for (int i = 0; i < H.size(); i ++) a.push_back(H[i] + i);
for (int i = 0; i < H.size(); i ++) b.push_back(H[i] - i);
res += count(a, b);
return res;
}
std::vector<int> construct_range(int M, int K) {
// vector<int> res; res.push_back(1);
// for (int i = 1; i < M; i ++) res.push_back(i);
// return res;
}
Compilation message (stderr)
triples.cpp: In function 'std::vector<int> construct_range(int, int)':
triples.cpp:111:1: warning: no return statement in function returning non-void [-Wreturn-type]
111 | }
| ^
# | 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... |