This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2674
#include "../../template/template.hpp"
#include "../../structure/segment-tree/segment-tree-fractional-cascading.hpp"
int main() {
int N, Q;
cin >> N;
vector< int64 > X(N);
for(int i = 0; i < N; i++) {
cin >> X[i];
}
SegmentTreeFractionalCascading seg(X);
cin >> Q;
while(Q--) {
int L, R, E;
cin >> L >> R >> E;
--L, --R;
cout << (R - L + 1) - seg.prod(L, R + 1, min(X[L], X[R]) - E, max(X[L], X[R]) + E + 1) << "\n";
}
}
#line 1 "test/verify/aoj-2674-3.test.cpp"
// competitive-verifier: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2674
#line 1 "template/template.hpp"
#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
#endif
using namespace std;
using int64 = long long;
const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < (int)v.size(); i++) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
for (T &in : v) is >> in;
return is;
}
template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {
return a > b && (a = b, true);
}
template <typename T = int64>
vector<T> make_v(size_t a) {
return vector<T>(a);
}
template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
for (auto &e : t) fill_v(e, v);
}
template <typename F>
struct FixPoint : F {
explicit FixPoint(F &&f) : F(std::forward<F>(f)) {}
template <typename... Args>
decltype(auto) operator()(Args &&...args) const {
return F::operator()(*this, std::forward<Args>(args)...);
}
};
template <typename F>
inline decltype(auto) MFP(F &&f) {
return FixPoint<F>{std::forward<F>(f)};
}
#line 4 "test/verify/aoj-2674-3.test.cpp"
#line 1 "structure/segment-tree/segment-tree-fractional-cascading.hpp"
template <typename T>
struct SegmentTreeFractionalCascading {
private:
vector<vector<T> > seg;
vector<vector<int> > LL, RR;
int sz;
int prod(int a, int b, int lower, int upper, int k, int l, int r) const {
if (a >= r or b <= l) {
return 0;
} else if (a <= l and r <= b) {
return upper - lower;
} else {
return prod(a, b, LL[k][lower], LL[k][upper], 2 * k + 1, l,
(l + r) >> 1) +
prod(a, b, RR[k][lower], RR[k][upper], 2 * k + 2, (l + r) >> 1, r);
}
}
public:
SegmentTreeFractionalCascading() = default;
explicit SegmentTreeFractionalCascading(const vector<T> &v) {
sz = 1;
while (sz < v.size()) sz <<= 1;
seg.resize(2 * sz - 1);
LL.resize(2 * sz - 1);
RR.resize(2 * sz - 1);
for (int k = 0; k < v.size(); k++) {
seg[k + sz - 1].emplace_back(v[k]);
}
for (int k = sz - 2; k >= 0; k--) {
seg[k].resize(seg[2 * k + 1].size() + seg[2 * k + 2].size());
LL[k].resize(seg[k].size() + 1);
RR[k].resize(seg[k].size() + 1);
merge(seg[2 * k + 1].begin(), seg[2 * k + 1].end(),
seg[2 * k + 2].begin(), seg[2 * k + 2].end(), seg[k].begin());
int tail1 = 0, tail2 = 0;
for (int i = 0; i < seg[k].size(); i++) {
while (tail1 < seg[2 * k + 1].size() and
seg[2 * k + 1][tail1] < seg[k][i])
++tail1;
while (tail2 < seg[2 * k + 2].size() and
seg[2 * k + 2][tail2] < seg[k][i])
++tail2;
LL[k][i] = tail1, RR[k][i] = tail2;
}
LL[k][seg[k].size()] = (int)seg[2 * k + 1].size();
RR[k][seg[k].size()] = (int)seg[2 * k + 2].size();
}
}
int prod(int a, int b, T l, T r) const {
l = int(lower_bound(seg[0].begin(), seg[0].end(), l) - seg[0].begin());
r = int(lower_bound(seg[0].begin(), seg[0].end(), r) - seg[0].begin());
return prod(a, b, l, r, 0, 0, sz);
}
};
#line 6 "test/verify/aoj-2674-3.test.cpp"
int main() {
int N, Q;
cin >> N;
vector< int64 > X(N);
for(int i = 0; i < N; i++) {
cin >> X[i];
}
SegmentTreeFractionalCascading seg(X);
cin >> Q;
while(Q--) {
int L, R, E;
cin >> L >> R >> E;
--L, --R;
cout << (R - L + 1) - seg.prod(L, R + 1, min(X[L], X[R]) - E, max(X[L], X[R]) + E + 1) << "\n";
}
}
Env | Name | Status | Elapsed | Memory |
---|---|---|---|---|
g++ | 00_sample_00.in | AC | 5 ms | 3 MB |
g++ | 00_sample_01.in | AC | 5 ms | 3 MB |
g++ | 00_sample_02.in | AC | 5 ms | 3 MB |
g++ | 10_randomsmall_00.in | AC | 5 ms | 4 MB |
g++ | 10_randomsmall_01.in | AC | 5 ms | 4 MB |
g++ | 10_randomsmall_02.in | AC | 5 ms | 4 MB |
g++ | 10_randomsmall_03.in | AC | 5 ms | 4 MB |
g++ | 10_randomsmall_04.in | AC | 5 ms | 4 MB |
g++ | 10_randomsmall_05.in | AC | 5 ms | 4 MB |
g++ | 10_randomsmall_06.in | AC | 5 ms | 4 MB |
g++ | 10_randomsmall_07.in | AC | 5 ms | 4 MB |
g++ | 10_randomsmall_08.in | AC | 5 ms | 4 MB |
g++ | 10_randomsmall_09.in | AC | 5 ms | 4 MB |
g++ | 20_random_00.in | AC | 77 ms | 34 MB |
g++ | 20_random_01.in | AC | 82 ms | 32 MB |
g++ | 20_random_02.in | AC | 89 ms | 53 MB |
g++ | 20_random_03.in | AC | 151 ms | 52 MB |
g++ | 20_random_04.in | AC | 62 ms | 32 MB |
g++ | 20_random_05.in | AC | 160 ms | 57 MB |
g++ | 20_random_06.in | AC | 47 ms | 30 MB |
g++ | 20_random_07.in | AC | 142 ms | 50 MB |
g++ | 20_random_08.in | AC | 132 ms | 57 MB |
g++ | 20_random_09.in | AC | 24 ms | 10 MB |
g++ | 80_tirivial_00.in | AC | 5 ms | 3 MB |
g++ | 90_max_00.in | AC | 204 ms | 60 MB |
g++ | 90_max_01.in | AC | 201 ms | 60 MB |
g++ | 90_max_challenge_00.in | AC | 132 ms | 60 MB |
g++ | 90_max_challenge_01.in | AC | 132 ms | 60 MB |
g++ | 90_max_challenge_02.in | AC | 135 ms | 60 MB |
g++ | 90_max_challenge_03.in | AC | 135 ms | 60 MB |
g++ | 90_max_challenge_04.in | AC | 133 ms | 60 MB |
clang++ | 00_sample_00.in | AC | 5 ms | 3 MB |
clang++ | 00_sample_01.in | AC | 5 ms | 3 MB |
clang++ | 00_sample_02.in | AC | 5 ms | 3 MB |
clang++ | 10_randomsmall_00.in | AC | 5 ms | 3 MB |
clang++ | 10_randomsmall_01.in | AC | 5 ms | 4 MB |
clang++ | 10_randomsmall_02.in | AC | 5 ms | 4 MB |
clang++ | 10_randomsmall_03.in | AC | 5 ms | 4 MB |
clang++ | 10_randomsmall_04.in | AC | 5 ms | 4 MB |
clang++ | 10_randomsmall_05.in | AC | 5 ms | 4 MB |
clang++ | 10_randomsmall_06.in | AC | 5 ms | 4 MB |
clang++ | 10_randomsmall_07.in | AC | 5 ms | 4 MB |
clang++ | 10_randomsmall_08.in | AC | 5 ms | 4 MB |
clang++ | 10_randomsmall_09.in | AC | 5 ms | 4 MB |
clang++ | 20_random_00.in | AC | 76 ms | 34 MB |
clang++ | 20_random_01.in | AC | 82 ms | 32 MB |
clang++ | 20_random_02.in | AC | 91 ms | 53 MB |
clang++ | 20_random_03.in | AC | 161 ms | 52 MB |
clang++ | 20_random_04.in | AC | 64 ms | 32 MB |
clang++ | 20_random_05.in | AC | 170 ms | 57 MB |
clang++ | 20_random_06.in | AC | 47 ms | 30 MB |
clang++ | 20_random_07.in | AC | 149 ms | 50 MB |
clang++ | 20_random_08.in | AC | 133 ms | 57 MB |
clang++ | 20_random_09.in | AC | 24 ms | 10 MB |
clang++ | 80_tirivial_00.in | AC | 5 ms | 3 MB |
clang++ | 90_max_00.in | AC | 201 ms | 60 MB |
clang++ | 90_max_01.in | AC | 209 ms | 60 MB |
clang++ | 90_max_challenge_00.in | AC | 135 ms | 60 MB |
clang++ | 90_max_challenge_01.in | AC | 135 ms | 60 MB |
clang++ | 90_max_challenge_02.in | AC | 134 ms | 60 MB |
clang++ | 90_max_challenge_03.in | AC | 135 ms | 60 MB |
clang++ | 90_max_challenge_04.in | AC | 134 ms | 60 MB |