This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
struct Data {
int l=0,r=0,s=0,a=0;
Data():l(0){}
Data(bool b) : l(b), r(b), s(b?1:-1), a(b){}
Data(Data a, Data b) : l(max(a.l, a.s+b.l)), r(max(b.r,b.s+a.r)), s(a.s+b.s), a(max(max(a.a+b.s, b.a+a.s), a.l+b.r)){}
};
vector<vector<Data>> tb(500100, vector<Data>(20));
int main() {
cin.sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
string str;
cin >> n >> str >> q;
for(int i = 1; i <= n; i++) {
tb[i][0] = Data(str[i-1]=='T');
}
for(int j = 1; j < 20; j++)
for(int i = 1; i < n-(1<<j)+2; i++)
tb[i][j] = Data(tb[i][j-1], tb[i+(1<<j-1)][j-1]);
while(q--){
Data d;
int a, b;
cin >> a >> b;
for(int i = 20;i--;)
if(a+(1<<i)-2<b)
d = Data(d,tb[a][i]), a+=(1<<i);
cout << d.a << '\n';
}
}
Compilation message (stderr)
election.cpp: In function 'int main()':
election.cpp:21:51: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
21 | tb[i][j] = Data(tb[i][j-1], tb[i+(1<<j-1)][j-1]);
| ~^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |