#include "bits/stdc++.h"
using namespace std;
const int N=400001;
int n;
string s;
int r[N], l[N];
int sum[4*N];
void upd(int i, int x, int lx, int rx){
sum[x]++;
if(rx-lx==1) return;
int m=(lx+rx)/2;
if(i<m) upd(i, 2*x+1, lx, m);
else upd(i, 2*x+2, m, rx);
}
void upd(int i) { upd(i, 0, 0, 2*n+1); }
int get_sum(int l, int r, int x, int lx, int rx){
if(lx>=r || rx<=l) return 0;
if(lx>=l && rx<=r) return sum[x];
int m=(lx+rx)/2;
return get_sum(l, r, 2*x+1, lx, m)+get_sum(l, r, 2*x+2, m, rx);
}
int get_sum(int l, int r) {
if(r<=l) return 0;
return get_sum(l, r, 0, 0, 2*n+1);
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> s;
vector<int> white, black;
for(int i=n; i<2*n; i++){
if(s[i]=='W') white.push_back(i+1);
else black.push_back(i+1);
}
int j=0, k=0;
for(int i=0; i<n; i++){
if(s[i]=='B') {
l[white[j]]=i+1, r[i+1]=white[j];
j++;
}
else{
l[black[k]]=i+1, r[i+1]=black[k];
k++;
}
}
// for(int i=0; i<n; i++){
// cout << "r[" << i+1 << "] = " << r[i+1] << "\n";
// }
long long ans=0;
for(int i=1; i<=2*n; i++){
if(r[i]) continue;
ans+=get_sum(l[i]+1, i);
upd(l[i]);
}
cout << (long long)n*(n-1)/2-ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Incorrect |
0 ms |
316 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Incorrect |
0 ms |
316 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Incorrect |
0 ms |
316 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Incorrect |
0 ms |
316 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |