#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename A>
void __print(const A &x);
template <typename A, typename B>
void __print(const pair<A, B> &p);
template <typename... A>
void __print(const tuple<A...> &t);
template <typename T>
void __print(stack<T> s);
template <typename T>
void __print(queue<T> q);
template <typename T, typename... U>
void __print(priority_queue<T, U...> q);
template <typename A>
void __print(const A &x) {
bool first = true;
cerr << '{';
for (const auto &i : x) {
cerr << (first ? "" : ","), __print(i);
first = false;
}
cerr << '}';
}
template <typename A, typename B>
void __print(const pair<A, B> &p) {
cerr << '(';
__print(p.first);
cerr << ',';
__print(p.second);
cerr << ')';
}
template <typename... A>
void __print(const tuple<A...> &t) {
bool first = true;
cerr << '(';
apply([&first](const auto &...args) { ((cerr << (first ? "" : ","), __print(args), first = false), ...); }, t);
cerr << ')';
}
template <typename T>
void __print(stack<T> s) {
vector<T> debugVector;
while (!s.empty()) {
T t = s.top();
debugVector.push_back(t);
s.pop();
}
reverse(debugVector.begin(), debugVector.end());
__print(debugVector);
}
template <typename T>
void __print(queue<T> q) {
vector<T> debugVector;
while (!q.empty()) {
T t = q.front();
debugVector.push_back(t);
q.pop();
}
__print(debugVector);
}
template <typename T, typename... U>
void __print(priority_queue<T, U...> q) {
vector<T> debugVector;
while (!q.empty()) {
T t = q.top();
debugVector.push_back(t);
q.pop();
}
__print(debugVector);
}
void _print() { cerr << "]\n"; }
template <typename Head, typename... Tail>
void _print(const Head &H, const Tail &...T) {
__print(H);
if (sizeof...(T))
cerr << ", ";
_print(T...);
}
#ifndef ONLINE_JUDGE
#define debug(...) cerr << "Line:" << __LINE__ << " [" << #__VA_ARGS__ << "] = ["; _print(__VA_ARGS__)
#else
#define debug(...)
#endif
#define int long long
char buf[1<<23],*p1=buf,*p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline int rd()
{
int s=0;
char ch=getchar(),last;
while(ch<'0'||ch>'9') last=ch,ch=getchar();
while(ch>='0'&&ch<='9') s=(s<<1)+(s<<3)+(ch^48),ch=getchar();
return last=='-'?-s:s;
}
int num[100];
inline void rt(int x)
{
if(x<0) putchar('-'),x=-x;;
int len=0;
do num[len++]=x%10;while(x/=10);
while(len--) putchar(num[len]+'0');
}
int n, q;
string s;
struct node {
int L, R;
int sum;
int best;
node operator + (const node& other) const {
node ret;
ret.sum = sum + other.sum;
ret.L = max(L, sum + other.L);
ret.R = max(other.R, other.sum + R);
ret.best = max({L + other.R, best + other.sum, sum + other.best});
return ret;
}
} st[2000010];
void build(int id, int L, int R) {
if (L == R) {
if (s[L - 1] == 'T') st[id] = {1, 1, 1, 1};
else st[id] = {0, 0, -1, 0};
return;
}
int mid = L + R >> 1;
build(id * 2, L, mid);
build(id * 2 + 1, mid + 1, R);
st[id] = st[id * 2] + st[id * 2 + 1];
}
node get(int id, int L, int R, int u, int v) {
if (u <= L && R <= v) return st[id];
int mid = L + R >> 1;
if (v <= mid) return get(id * 2, L, mid, u, v);
if (u > mid) return get(id * 2 + 1, mid + 1, R, u, v);
return get(id * 2, L, mid, u, v) + get(id * 2 + 1, mid + 1, R, u, v);
}
void solve(){
cin >> n;
cin >> s;
build(1, 1, n);
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
cout << get(1, 1, n, l, r).best << '\n';
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
solve();
}
Compilation message
election.cpp: In function 'void build(long long int, long long int, long long int)':
election.cpp:149:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
149 | int mid = L + R >> 1;
| ~~^~~
election.cpp: In function 'node get(long long int, long long int, long long int, long long int, long long int)':
election.cpp:157:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
157 | int mid = L + R >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
52 ms |
9292 KB |
Output is correct |
7 |
Correct |
45 ms |
9828 KB |
Output is correct |
8 |
Correct |
51 ms |
9744 KB |
Output is correct |
9 |
Correct |
38 ms |
9820 KB |
Output is correct |
10 |
Correct |
50 ms |
9828 KB |
Output is correct |
11 |
Correct |
58 ms |
9944 KB |
Output is correct |
12 |
Correct |
53 ms |
9944 KB |
Output is correct |
13 |
Correct |
59 ms |
9976 KB |
Output is correct |
14 |
Correct |
44 ms |
10060 KB |
Output is correct |
15 |
Correct |
47 ms |
9836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
52 ms |
9292 KB |
Output is correct |
7 |
Correct |
45 ms |
9828 KB |
Output is correct |
8 |
Correct |
51 ms |
9744 KB |
Output is correct |
9 |
Correct |
38 ms |
9820 KB |
Output is correct |
10 |
Correct |
50 ms |
9828 KB |
Output is correct |
11 |
Correct |
58 ms |
9944 KB |
Output is correct |
12 |
Correct |
53 ms |
9944 KB |
Output is correct |
13 |
Correct |
59 ms |
9976 KB |
Output is correct |
14 |
Correct |
44 ms |
10060 KB |
Output is correct |
15 |
Correct |
47 ms |
9836 KB |
Output is correct |
16 |
Correct |
441 ms |
43340 KB |
Output is correct |
17 |
Correct |
398 ms |
42968 KB |
Output is correct |
18 |
Correct |
373 ms |
43404 KB |
Output is correct |
19 |
Correct |
351 ms |
42744 KB |
Output is correct |
20 |
Correct |
461 ms |
42420 KB |
Output is correct |
21 |
Correct |
443 ms |
44488 KB |
Output is correct |
22 |
Correct |
436 ms |
44212 KB |
Output is correct |
23 |
Correct |
445 ms |
44440 KB |
Output is correct |
24 |
Correct |
506 ms |
43976 KB |
Output is correct |
25 |
Correct |
456 ms |
43596 KB |
Output is correct |