#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 5e5 + 5;
int n, q;
int res[N];
array<int, 2> tr[4 * N];
string s;
vector<array<int, 2>> que[N];
array<int, 2> operator + (const array<int, 2> <, const array<int, 2> &rt) {
return {min(rt[0], lt[0] + rt[1]), lt[1] + rt[1]};
}
void upd(int i, int x, int id = 1, int l = 1, int r = n) {
if (l == r) {
tr[id] = {x, x};
return;
}
int m = (l + r) / 2;
if (i <= m) {
upd(i, x, id * 2, l, m);
} else {
upd(i, x, id * 2 + 1, m + 1, r);
}
tr[id] = tr[id * 2] + tr[id * 2 + 1];
}
array<int, 2> qry(int u, int v, int id = 1, int l = 1, int r = n) {
if (u <= l && r <= v) {
return tr[id];
}
int m = (l + r) / 2;
if (v <= m) {
return qry(u, v, id * 2, l, m);
}
if (m < u) {
return qry(u, v, id * 2 + 1, m + 1, r);
}
return qry(u, v, id * 2, l, m) + qry(u, v, id * 2 + 1, m + 1, r);
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> s >> q;
for (int i = 1; i <= q; ++i) {
int l, r; cin >> l >> r;
que[l].push_back({r, i});
}
vector<int> st;
for (int i = n; i; --i) {
if (s[i - 1] == 'T') {
st.push_back(i);
} else {
upd(i, 1);
if (st.size()) {
upd(st.back(), -1);
st.pop_back();
}
}
sort(que[i].begin(), que[i].end());
int k = 0;
for (auto [j, id] : que[i]) {
while (k < st.size() && st[k] <= j) {
++k;
}
res[id] = k + max(0, -qry(i, j)[0]);
}
}
for (int i = 1; i <= q; ++i) {
cout << res[i] << "\n";
}
return 0;
}
Compilation message
election.cpp: In function 'int main()':
election.cpp:73:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | while (k < st.size() && st[k] <= j) {
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
12124 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
12124 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
12124 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |