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>
#include <ext/pb_ds/assoc_container.hpp>
#define vi vector<int>
#define ve vector
#define ll long long
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define onbit __builtin_popcount
#define ii pair<int,int>
#define vvi vector<vi>
#define vii vector<ii>
#define gii greater<ii>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF 1e18
#define eps 1e-7
#define eps1 1e-2
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MAX_A 1e5+5
using namespace std;
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll MOD = 1e9+9;
const int nax = 2e3+5;
const int MAX_VAL = 1e6;
double PI=3.14159265359;
int arx[8]={1,1,0,-1,-1,-1, 0, 1};
int ary[8]={0,1,1, 1, 0,-1,-1,-1};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
int n;
string t;
bool vis[nax];
int main(){
optimise;
/*#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif*/
//setIO("redistricting");
cin>>n;
cin>>t;
int q;cin>>q;
vi pre(n,0);
vi suf(n,0);
for (int i = 0; i < n; ++i)
{
if (t[i]=='T') pre[i]++;
if (i) pre[i]+=pre[i-1];
}
for (int i = n-1; i >= 0; --i)
{
if (t[i]=='T') suf[i]++;
if (i<n-1) suf[i]+=suf[i+1];
}
while(q--)
{
int x,y;
cin>>x>>y;
x--;y--;
int mx=0;
int l=-1;
int r=n;
int ans;
if (x==0){
ans=0;
}else ans=pre[x-1];
while(r-l>1)
{
int mid=(r+l)/2;
if(pre[mid]>ans)r=mid;
else l=mid;
}
if (r>y){
cout << 0<<endl;
continue;
}
int cur=r;
int ans1;
if (y==n-1) ans1=0;
else ans1=suf[y+1];
l=-1;r=n;
while(r-l>1){
int mid=(r+l)/2;
if (suf[mid]>ans) l=mid;
else r=mid;
}
int cur1=l;
//cout << cur<<" "<<cur1<<endl;
//cout << suf[cur]<<" "<<pre[cur1]<<endl;
//cout << ans1<<" "<<ans<<endl;
cout <<max((suf[cur]-ans1-(y-cur+1-suf[cur]+ans1)),pre[cur1]-ans-(cur1-x+1-pre[cur1]+ans))<<endl;
}
}
Compilation message (stderr)
election.cpp: In function 'int main()':
election.cpp:66:13: warning: unused variable 'mx' [-Wunused-variable]
66 | int mx=0;
| ^~
election.cpp: In function 'void setIO(std::string)':
election.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
election.cpp:34:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | freopen((s + ".out").c_str(), "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... |