#include <bits/stdc++.h>
using namespace std;
signed main () {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
string s;
cin >> s;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
a[i] = (s[i] == 'C' ? 1 : -1);
}
auto Or = [&](vector<int>& x, const vector<int>& y) {
for (int i = 0; i < n; ++i) {
x[i] |= y[i];
}
};
vector<int> tmp(n);
stack<int> stk;
vector<vector<int>> ver(n, vector<int> (n));
for (int i = 0; i < n; ++i) {//if we start decreasingly
if (a[i] == 1) {
if (stk.size()) {
int top = stk.top();
stk.pop();
tmp[top] = 0;
}
} else {
stk.push(i);
tmp[i] = 1;
}
ver[i] = tmp;
}
while (stk.size()) stk.pop();
queue<int> que;
for (int i = 0; i < n; ++i) tmp[i] = 0;
vector<vector<int>> ver2(n, vector<int> (n));
for (int i = n - 1; i >= 0; --i) {
if (a[i] == 1) {
if (que.size()) {
int top = que.front();
que.pop();
tmp[top] = 0;
}
} else {
que.push(i);
tmp[i] = 1;
}
ver2[i] = tmp;
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
--l, --r;
int ans = 0;
vector<int> va = ver[r];
Or(va, ver2[l]);
for (int i = l; i <= r; ++i) {
ans += va[i];
}
cout << ans << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
31724 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
31724 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
31724 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |