#include <bits/stdc++.h>
#define ll long long
#define rep(i, m, n) for (int i = (m); i <= (n); i++)
#define REB(i, m, n) for (int i = (m); i >= (n); i--)
#define iter(v, id) for (auto v : id)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define pb push_back
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
using namespace std;
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
const int N = 5e5 + 7;
const int Mod = 1e9 + 7;
const ll INF = 1e18 + 7;
const int BASE = 137;
int n, Q;
int a[N];
struct Segment_Tree {
int m;
struct Node {
int pre, suf, S, A;
};
Node st[N << 2];
Node mer (Node lc, Node rc) {
Node cur;
cur.pre = max(lc.pre, lc.S + rc.pre);
cur.suf = max(rc.suf, rc.S + lc.suf);
cur.S = lc.S + rc.S;
cur.A = max({lc.A + rc.S, rc.A + lc.S, lc.pre + rc.suf});
return cur;
}
void build (int id, int l, int r) {
if (l == r) {
st[id].pre = st[id].suf = st[id].A = max(0, a[l]);
st[id].S = a[l];
return;
}
int mid = l + r >> 1;
build (id << 1, l, mid);
build (id << 1 | 1, mid + 1, r);
st[id] = mer (st[id << 1], st[id << 1 | 1]);
}
Node get (int id, int l, int r, int u, int v) {
if (l > v || r < u) return {0, 0, 0, 0};
if (l >= u && r <= v) return st[id];
int mid = l + r >> 1;
return mer (get (id << 1, l, mid, u, v), get (id << 1 | 1, mid + 1, r, u, v));
}
void init (int n) {
m = n;
build (1, 1, n);
}
int get (int u, int v) {
return get (1, 1, m, u, v).A;
}
}ST;
void process() {
cin >> n;
string s;
cin >> s;
rep (i, 1, n) a[i] = s[i - 1] == 'C' ? -1 : 1;
ST.init(n);
cin >> Q;
rep (q, 1, Q) {
int L, R;
cin >> L >> R;
cout << ST.get(L, R) <<"\n";
}
}
#define file(name) if(fopen(name".inp","r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
main () {
// file("c");
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int num_Test = 1;
// cin >> num_Test;
while (num_Test--) {
process();
}
}
/*
onepunch +28
5 3
1 2 3 4 5
1 2
2 3
3 4
4 5
1 3
3 5
1 5
5 3
1 2 3 4 5
1 2
1 3
3 4
3 5
2 3
4 5
3 4
*/
컴파일 시 표준 에러 (stderr) 메시지
election.cpp:90:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
90 | main () {
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |