結果
問題 | No.878 Range High-Element Query |
ユーザー | MarcusAureliusAntoninus |
提出日時 | 2019-09-06 23:19:07 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 85 ms / 2,000 ms |
コード長 | 3,648 bytes |
コンパイル時間 | 2,258 ms |
コンパイル使用メモリ | 213,392 KB |
実行使用メモリ | 13,440 KB |
最終ジャッジ日時 | 2024-06-24 21:16:45 |
合計ジャッジ時間 | 3,847 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 3 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 3 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 2 ms
5,376 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | AC | 77 ms
12,452 KB |
testcase_12 | AC | 60 ms
11,412 KB |
testcase_13 | AC | 62 ms
10,112 KB |
testcase_14 | AC | 47 ms
8,836 KB |
testcase_15 | AC | 59 ms
11,264 KB |
testcase_16 | AC | 79 ms
13,160 KB |
testcase_17 | AC | 85 ms
13,440 KB |
testcase_18 | AC | 85 ms
13,372 KB |
ソースコード
#include <bits/stdc++.h> ///////////////////////// // Range Maximum Query // ///////////////////////// class RMaxQ { private: std::vector<int64_t> container_; const int64_t negative_inf_{-1}; void build(const unsigned int array_size) { unsigned int length{1}; while (length < array_size) length <<= 1; container_.resize(2 * length, negative_inf_); } public: RMaxQ(const unsigned int array_size) { build(array_size); } RMaxQ(const std::vector<int64_t> &array) { build(array.size()); std::copy(array.begin(), array.end(), container_.begin() + (container_.size() >> 1)); for (auto i{(container_.size() >> 1) - 1}; i > 0; i--) container_[i] = std::max(container_[2 * i], container_[2 * i + 1]); } // indexは0-indexed void update(const int index, const int64_t assigned) { auto update_place{(container_.size() >> 1) + index}; container_[update_place] = assigned; while (update_place > 1) { update_place >>= 1; container_[update_place] = std::max(container_[2 * update_place], container_[2 * update_place + 1]); } } // left,rightは0-indexed、[left, right)の半開区間 int64_t get(const int left, const int right) const { int64_t max{negative_inf_}; for (int left_i{std::max(0, left) + ((int)container_.size() >> 1)}, right_i{std::min((int)container_.size() >> 1, right) + ((int)container_.size() >> 1)}; left_i < right_i; left_i >>= 1, right_i >>= 1 ) { if (left_i & 1) { max = std::max(max, container_[left_i]); left_i++; } if (right_i & 1) { right_i--; max = std::max(max, container_[right_i]); } } return max; } }; /////////////////////////// // Range Sum Query (BIT) // /////////////////////////// class RSumQBIT { private: std::vector<int64_t> container_; int64_t getHelper(const int index) const { if (index < 0) return 0; if ((int)(container_.size()) <= index) return container_.back(); int64_t sum{}; for (int add_place{index}; add_place > 0; add_place -= add_place & -add_place) sum += container_[add_place]; return sum; } public: RSumQBIT(const int array_size) : container_(array_size + 1) {} // indexは0-indexed void update(const int index, const int64_t added) { for (int update_place{index + 1}; update_place < (int)(container_.size()); update_place += update_place & -update_place) container_[update_place] += added; } // left,rightは0-indexed、[left, right)の半開区間 int64_t get(const int left, const int right) const { return -getHelper(left) + getHelper(right); } }; int main() { int N, Q; scanf("%d%d", &N, &Q); std::vector<int> a(N); for (auto& e: a) scanf("%d", &e); std::vector<int> prev(N, -1); RMaxQ myHome(N); for (int i{}; i < N; i++) { a[i]--; prev[i] = myHome.get(a[i] + 1, N); myHome.update(a[i], i); } // for (auto& e: prev) printf("%d ", e); // putchar('\n'); std::vector<std::array<int64_t, 4>> queries(N + Q); // {1, left, right, q_i} // {2, prev, index, -1} for (int i{}; i < N; i++) queries[i] = {2, prev[i], i, -1}; for (int i{}; i < Q; i++) { int com, left, right; scanf("%d%d%d", &com, &left, &right); left--; queries[N + i] = {1, left, right, i}; } std::sort(queries.begin(), queries.end(), [](auto& a, auto& b) { if (a[1] != b[1]) return a[1] < b[1]; else return a[0] < b[0]; }); // for (auto& e: queries) // { // for (auto& f: e) printf("%d ", f); // putchar('\n'); // } RSumQBIT bit(N); std::vector<int> ans(Q); for (auto& query: queries) { if (query[0] == 1) ans[query[3]] = bit.get(query[1], query[2]); else bit.update(query[2], 1); } for (auto& e: ans) printf("%d\n", e); return 0; }