Submission #195572

# Submission time Handle Problem Language Result Execution time Memory
195572 2020-01-16T09:59:43 Z AlexPop28 Election (BOI18_election) C++11
0 / 100
4 ms 760 KB
#include <bits/stdc++.h>
#define dbg() cerr <<
#define name(x) (#x) << ": " << (x) << ' ' <<

using namespace std;

// aintul interativ e baza
struct SegmTree {
  struct Node {
    int sum, min_suf;
    Node(int val = 0) : sum(val), min_suf(val) {}
  };
  int n;
  vector<Node> tree;
  SegmTree(int n_) : n(n_), tree(2 * n) {}
  Node Join(Node l, Node r) {
    Node ret;
    ret.sum = l.sum + r.sum;
    ret.min_suf = min(l.min_suf + r.sum, r.min_suf);
    return ret;
  }
  void Update(int pos, int val) {
    for (tree[pos += n] = Node(val); pos > 1; pos /= 2) {
      int x = pos, y = pos ^ 1;
      if (x & 1) swap(x, y);
      tree[pos / 2] = Join(tree[x], tree[y]); 
    }
  }
  int Query(int l, int r) {
    ++r;
    Node ans_left, ans_right;
    for (l += n, r += n; l < r; l /= 2, r /= 2) {
      if (l & 1) ans_left  = Join(ans_left,  tree[l++]);
      if (r & 1) ans_right = Join(tree[--r], ans_right);
    }
    return Join(ans_left, ans_right).min_suf;
  }
};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0); 

  int n; cin >> n; cin.get();
  string s; getline(cin, s);
  int q; cin >> q;
  vector<vector<pair<int, int>>> qrs(n);
  for (int i = 0; i < q; ++i) {
    int l, r; cin >> l >> r; --l, --r;
    qrs[l].emplace_back(i, r);
  }
  SegmTree st(n);
  vector<int> ans(q), stk;
  for (int i = n - 1; i >= 0; --i) {
    int x = s[i] == 'C' ? +1 : -1;
    if (x == 1) {
      if (!stk.empty()) {
        st.Update(stk.back(), -1);
        stk.pop_back();
      }
      st.Update(i, x);
    }
    if (x == -1) {
      stk.emplace_back(i);
    }
//    dbg() name(i) "stk: " << endl;
//    for (auto &x : stk) dbg() x << ' '; dbg() endl;
    for (auto &p : qrs[i]) {
      int idx, r; tie(idx, r) = p;
      ans[idx] = (upper_bound(stk.rbegin(), stk.rend(), r) - stk.rbegin());
//      dbg() name(idx + 1) name(r) name(ans[idx]) endl;
      ans[idx] -= st.Query(i, r);
    }
  }
  for (int i = 0; i < q; ++i) {
    cout << ans[i] << '\n';
  }
}
# Verdict Execution time Memory Grader output
1 Runtime error 4 ms 760 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 4 ms 760 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 4 ms 760 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -