#include<iostream>
#include<stdio.h>
#include<stdint.h>
#include<iomanip>
#include<algorithm>
#include<utility>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<deque>
#include<math.h>
#include<assert.h>
#include<string.h>
using namespace std;
const int64_t INF = (int64_t) 1e18 + 100;
const int64_t mINF = (int64_t) 1e6 * 2 + 100;
const int64_t MOD = 1e9 + 7;
const int64_t MOD2 = 998244353;
const int nbit = 31;
const int ndig = 10;
const int nchar = 26;
const int D = 4;
int dr[D] = {-1, 0, 1, 0};
int dc[D] = {0, 1, 0, -1};
const int Dk = 8;
int drk[Dk] = {2, 2, -2, -2, 1, 1, -1, -1};
int dck[Dk] = {1, -1, 1, -1, 2, -2, 2, -2};
struct Node
{
int sum;
int pre;
Node(int sum_, int pre_) : sum(sum_), pre(pre_) {}
};
struct Solution
{
int n;
int sum;
int max_;
vector<int> ans;
vector<Node> tree;
vector<pair<int, int> > v;
Solution() {}
void solve()
{
string s;
cin >> n >> s;
while(__builtin_popcount(n) != 1) n++;
tree.resize(2 * n, Node(0, 0));
int q;
cin >> q;
ans.resize(q, 0);
for(int i = 0; i < q; i++)
{
int l;
int r;
cin >> l >> r;
l--; r--;
v.push_back(make_pair(l, r));
}
Run(s);
int m = (int) s.length();
reverse(s.begin(), s.end());
for(int i = 0; i < q; i++)
{
v[i].first = m - v[i].first - 1;
v[i].second = m - v[i].second - 1;
swap(v[i].first, v[i].second);
}
Run(s);
for(int i = 0; i < q; i++)
{
cout << ans[i] << "\n";
}
}
void Run(string& s)
{
for(int i = 0; i < (int) s.length(); i++)
{
if(s[i] == 'T') tree[n + i] = Node(1, 1);
else tree[n + i] = Node(-1, 0);
}
for(int i = n - 1; i >= 1; i--)
{
tree[i].sum = tree[i * 2].sum + tree[i * 2 + 1].sum;
tree[i].pre = max(tree[i * 2].pre, tree[i * 2].sum + tree[i * 2 + 1].pre);
}
for(int i = 0; i < (int) v.size(); i++)
{
int l = v[i].first;
int r = v[i].second;
sum = 0;
max_ = 0;
get(1, 0, n - 1, l, r);
ans[i] = max(ans[i], max_);
}
}
void get(int node, int nleft, int nright, int left, int right)
{
if(left > nright || nleft > right) return;
if(left <= nleft && nright <= right)
{
max_ = max(max_, sum + tree[node].pre);
sum += tree[node].sum;
return;
}
int mid = nleft + (nright - nleft) / 2;
get(node * 2, nleft, mid, left, right);
get(node * 2 + 1, mid + 1, nright, left, right);
}
void modadd(int& t1, int t2)
{
t1 += t2;
if(t1 >= MOD) t1-= MOD;
return;
}
void modsub(int& t1, int t2)
{
t1 -= t2;
if(t1 < 0) t1 += MOD;
return;
}
int modmul(int t1, int t2)
{
int64_t res = 1LL * t1 * t2;
return res % MOD;
}
int Abs(int tmp)
{
if(tmp < 0) return -tmp;
return tmp;
}
int MASK(int j)
{
return (1 << j);
}
int BIT(int tmp, int j)
{
int x = tmp & MASK(j);
if(x != 0) return 1;
return 0;
}
};
void __setup()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
}
int main()
{
__setup();
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
Solution().solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |