#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 ++;}
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll count(const vector<int>& a, const vector<int>& b) {
if (a.size() != b.size()) throw invalid_argument("a and b must have same size");
int n = (int)a.size();
if (n == 0) return 0;
map<pair<int,int>, int> cntPair;
map<int,int> cntA, cntB, diag;
for (int i = 0; i < n; ++i) {
cntPair[{a[i], b[i]}]++;
cntA[a[i]]++;
cntB[b[i]]++;
}
for (auto &it : cntPair) {
int x = it.first.first;
int y = it.first.second;
if (x == y) diag[x] = it.second;
}
map<int, vector<pair<int,int>>> listA;
for (auto &it : cntPair) {
int x = it.first.first;
int y = it.first.second;
int f = it.second;
listA[x].push_back({y, f});
}
auto getCnt = [&](int x, int v)->int {
auto it = cntPair.find({x, v});
if (it == cntPair.end()) return 0;
return it->second;
};
ll S_total = 0;
for (auto &it : cntPair) {
int x = it.first.first;
int y = it.first.second;
int f_xy = it.second;
ll dot = 0;
auto &Lx = listA[x];
auto &Ly = listA[y];
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;
}
}
S_total += (ll)f_xy * dot;
}
ll A = 0;
for (auto &it : cntA) {
int x = it.first;
ll cntax = it.second;
ll d = diag.count(x) ? diag[x] : 0;
A += cntax * d;
}
ll B = 0;
for (auto &it : diag) {
ll d = it.second;
B += d * d;
}
ll C = 0;
for (auto &it : cntB) {
int x = it.first;
ll cntbx = it.second;
ll d = diag.count(x) ? diag[x] : 0;
C += cntbx * d;
}
ll C1 = 0;
for (auto &it : diag) C1 += it.second;
ll non_distinct = A + B + C - 2 * C1;
ll result = S_total - non_distinct;
return result;
}
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:139:1: warning: no return statement in function returning non-void [-Wreturn-type]
139 | }
| ^
# | 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... |