#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define f first
#define s second
#define endl "\n"
const int N = 2e5 + 5, mod = 1e9 + 7; //!
int t, p[N], tree[4 * N], lazy[4 * N], ans[N];
vector<pair<int,int> > x[N];
void push(int u ,int l,int r) {
tree[u] += lazy[u];
if(l != r) lazy[2 * u] += lazy[u], lazy[2 * u + 1] += lazy[u];
lazy[u] = 0;
return;
}
void upd(int u, int st, int en, int l,int r,int v) {
if(lazy[u]) push(u, l, r);
if(l > en || r < st) return;
if(st <= l && r <= en) {
lazy[u] = v;
push(u, l, r);
return;
}
int mid = (l + r) / 2;
upd(2 * u, st, en, l, mid, v); upd(2 * u + 1, st, en, mid + 1, r, v);
tree[u] = max(tree[2 * u], tree[2 * u + 1]);
}
int get(int u, int st,int en, int l,int r) {
if(lazy[u]) push(u, l, r);
if(l > en || r < st) return -N;
if(st <= l && r <= en) return tree[u];
int mid = (l + r) / 2;
return max(get(2 * u, st, en, l, mid), get(2 * u + 1, st, en, mid + 1, r));
}
main() {
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int n;
cin >> n;
for(int i = 1; i <= n; i++) {
char c; cin >> c;
p[i] = p[i - 1] + (c == 'T' ? 1 : -1);
upd(1, 1, i, 1, n, p[i] - p[i - 1]);
}
int q; cin >> q;
for(int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r;
x[l].push_back({i, r});
}
vector<int> v;
int l = 0;
int cnt = 0;
for(int i = n; i >= 1; i--) {
while(v.size() && p[v.back()] <= p[i]) {
if(l == v.size()) {
l--;
upd(1,1, v.back(), 1, n, 1);
--cnt;
}
v.pop_back();
}
v.push_back(i);
while(l < v.size() && p[v[l]] - p[i - 1] > 0) upd(1, 1, v[l], 1, n, -1), ++cnt, ++l;
while(l > 0 && p[v[l - 1]] - p[i - 1] <= 0) upd(1, 1, v[l - 1], 1, n, 1), --cnt, --l;
if(x[i].size()) {
} else continue;
for(int j = 0; j < x[i].size(); j++) {
ans[x[i][j].f] = max(0ll, get(1, i, x[i][j].s, 1, n) - (p[n] - p[x[i][j].s])) + cnt;
}
}
for(int i = 1; i <= q; i++) cout << ans[i] << " ";
}
Compilation message
election.cpp:36:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
36 | main() {
| ^~~~
election.cpp: In function 'int main()':
election.cpp:57:9: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | if(l == v.size()) {
| ~~^~~~~~~~~~~
election.cpp:65:11: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | while(l < v.size() && p[v[l]] - p[i - 1] > 0) upd(1, 1, v[l], 1, n, -1), ++cnt, ++l;
| ~~^~~~~~~~~~
election.cpp:70:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int j = 0; j < x[i].size(); j++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
5204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
5204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
5204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |