#include <iostream> #include <vector> #include <cstdint> #define int long long constexpr bool dbg = false; #define DEBUG if constexpr(dbg) constexpr int maxn = 3e5 + 7, treeSize = 1 << 20; int n; bool prawo[maxn]; std::vector<std::pair<int, int>> V; int treeNum[treeSize]; std::pair<int, int> treeSeries[treeSize]; void addNum(int l, int r, int x) { l += treeSize / 2; r += treeSize / 2; treeNum[l] += x; if(l != r) treeNum[r] += x; while(l / 2 != r / 2) { if(l % 2 == 0) treeNum[l + 1] += x; if(r % 2 == 1) treeNum[r - 1] += x; l /= 2; r /= 2; } } int queryNum(int i) { i += treeSize / 2; int sum = 0; while(i) { sum += treeNum[i]; i /= 2; } return sum; } int globalStart; void addSeries(int l, int r, int step, int mL, int mR, int v) { if(mR < l || r < mL) return; if(l <= mL && mR <= mL) { treeSeries[v].first += globalStart; treeSeries[v].second += step; globalStart += step * (mR - mL + 1); return; } addSeries(l, r, step, mL, (mL + mR) / 2, v * 2); addSeries(l, r, step, (mL + mR) / 2 + 1, mR, v * 2 + 1); } int querySeries(int i, int mL, int mR, int v) { if(mL > i || i > mR) return 0; int ans = treeSeries[v].first + treeSeries[v].second * (i - mL); if(mL != mR) { ans += querySeries(i, mL, (mL + mR) / 2, v * 2); ans += querySeries(i, (mL + mR) / 2 + 1, mR, v * 2 + 1); } return ans; } int query(int i) { return queryNum(i) + querySeries(i, 0, treeSize / 2 - 1, 1); } void input() { scanf("%lld", &n); char c; for(int i = 0; i < n; i++) { scanf(" %c", &c); prawo[i] = (c == 'P'); } } void calcV() { int i = 0; while(i < n && !prawo[i]) i++; if(i == n) return; std::pair<int, int> cur = {1, 0}; i++; while(i < n) { if(prawo[i] && prawo[i - 1]) cur.first++; else if(prawo[i]) { V.push_back(cur); cur = {1, 0}; } else cur.second++; i++; } if(cur.second != 0) V.push_back(cur); } void output() { for(int i = 0; i < n; i++) printf("%lld ", query(i)); } int32_t main() { input(); calcV(); int i = 0, carry = 0; while(i < n && !prawo[i]) i++; for(auto& [r, l] : V) { r += carry; DEBUG std::cerr << r << ' ' << l << std::endl; if(r < l) { globalStart = 1; addSeries(i, i + r - 1, 2, 0, treeSize / 2 - 1, 1); int top = 1 + 2 * (r - 1); addNum(i + r, i + l - 1, top + 1); globalStart = top; addSeries(i + l, i + l + r - 1, -2, 0, treeSize / 2 - 1, 1); } if(r > l) { globalStart = 1; addSeries(i, i + l - 1, 2, 0, treeSize / 2 - 1, 1); int top = 1 + 2 * (l - 1); addNum(i + l, i + r - 1, top + 1); globalStart = top; addSeries(i + r, i + r + l - 1, -2, 0, treeSize / 2 - 1, 1); } if(l == r) { globalStart = 1; addSeries(i, i + l - 1, 2, 0, treeSize / 2 - 1, 1); int top = 1 + 2 * (l - 1); globalStart = top; addSeries(i + l, i + r + l - 1, -2, 0, treeSize / 2 - 1, 1); } i += l; carry = r; } output(); 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | #include <iostream> #include <vector> #include <cstdint> #define int long long constexpr bool dbg = false; #define DEBUG if constexpr(dbg) constexpr int maxn = 3e5 + 7, treeSize = 1 << 20; int n; bool prawo[maxn]; std::vector<std::pair<int, int>> V; int treeNum[treeSize]; std::pair<int, int> treeSeries[treeSize]; void addNum(int l, int r, int x) { l += treeSize / 2; r += treeSize / 2; treeNum[l] += x; if(l != r) treeNum[r] += x; while(l / 2 != r / 2) { if(l % 2 == 0) treeNum[l + 1] += x; if(r % 2 == 1) treeNum[r - 1] += x; l /= 2; r /= 2; } } int queryNum(int i) { i += treeSize / 2; int sum = 0; while(i) { sum += treeNum[i]; i /= 2; } return sum; } int globalStart; void addSeries(int l, int r, int step, int mL, int mR, int v) { if(mR < l || r < mL) return; if(l <= mL && mR <= mL) { treeSeries[v].first += globalStart; treeSeries[v].second += step; globalStart += step * (mR - mL + 1); return; } addSeries(l, r, step, mL, (mL + mR) / 2, v * 2); addSeries(l, r, step, (mL + mR) / 2 + 1, mR, v * 2 + 1); } int querySeries(int i, int mL, int mR, int v) { if(mL > i || i > mR) return 0; int ans = treeSeries[v].first + treeSeries[v].second * (i - mL); if(mL != mR) { ans += querySeries(i, mL, (mL + mR) / 2, v * 2); ans += querySeries(i, (mL + mR) / 2 + 1, mR, v * 2 + 1); } return ans; } int query(int i) { return queryNum(i) + querySeries(i, 0, treeSize / 2 - 1, 1); } void input() { scanf("%lld", &n); char c; for(int i = 0; i < n; i++) { scanf(" %c", &c); prawo[i] = (c == 'P'); } } void calcV() { int i = 0; while(i < n && !prawo[i]) i++; if(i == n) return; std::pair<int, int> cur = {1, 0}; i++; while(i < n) { if(prawo[i] && prawo[i - 1]) cur.first++; else if(prawo[i]) { V.push_back(cur); cur = {1, 0}; } else cur.second++; i++; } if(cur.second != 0) V.push_back(cur); } void output() { for(int i = 0; i < n; i++) printf("%lld ", query(i)); } int32_t main() { input(); calcV(); int i = 0, carry = 0; while(i < n && !prawo[i]) i++; for(auto& [r, l] : V) { r += carry; DEBUG std::cerr << r << ' ' << l << std::endl; if(r < l) { globalStart = 1; addSeries(i, i + r - 1, 2, 0, treeSize / 2 - 1, 1); int top = 1 + 2 * (r - 1); addNum(i + r, i + l - 1, top + 1); globalStart = top; addSeries(i + l, i + l + r - 1, -2, 0, treeSize / 2 - 1, 1); } if(r > l) { globalStart = 1; addSeries(i, i + l - 1, 2, 0, treeSize / 2 - 1, 1); int top = 1 + 2 * (l - 1); addNum(i + l, i + r - 1, top + 1); globalStart = top; addSeries(i + r, i + r + l - 1, -2, 0, treeSize / 2 - 1, 1); } if(l == r) { globalStart = 1; addSeries(i, i + l - 1, 2, 0, treeSize / 2 - 1, 1); int top = 1 + 2 * (l - 1); globalStart = top; addSeries(i + l, i + r + l - 1, -2, 0, treeSize / 2 - 1, 1); } i += l; carry = r; } output(); return 0; } |