#include <bits/stdc++.h>
using namespace std;
using ll = long long;
//sub4:
vector<int> construct_range(int M, int K) {
vector<int> ans1 = {
4, 3, 1, 2, 1,
4, 3, 2, 7, 6,
5, 8, 11, 10, 9,
1, 7, 2, 3, 4,
};
if(M <= 20){
while(ans1.size() > M || ans1.back() >= M) ans1.pop_back();
return ans1;
}
vector<int> S = {0};
vector<bool> in_span(2*M, false);
int span_cnt = 0;
vector<pair<int,int>> pts;
while(span_cnt < M-1) {
int best_t = -1, best_gain = -1;
vector<bool> inS(2*M, false);
for(int u:S) inS[u] = true;
for(int t = 0; t <= 2*(M-1); t += 2) if(!inS[t]) {
int gain = 0;
for(int u:S) {
if(u==t) continue;
int s = u + t;
if(s >= 2 && s <= 2*(M-1) && (s%2==0) && !in_span[s]) ++gain;
}
if(gain > best_gain) {
best_gain = gain;
best_t = t;
}
}
if(best_t < 0) break;
for(int u:S) {
if(u == best_t) continue;
int s = u + best_t;
if(s >= 2 && s <= 2*(M-1) && (s%2==0) && !in_span[s]) {
int x = min(u, best_t), y = max(u, best_t);
pts.emplace_back(x, y);
in_span[s] = true;
++span_cnt;
}
}
S.push_back(best_t);
}
vector<int> H(M, 1);
for(auto [x,y] : pts) {
int idx = (x + y) / 2;
int h = (y - x) / 2;
if(idx >= 0 && idx < M) {
H[idx] = h;
}
}
return H;
}
/*long long count_triples(vector<int> H)
{
int n = H.size();
ll ans = 0;
for (int k = 0; k < n; k++)
{
int c = H[k];
int i = k - c;
if (i < 0 || i >= n) continue;
int a = H[i];
if (a > c - a) continue;
int need = c - a;
int j1 = i + a;
int j2 = k - a;
if (i < j1 && j1 < k && H[j1] == need) ans++;
if (j2 != j1 && i < j2 && j2 < k && H[j2] == need) ans++;
}
return ans;
}*/
/*void nen(vector<int>& H)
{
vector<int> temp = H;
sort(temp)
}*/
long long count_thdb(vector<signed> H)
{
int n = H.size();
vector<int> u(n), v(n);
for(int i = 0; i < n; i++)
{
u[i] = i - H[i];
v[i] = i + H[i];
}
}
long long count_triples(vector<int> H) {
int n = H.size();
vector<vector<int>> list_by_A(n + 1);
vector<vector<int>> list_by_B(n + 1);
for (int j = 0; j < n; j++) {
int a = j - H[j];
if (a >= 0 && a < n) {
list_by_A[a].push_back(j);
}
int b = j + H[j];
if (b < n) {
list_by_B[b].push_back(j);
}
}
long long count1 = 0;
for (int i = 0; i < n; i++) {
int k0 = i + H[i];
if (k0 >= n) continue;
for (int j : list_by_A[i]) {
if (i < j && j < k0) {
if (H[k0] == k0 - j) {
count1++;
}
}
}
for (int j : list_by_B[k0]) {
if (i < j && j < k0) {
if (H[k0] == j - i) {
count1++;
}
}
}
}
long long count21 = 0;
for (int j = 0; j < n; j++) {
for (int i : list_by_B[j]) {
int k0 = i + H[j];
if (k0 < n && k0 > j) {
if (k0 - H[k0] == j) {
count21++;
}
}
}
}
long long count22 = 0;
unordered_map<int, vector<int>> groups;
for (int i = 0; i < n; i++) {
int x = H[i] - i;
groups[x].push_back(i);
}
for (auto &p : groups) {
auto &list_i = p.second;
sort(list_i.begin(), list_i.end());
int n_list = list_i.size();
for (int i_idx = 0; i_idx < n_list; i_idx++) {
for (int j_idx = i_idx + 1; j_idx < n_list; j_idx++) {
int i1 = list_i[i_idx];
int i2 = list_i[j_idx];
int d = i2 - i1;
int k = i1 + H[i2];
if (k < 0 || k >= n) continue;
if (H[k] == d) {
count22++;
}
}
}
}
long long count3 = 0;
for (int j = 0; j < n; j++) {
int k0 = j + H[j];
if (k0 >= n) continue;
for (int i : list_by_B[j]) {
if (H[k0] == H[i] + H[j]) {
count3++;
}
}
}
for (int j = 0; j < n; j++) {
int i0 = j - H[j];
if (i0 < 0 || i0 >= j) continue;
int k0 = j + H[i0];
if (k0 < n && k0 > j) {
if (H[k0] == H[i0] + H[j]) {
count3++;
}
}
}
return count1 + count21 + count22 + count3;
}
Compilation message (stderr)
triples.cpp: In function 'long long int count_thdb(std::vector<int>)':
triples.cpp:97:1: warning: no return statement in function returning non-void [-Wreturn-type]
97 | }
| ^
# | 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... |