#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 = 5e5 + 5, mod = 1e9 + 7; //!
int t[N], t2[N], 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));
}
void upd(int id, int val) {
for(id; id >= 1; id -= id & (-id)) t[id] += val;
} int n;
int get(int id) {
int ans = 0;
for(id; id <= n; id += id & (-id)) ans += t[id];
return ans;
}
void UPD(int id, int val) {
for(id; id >= 1; id -= id & (-id)) t2[id] += val;
}
int GET(int id) {
int ans = 0;
for(id; id <= n; id += id & (-id)) ans += t2[id];
return ans;
}
main() {
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
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]);
upd(i, 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;
for(int i = n; i >= 1; i--) {
while(v.size() && p[v.back()] <= p[i]) {
if(l == v.size()) {
l--;
upd(v.back(), 1);
UPD(v.back(), -1);
upd(1,1, v.back(), 1, n, 1);
}
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), upd(v[l], -1), UPD(v[l], 1), ++l;
while(l > 0 && p[v[l - 1]] - p[i - 1] <= 0) upd(1, 1, v[l - 1], 1, n, 1), upd(v[l - 1], 1), UPD(v[l - 1], -1), --l;
if(x[i].size()) {
} else continue;
for(int j = 0; j < x[i].size(); j++) {
ans[x[i][j].f] = max(0, get(1, i, x[i][j].s, 1, n) - get(x[i][j].s + 1)) + GET(i) - GET(x[i][j].s + 1);
}
}
for(int i = 1; i <= q; i++) cout << ans[i] << endl;
}
Compilation message
election.cpp: In function 'void upd(int, int)':
election.cpp:37:6: warning: statement has no effect [-Wunused-value]
37 | for(id; id >= 1; id -= id & (-id)) t[id] += val;
| ^~
election.cpp: In function 'int get(int)':
election.cpp:41:6: warning: statement has no effect [-Wunused-value]
41 | for(id; id <= n; id += id & (-id)) ans += t[id];
| ^~
election.cpp: In function 'void UPD(int, int)':
election.cpp:45:6: warning: statement has no effect [-Wunused-value]
45 | for(id; id >= 1; id -= id & (-id)) t2[id] += val;
| ^~
election.cpp: In function 'int GET(int)':
election.cpp:49:6: warning: statement has no effect [-Wunused-value]
49 | for(id; id <= n; id += id & (-id)) ans += t2[id];
| ^~
election.cpp: At global scope:
election.cpp:52:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
52 | main() {
| ^~~~
election.cpp: In function 'int main()':
election.cpp:73:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | if(l == v.size()) {
| ~~^~~~~~~~~~~
election.cpp:82:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | while(l < v.size() && p[v[l]] - p[i - 1] > 0) upd(1, 1, v[l], 1, n, -1), upd(v[l], -1), UPD(v[l], 1), ++l;
| ~~^~~~~~~~~~
election.cpp:87:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for(int j = 0; j < x[i].size(); j++) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
12212 KB |
Output is correct |
2 |
Correct |
11 ms |
12212 KB |
Output is correct |
3 |
Correct |
9 ms |
12208 KB |
Output is correct |
4 |
Correct |
10 ms |
12132 KB |
Output is correct |
5 |
Correct |
9 ms |
12220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
12212 KB |
Output is correct |
2 |
Correct |
11 ms |
12212 KB |
Output is correct |
3 |
Correct |
9 ms |
12208 KB |
Output is correct |
4 |
Correct |
10 ms |
12132 KB |
Output is correct |
5 |
Correct |
9 ms |
12220 KB |
Output is correct |
6 |
Correct |
104 ms |
16968 KB |
Output is correct |
7 |
Correct |
86 ms |
16500 KB |
Output is correct |
8 |
Correct |
97 ms |
16588 KB |
Output is correct |
9 |
Correct |
97 ms |
16864 KB |
Output is correct |
10 |
Correct |
102 ms |
16804 KB |
Output is correct |
11 |
Correct |
88 ms |
17180 KB |
Output is correct |
12 |
Correct |
88 ms |
17060 KB |
Output is correct |
13 |
Correct |
98 ms |
17244 KB |
Output is correct |
14 |
Correct |
109 ms |
17032 KB |
Output is correct |
15 |
Correct |
84 ms |
16920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
12212 KB |
Output is correct |
2 |
Correct |
11 ms |
12212 KB |
Output is correct |
3 |
Correct |
9 ms |
12208 KB |
Output is correct |
4 |
Correct |
10 ms |
12132 KB |
Output is correct |
5 |
Correct |
9 ms |
12220 KB |
Output is correct |
6 |
Correct |
104 ms |
16968 KB |
Output is correct |
7 |
Correct |
86 ms |
16500 KB |
Output is correct |
8 |
Correct |
97 ms |
16588 KB |
Output is correct |
9 |
Correct |
97 ms |
16864 KB |
Output is correct |
10 |
Correct |
102 ms |
16804 KB |
Output is correct |
11 |
Correct |
88 ms |
17180 KB |
Output is correct |
12 |
Correct |
88 ms |
17060 KB |
Output is correct |
13 |
Correct |
98 ms |
17244 KB |
Output is correct |
14 |
Correct |
109 ms |
17032 KB |
Output is correct |
15 |
Correct |
84 ms |
16920 KB |
Output is correct |
16 |
Correct |
886 ms |
47884 KB |
Output is correct |
17 |
Correct |
679 ms |
44252 KB |
Output is correct |
18 |
Correct |
841 ms |
45348 KB |
Output is correct |
19 |
Correct |
779 ms |
47060 KB |
Output is correct |
20 |
Correct |
753 ms |
47432 KB |
Output is correct |
21 |
Correct |
901 ms |
49680 KB |
Output is correct |
22 |
Correct |
830 ms |
48692 KB |
Output is correct |
23 |
Correct |
845 ms |
49808 KB |
Output is correct |
24 |
Correct |
897 ms |
48644 KB |
Output is correct |
25 |
Correct |
783 ms |
47528 KB |
Output is correct |