// author : anhtun_hi , nqg
#include <bits/stdc++.h>
#define ll long long
#define ii pair<ll, ll>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define reset(h, v) memset(h, v, sizeof h)
#define Forv(i, a) for(auto& i : a)
#define For(i, a, b) for(int i = a; i <= b; ++i)
#define Ford(i, a, b) for(int i = a; i >= b; --i)
#define c_bit(i) __builtin_popcountll(i)
#define Bit(mask, i) ((mask >> i) & 1LL)
#define onbit(mask, i) ((mask) bitor (1LL << i))
#define offbit(mask, i) ((mask) &~ (1LL << i))
#define Log2(x) (63 - __builtin_clzll(x))
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
using namespace std;
namespace std {
template <typename T, int D>
struct _vector : public vector <_vector <T, D - 1>> {
static_assert(D >= 1, "Dimension must be positive!");
template <typename... Args>
_vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
};
template <typename T> struct _vector <T, 1> : public vector <T> {
_vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
};
template <class A, class B> bool minimize(A &a, B b){return a > b ? a = b, true : false;}
template <class A, class B> bool maximize(A &a, B b){return a < b ? a = b, true : false;}
}
const int dx[] = {0, 0, +1, -1}, dy[] = {-1, +1, 0, 0}, LOG = 20, base = 311, inf = 1e9 + 5;
const int MAXN = 1e6 + 5;
const ll mod = 1e9 + 7;
const ll oo = 1e18;
//#define int long long
int n, q, pref[MAXN], suff[MAXN];
string s;
struct Node{
int ans, sum, maxpre, maxsuf;
Node(){
ans = sum = maxpre = maxsuf = 0;
}
Node(int s1, int s2, int s3, int s4){
ans = s1, sum = s2, maxpre = s3, maxsuf = s4;
}
friend Node operator + (const Node& x, const Node &y){
Node res;
res.sum = x.sum + y.sum;
res.maxpre = max(x.maxpre, x.sum + y.maxpre);
res.maxsuf = max(y.maxsuf, x.maxsuf + y.sum);
res.ans = max({x.ans, y.ans, x.maxpre + y.maxsuf, x.ans + y.sum, x.sum + y.ans});
return res;
}
} st[MAXN << 2], zero;
void update(int u, char val, int id = 1, int l = 1, int r = n){
if (u < l || r < u) return;
if (l == r) {
if (val == 'C') st[id] = Node(0, -1, 0, 0);
if (val == 'T') st[id] = Node(1, 1, 1, 1);
return;
}
int mid = l + r >> 1;
update(u, val, id << 1, l, mid);
update(u, val, id << 1 | 1, mid + 1, r);
st[id] = st[id << 1] + st[id << 1 | 1];
}
Node get(int u, int v, int id = 1, int l = 1, int r = n){
if (v < l || r < u) return zero;
if (u <= l && r <= v) return st[id];
int mid = l + r >> 1;
return get(u, v, id << 1, l, mid) + get(u, v, id << 1 | 1, mid + 1, r);
}
void Solve() {
cin >> n >> s >> q;
For(i, 1, n) update(i, s[i - 1]);
while(q--){
int l, r; cin >> l >> r;
cout << get(l, r).ans << '\n';
}
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
if(fopen("lat.inp", "r")) {
freopen("lat.inp", "r", stdin);
freopen("lat.out", "w", stdout);
}
int t = 1;
// cin >> t;
while(t --) Solve();
return 0;
}
Compilation message (stderr)
election.cpp: In function 'int main()':
election.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | freopen("lat.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
election.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | freopen("lat.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |