結果
| 問題 |
No.878 Range High-Element Query
|
| コンテスト | |
| ユーザー |
ngtkana
|
| 提出日時 | 2019-09-09 02:26:16 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 217 ms / 2,000 ms |
| コード長 | 4,618 bytes |
| コンパイル時間 | 2,128 ms |
| コンパイル使用メモリ | 204,560 KB |
| 最終ジャッジ日時 | 2025-01-07 17:29:45 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 18 |
ソースコード
#include <bits/stdc++.h>
#define loop(n) for (int ngtkana_is_genius = 0; ngtkana_is_genius < int(n); ngtkana_is_genius++)
#define rep(i, begin, end) for(int i = int(begin); i < int(end); i++)
#define all(v) v.begin(), v.end()
#define lint long long
auto cmn = [](auto& a, auto b){if (a > b) {a = b; return true;} return false;};
auto cmx = [](auto& a, auto b){if (a < b) {a = b; return true;} return false;};
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail){
std::cerr << " " << head;
debug_impl(tail...);
}
#define debug(...)\
std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:";\
debug_impl(__VA_ARGS__);\
std::cerr << std::noboolalpha;
template<typename Value, typename BinaryOp>
class segment_tree {
int sz, n, N;
BinaryOp op;
Value id;
std::vector<Value> table;
auto& op_eq(Value& x, Value y) const { return x = op(x, y); }
void merge(int u)
{ table.at(u) = op(table.at(2 * u), table.at(2 * u + 1)); }
Value query_impl(int l, int r, int k, int L, int R) const {
return l <= L && R <= r
? table.at(k)
: R <= l || r <= L
? id
: op(
query_impl(l, r, 2 * k, L, (L + R) / 2),
query_impl(l, r, 2 * k + 1, (L + R) / 2, R)
);
}
public:
segment_tree(int sz, BinaryOp op, Value id):
sz(sz),
n (std::pow(2, int(std::log2(2 * sz - 1)))),
N (n * 2),
op (op),
id (id),
table (N, id)
{}
auto& at(int i) { return table.at(n + i); }
auto& at(int i) const { return table.at(n + i); }
auto collect() const {
auto ret = std::vector<Value>(sz);
for (auto i = 0; i < sz; i++)
{ ret.at(i) = at(i); }
return ret;
}
auto query(int l, int r) const
{ return query_impl(l, r, 1, 0, n); }
void build_oneline(int i)
{ for (i += n, i /= 2; i > 0; i /= 2) merge(i); }
void build()
{ for (auto i = 1; i < n; i++) merge(i); }
void update (int u, Value val) {
at(u) = val;
build_oneline(u);
}
void add (int u, Value val) {
at(u) += val;
build_oneline(u);
}
};
template<typename Value, typename BinaryOp>
auto make_segment_tree(int sz, BinaryOp op, Value id)
{ return segment_tree<Value, BinaryOp>(sz, std::move(op), id); }
template <typename T>
std::istream& operator>> (std::istream& is, std::vector<T>& v) {
for (auto & x : v) is >> x;
return is;
}
template <typename T>
std::ostream& operator<< (std::ostream& os, const std::vector<T>& v) {
auto n = v.size();
os << "{";
for (size_t i = 0; i < n; i++)
{os << (i > 0 ? "," : "") << v.at(i);}
return os << "}";
}
template <typename T>
auto make_vector_impl(size_t sz, T t) {return std::vector<T>(sz, t);}
template <size_t N, typename T, typename U, std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz, U u) {return make_vector_impl(sz, T(u));}
template <size_t N, typename T, std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz) {return std::vector<T>(sz);}
template <size_t N, typename T, typename... Args, std::enable_if_t<N != 1, std::nullptr_t> = nullptr>
auto make_vector(size_t a, Args... args) {return make_vector_impl(a, make_vector<N - 1, T>(args...));}
template <typename T, typename Size_t>
auto& at(T& t, Size_t i) {return t.at(i);}
template <typename T, typename Size_t, typename... Args>
auto& at(T& t, Size_t i, Args... args) {return at(t.at(i), args...);}
int main() {
std::cin.tie(0); std::cin.sync_with_stdio(false);
int n, q; std::cin >> n >> q;
std::vector<int> a(n); std::cin >> a;
for (auto & x : a) x--;
auto min_tree = make_segment_tree(
n,
[](auto x, auto y){ return std::min(x, y); },
n
);
std::vector<int> next(n + 1, n);
for (auto i = n - 1; i >= 0; i--) {
next.at(i) = min_tree.query(a.at(i), n);
min_tree.update(a.at(i), i);
}
auto lg = (int)std::log2(2 * n - 1);
auto table = make_vector<2, int>(lg, n + 1);
table.front() = next;
rep(i, 0, lg - 1) {
auto& crr = table.at(i);
auto& nxt = table.at(i + 1);
rep(j, 0, n + 1)
{ nxt.at(j) = crr.at(crr.at(j)); }
}
loop(q) {
int c, l, r;
std::cin >> c >> l >> r;
l--;
auto crr = l;
auto ret = 0;
for (auto p = lg - 1; p >= 0; p--) {
auto nxt = table.at(p).at(crr);
if (nxt < r) {
crr = nxt;
ret += std::pow(2, p);
}
}
std::cout << ret + 1 << std::endl;
}
return 0;
}
ngtkana