#include <iostream> #include <vector> #include <string> using namespace std; void update(vector<int> &T, int i, int v) { i++; while (i < T.size()) { T[i] += v; i += i & (-i); } } int get(vector<int> &T, int i) { int s = 0; i++; while (i > 0) { s += T[i]; i -= i & (-i); } return s; } int main() { int n; cin >> n; string a; cin >> a; vector<int> T(n + 1, 0); int NP = 0, NL; for (char c : a) { if (c == 'P') NP++; } NL = n - NP; int np = 0, nl = 0; for (int i = 0; i < n; ++i) { if (a[i] == 'P') { np++; int poz = NL + np - 1; //cout << "("<<i<<","<<poz+1<<")\n"; update(T, i+1, 1); update(T, poz + 1, -1); } else { nl++; int poz = nl - 1; update(T, poz, 1); update(T, i, -1); } //for (int i = 0; i < n; ++i) cout << get(T, i) << " "; cout << endl; } for (int i = 0; i < n; ++i) cout << get(T, i) << " "; cout << endl; 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 53 54 55 56 57 58 59 60 61 62 | #include <iostream> #include <vector> #include <string> using namespace std; void update(vector<int> &T, int i, int v) { i++; while (i < T.size()) { T[i] += v; i += i & (-i); } } int get(vector<int> &T, int i) { int s = 0; i++; while (i > 0) { s += T[i]; i -= i & (-i); } return s; } int main() { int n; cin >> n; string a; cin >> a; vector<int> T(n + 1, 0); int NP = 0, NL; for (char c : a) { if (c == 'P') NP++; } NL = n - NP; int np = 0, nl = 0; for (int i = 0; i < n; ++i) { if (a[i] == 'P') { np++; int poz = NL + np - 1; //cout << "("<<i<<","<<poz+1<<")\n"; update(T, i+1, 1); update(T, poz + 1, -1); } else { nl++; int poz = nl - 1; update(T, poz, 1); update(T, i, -1); } //for (int i = 0; i < n; ++i) cout << get(T, i) << " "; cout << endl; } for (int i = 0; i < n; ++i) cout << get(T, i) << " "; cout << endl; return 0; } |