Submission #654709

# Submission time Handle Problem Language Result Execution time Memory
654709 2022-11-01T10:18:31 Z Valera_Grinenko Election (BOI18_election) C++17
0 / 100
2 ms 340 KB
// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct node {
  bool id = 0;
  //fixed -> no negative pref sum
  //not fixed -> any pref sum
  int ans = 0;
  int sum = 0;
  int mn_suf = 0;
  int mn_pref = 0;
};
node comb(node a, node b) {
  node c;
  if(a.id) return b;
  if(b.id) return a;
  c.ans = max(a.ans + max(0, -(b.mn_pref + a.ans + a.sum)), b.ans + max(0, -(a.mn_suf + b.sum + b.ans)));
  c.sum = a.sum + b.sum;
  c.mn_suf = min(b.mn_suf, a.mn_suf + b.sum);
  c.mn_pref = min(a.mn_pref, b.mn_pref + a.sum);
  return c;
}
node idnode;
const int N = 5e5 + 42;
node st[4 * N];
string s;
void build(int v, int tl, int tr) {
  if(tl == tr) {
    if(s[tl] == 'C') {
      st[v].ans = 0;
      st[v].sum = 1;
      st[v].mn_suf = 0;
      st[v].mn_pref = 0;
    } else {
      st[v].ans = 1;
      st[v].sum = -1;
      st[v].mn_suf = -1;
      st[v].mn_pref = -1;
    }
  } else {
    int tm = (tl + tr) / 2;
    build((v << 1), tl, tm);
    build((v << 1) + 1, tm + 1, tr);
    st[v] = comb(st[(v << 1)], st[(v << 1) + 1]);
  }
}
node get(int v, int tl, int tr, int l, int r) {
  if(l > r) return idnode;
  if(l == tl && r == tr) return st[v];
  int tm = (tl + tr) / 2;
  return comb(get((v << 1), tl, tm, l, min(r, tm)), get((v << 1) + 1, tm + 1, tr, max(l, tm + 1), r));
}
void solve() {
  int n, q;
  cin >> n;
  cin >> s;
  build(1, 0, n - 1);
  cin >> q;
  while(q--) {
    int l, r;
    cin >> l >> r; l--; r--;
    node res = get(1, 0, n - 1, l, r);
    cout << res.ans << '\n';
  }
}
int main() {

  ios::sync_with_stdio(0); cin.tie(0);

  idnode.id = 1;

  int t = 1;

  // cin >> t;

  while(t--) solve();

  return 0;
}
/*

*/
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -