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>
# define fi first
# define se second
using namespace std;
vector< pair<int, int> > pref[500001], suff[500001];
int ans[500001];
int N;
string S;
const int MXN = 5e5 + 1;
int fen[MXN + 1];
void upd(int a, int b) {
while(a <= MXN) {
fen[a] += b;
a += a&(-a);
}
}
int qry(int a) {
int fn = 0;
while(a > 0) {
fn += fen[a];
a -= a&(-a);
}
return fn;
}
void calc() {
vector< pair<int, int> > L, V;
L.clear();
V.clear();
for(int i=0;i<=MXN;i++) fen[i] = 0;
for(int i=0;i<N;i++) {
// cout<<i<<endl;
if(V.size() == 0) V.push_back(make_pair(i, i));
else {
while(V.back().se != i - 1) {
}
V.back().se++;
}
if(S[i] == 'C') {
if(V.size() == 0) {
L.back().se++;
} else {
L.push_back(V.back());
L.push_back(make_pair(-1, 1));
V.clear();
}
} else {
if(V.size() == 1) {
upd(V.back().fi + 1, 1);
upd(V.back().se + 2, -1);
}
// if(V.size() > 0) cout<<i<<" "<<V.back().fi<<" "<<V.back().se<<endl;
if(L.size() && L.back().se == 1) {
L.pop_back();
if(V.size() == 0) V.push_back(L.back());
else V.back().fi = L.back().fi;
L.pop_back();
} else if(L.size()) L.back().se--;
}
for(int k=0;k<pref[i].size();k++) {
// cout<<"qry : "<<pref[i][k].se<<" "<<pref[i][k].fi<<endl;
ans[pref[i][k].se] = max(ans[pref[i][k].se], qry(pref[i][k].fi + 1));
}
}
}
int main() {
cin>>N;
cin>>S;
int Q;
cin>>Q;
for(int tc = 1;tc<=Q;tc++) {
int a, b;
cin>>a>>b;
if(a > b) swap(a, b);
a--;
b--;
pref[b].push_back(make_pair(a, tc));
suff[a].push_back(make_pair(b, tc));
}
// calculate prefnya dulu
calc();
string T = "";
for(int i=N-1;i>=0;i--) T += S[i];
S = T;
for(int i=0;i<N;i++) pref[i].clear();
for(int i=N-1;i>=0;i--) {
for(int k=0;k<suff[i].size();k++) {
pref[N - i - 1].push_back(make_pair(N - suff[i][k].fi - 1, suff[i][k].se));
}
}
calc();
for(int i=1;i<=Q;i++) cout<<ans[i]<<"\n";
return 0;
}
Compilation message (stderr)
election.cpp: In function 'void calc()':
election.cpp:70:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int k=0;k<pref[i].size();k++) {
| ~^~~~~~~~~~~~~~~
election.cpp: In function 'int main()':
election.cpp:102:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int k=0;k<suff[i].size();k++) {
| ~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |