This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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);
vector<vector<int>> ver(n, vector<int> (n));
stack<int> stk;
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;
}
Or(ver[i], tmp);
}
while (stk.size()) stk.pop();
for (int i = n - 1; i >= 0; --i) {
if (a[i] == 1) {
if (stk.size()) {
int top = stk.top();
stk.pop();
tmp[top] = 0;
}
} else {
stk.push(i);
tmp[i] = 1;
}
Or(ver[n - i - 1], tmp);
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
--l, --r;
int ans = 0;
for (int i = l; i <= r; ++i) {
ans += ver[l][i];
}
cout << ans << '\n';
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |