#include <bits/stdc++.h>
using namespace std;
const int base = 1<<19;
const int N = 3e5+3;
int tree[2*base], tmp[N];
int cnt(int v){
v+=base;
int out=0;
while(v>0){
out+=tree[v];
v/=2;
}
return out;
}
void upd(int a, int b, int x){
a+=base-1;
b+=base+1;
while(a/2!=b/2){
if(a%2==0) tree[a+1]+=x;
if(b%2==1) tree[b-1]+=x;
a/=2; b/=2;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
string s;
queue<int> Q;
cin >> n >> s;
for(int i=0; i<n; i++){
if(s[i]=='L'){
Q.push(i);
}
}
for(int i=0; i<n; i++){
if(s[i]=='P' && !Q.empty()){
int w=Q.front();
tmp[i]++; tmp[w]++;
if(i+1!=w){
upd(i+1, w-1, 2);
}
s[w]='P';
Q.pop();
}
if(s[i]=='L') Q.pop();
}
for(int i=0; i<n; i++){
cout << tmp[i]+cnt(i) << ' ';
}
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #include <bits/stdc++.h> using namespace std; const int base = 1<<19; const int N = 3e5+3; int tree[2*base], tmp[N]; int cnt(int v){ v+=base; int out=0; while(v>0){ out+=tree[v]; v/=2; } return out; } void upd(int a, int b, int x){ a+=base-1; b+=base+1; while(a/2!=b/2){ if(a%2==0) tree[a+1]+=x; if(b%2==1) tree[b-1]+=x; a/=2; b/=2; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; string s; queue<int> Q; cin >> n >> s; for(int i=0; i<n; i++){ if(s[i]=='L'){ Q.push(i); } } for(int i=0; i<n; i++){ if(s[i]=='P' && !Q.empty()){ int w=Q.front(); tmp[i]++; tmp[w]++; if(i+1!=w){ upd(i+1, w-1, 2); } s[w]='P'; Q.pop(); } if(s[i]=='L') Q.pop(); } for(int i=0; i<n; i++){ cout << tmp[i]+cnt(i) << ' '; } return 0; } |
English