// 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;
int st[MAXN << 2][2];
void update(int u, int val, int op, int id = 1, int l = 1, int r = n){
if (u < l || r < u) return;
if (l == r) return void (st[id][op] = val);
int mid = l + r >> 1;
update(u, val, op, id << 1, l, mid);
update(u, val, op, id << 1 | 1, mid + 1, r);
st[id][op] = max(st[id << 1][op], st[id << 1 | 1][op]);
}
int get(int u, int v, int op, int id = 1, int l = 1, int r = n){
if (v < l || r < u) return 0;
if (u <= l && r <= v) return st[id][op];
int mid = l + r >> 1;
return max(get(u, v, op, id << 1, l, mid), get(u, v, op, id << 1 | 1, mid + 1, r));
}
void Solve() {
cin >> n >> s >> q;
For(i, 1, n) {
if (s[i-1] == 'C') pref[i] = pref[i - 1] - 1;
if (s[i-1] == 'T') pref[i] = pref[i - 1] + 1;
}
Ford(i, n, 1) {
if (s[i-1] == 'C') suff[i] = suff[i + 1] - 1;
if (s[i-1] == 'T') suff[i] = suff[i + 1] + 1;
}
For(i, 1, n) update(i, pref[i], 0), update(i, suff[i], 1);
while(q--){
int l, r; cin >> l >> r;
int mx1 = get(l, r, 0);
int mx2 = get(l, r, 1);
mx1 -= pref[l - 1];
mx2 -= suff[r + 1];
cout << max(mx1, mx2) << '\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:83:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | freopen("lat.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
election.cpp:84:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | 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... |