Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- #include <vector>
- using namespace std;
- class QueryStack {
- public:
- QueryStack() {}
- bool empty() { return vals.empty(); }
- void push(int x) {
- vals.push(x);
- int next_max = (maxs.empty()) ? x : max(maxs.top(), x);
- maxs.push(next_max);
- }
- void pop() {
- vals.pop();
- maxs.pop();
- }
- int top() {
- return vals.top();
- }
- int stack_max() {
- if (!empty())
- return maxs.top();
- else
- return -(1 << 30);
- }
- private:
- stack<int> vals, maxs;
- };
- class QueryQueue {
- public:
- QueryQueue() { }
- bool empty() { return head.empty() && tail.empty(); }
- void push(int x) { tail.push(x); }
- void pop() {
- if (!head.empty()) head.pop();
- else {
- while (!tail.empty()) {
- int x = tail.top();
- tail.pop();
- head.push(x);
- }
- }
- }
- int queue_max() {
- return max(head.stack_max(), tail.stack_max());
- }
- private:
- QueryStack head, tail;
- };
- void solve(vector<int>& seq) {
- int left = 0, right = 1;
- QueryQueue queue;
- queue.push(seq[left]);
- int n_queries; cin >> n_queries;
- for (int i = 0; i < n_queries; ++i) {
- char type; cin >> type;
- if (type == 'R') {
- queue.push(seq[right++]);
- }
- else {
- queue.pop();
- ++left;
- }
- cout << queue.queue_max() << " ";
- }
- }
- int main() {
- int sz; cin >> sz;
- vector<int> seq(sz);
- for (auto& el : seq) cin >> el;
- solve(seq);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement