#include "triples.h"
#include <bits/stdc++.h>
using namespace std;
vector<int> h;
vector<bool> vis;
bool check_range(int i, int j, int k) {
// checks if the range is correct and H[i] is the lowest
if(vis[i] || vis[j] || vis[k]) return false;
if(i < 0LL || j < 0LL || k < 0LL || k < j) return false;
if(i >= h.size() || j >= h.size() || k >= h.size()) return false;
vector<long long> diff = {abs(i - j), abs(i - k), abs(j - k)};
vector<long long> diff2 = {h[i], h[j], h[k]};
sort(diff.begin(), diff.end());
sort(diff2.begin(), diff2.end());
return diff == diff2;
}
bool check_range2(int i, int j, int k) {
if(vis[i] || vis[j] || vis[k]) return false;
if(i < 0LL || j < 0LL || k < 0LL) return false;
if(i >= h.size() || j >= h.size() || k >= h.size()) return false;
vector<long long> diff = {abs(i - j), abs(i - k), abs(j - k)};
vector<long long> diff2 = {h[i], h[j], h[k]};
sort(diff.begin(), diff.end());
sort(diff2.begin(), diff2.end());
return diff == diff2;
}
long long count_triples(std::vector<int> H) {
h = H;
long long ans = 0;
int n = h.size();
vector<pair<int, int> > h_sizes;
vis.resize(n);
int maxx = 0;
for(int i = 0; i < n; i++) {
h_sizes.push_back({h[i], i});
vis[i] = false;
maxx = max(maxx, h[i]);
}
sort(h_sizes.begin(), h_sizes.end());
if(n <= 2000) {
for(int i = 0; i < h_sizes.size(); i++) {
int idx = h_sizes[i].second;
for(int j = 0; j < n; j++) {
int this_diff = abs(idx - j);
int actual_diff = (h_sizes[i].first == this_diff) ? h[j] : h_sizes[i].first;
ans += check_range(idx, j, j - actual_diff);
// if(check_range(idx, j, j - actual_diff)) cout << "1: " << idx << " " << j << " " << j - actual_diff << "\n";
ans += check_range(idx, j, j + actual_diff);
// if(check_range(idx, j, j + actual_diff)) cout << "2: " << idx << " " << j << " " << j + actual_diff << "\n";
ans += check_range(idx, j, idx - actual_diff);
// if(check_range(idx, j, idx - actual_diff)) cout << "3: " << idx << " " << j << " " << i - actual_diff << "\n";
ans += check_range(idx, j, idx + actual_diff);
// if(check_range(idx, j, idx + actual_diff)) cout << "4: " << idx << " " << j << " " << i + actual_diff << "\n";
}
vis[idx] = true;
}
return ans;
}
if(maxx <= 10) {
for(int i = 0; i < n; i++) {
for(int j = i + 1; j <= i + 10; j++) {
for(int k = j + 1; k <= j + 10; k++) {
ans += check_range2(i, j, k);
}
}
}
return ans;
}
reverse(h.begin(), h.end());
for(int i = 0; i < n; i++) {
int k = i + h[i];
ans += (check_range2(i, k - h[k], k) && (k - h[k] > i));
ans += (check_range2(i, i + h[k], k) && (i + h[k] < k) && (i + h[k] != k - h[k]));
// cout << "i = " << i << ", k = " << k << ", ans = " << ans << "\n";
}
return ans;
}
std::vector<int> construct_range(int M, int K) {
std::vector<int> ok;
for(int i = 0; i < M; i++) {
if(i % 3 == 0) ok.push_back(2);
else ok.push_back(1);
}
return ok;
}
# | 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... |