#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define vi vector<ll>
#define vvi vector<vi>
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define ld long double
#define pii pair<ll, ll>
#define mt make_tuple
#define mn(a, b) a = min(a, b)
#define mx(a, b) a = max(a, b)
using namespace std;
const ll INF = (ll)2e9;
const ll inf = (ll)2e18;
const ld eps = (ld)1e-8;
const ll mod = (ll)998244353;
const ll MAXN = (ll)1e5 + 1;
const ll MAXC = (ll)1e6 + 1;
const ll MAXE = (ll)1000;
const ll MAXLOG = 21;
const ll maxlen = (ll)1e5;
const ll asci = (ll)256;
const ll block = 480;
const ld PI = acos(-1);
const ld e = 2.7182818284;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<
pii,
null_type,
less<pii>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;*/
template <class T>
istream& operator >>(istream &in, vector<T> &arr){
for (T &cnt : arr) {
in >> cnt;
}
return in;
};
struct skoba{
int sum = INF, p = INF, s = INF;
skoba(int sum, int p, int s): sum(sum), p(p), s(s) {}
skoba() {}
};
skoba merge(skoba a, skoba b) {
return skoba(min({a.sum, b.sum, a.p + b.s}), min(a.p, b.p), min(a.s, b.s));
}
struct stree{
vector<skoba> t;
stree(int n) {t.resize(4 * n);}
void build(int v, int tl, int tr, vi &pref, vi &suf) {
if (tl + 1 == tr) return void(t[v] = skoba(pref[tl] + suf[tl], min(pref[tl], pref[tl + 1]), min(suf[tl], suf[tl + 1])));
int tm = (tl + tr) / 2;
build(2 * v, tl, tm, pref, suf);
build(2 * v + 1, tm, tr, pref, suf);
t[v] = merge(t[2 * v], t[2 * v + 1]);
}
skoba ask(int v, int tl, int tr, int l, int r) {
if (tl >= r || l >= tr) return skoba();
if (l <= tl && tr <= r) return t[v];
int tm = (tl + tr) / 2;
return merge(ask(2 * v, tl, tm, l, r), ask(2 * v + 1, tm, tr, l, r));
}
};
void solve() {
int n; cin >> n;
string s; cin >> s;
vi a(n);
for (int i = 0; i < n; ++i) a[i] = s[i] == 'C'? 1: -1;
vi pref(n + 1), suf(n + 1);
for (int i = 0; i < n; ++i) {
pref[i + 1] = pref[i] + a[i];
}
for (int i = n - 1; i > -1; --i) {
suf[i] = suf[i + 1] + a[i];
}
stree t(n);
t.build(1, 0, n, pref, suf);
int q; cin >> q;
while (q--) {
int l, r; cin >> l >> r;
l--, r--;
cout << pref[l] + suf[r + 1] - t.ask(1, 0, n, l, r).sum << "\n";
}
}
int main() {
srand(time(0));
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#endif
cout.precision(30);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
512 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
512 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
512 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |