#include <bits/stdc++.h>
using namespace std;
struct info {
int elim, sum, minPref, minSuf;
info operator + ( const info &r ) const {
info l = *this;
info ans;
ans.elim = -(min( l.minPref, l.minSuf ) + min( r.minPref, r.minSuf ));
ans.sum = l.sum + r.sum;
ans.minPref = l.minPref + (l.minPref == sum ? r.minPref : 0);
ans.minSuf = r.minSuf + (r.minSuf == sum ? l.minPref : 0);
return ans;
}
};
const int MAX_N = 5e5;
int vote[MAX_N + 1];
info segTree[4 * MAX_N];
void init( int v, int l, int r ) {
if ( l == r ) {
segTree[v] = { (vote[l] == -1), vote[l], -(vote[l] == -1), -(vote[l] == -1) };
return;
}
int mid = (l + r) / 2;
init( v * 2 + 1, l, mid );
init( v * 2 + 2, mid + 1, r );
segTree[v] = segTree[v * 2 + 1] + segTree[v * 2 + 2];
}
info query( int v, int l, int r, int lq, int rq ) {
if ( lq <= l && r <= rq )
return segTree[v];
int mid = (l + r) / 2;
if ( mid + 1 > rq )
return query( v * 2 + 1, l, mid, lq, rq );
if ( mid < lq )
return query( v * 2 + 2, mid + 1, r, lq, rq );
return query( v * 2 + 1, l, mid, lq, rq ) + query( v * 2 + 2, mid + 1, r, lq, rq );
}
int main() {
int n, q;
cin >> n;
for ( int i = 1; i <= n; i++ ) {
char ch;
cin >> ch;
vote[i] = (ch == 'C' ? 1 : -1);
}
init( 0, 1, n );
cin >> q;
for ( int i = 0; i < q; i++ ) {
int l, r;
cin >> l >> r;
cout << query( 0, 1, n, l, r ).elim << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |