#include "bits/stdc++.h"
using namespace std;
int a[500010];
int p[500010];
int solve(int l, int r) {
int cur = 0;
int ans = 0;
int mx = 0;
int mn = 0;
for(int i = l; i <= r; i++) {
cur += a[i];
mn = min(mn, cur);
mx = max(mx, cur - mn);
}
ans += mx - cur;
cout << mx << ' ' << cur << endl;
return ans;
}
char s[500010];
int opt[500010];
const int inf = 1e9;
vector <pair <int, int>> g[500010];
int ans[500010];
int n;
int t[500010 * 4];
int prop[500010 * 4];
int rmq[500010 * 4];
void pushdown(int c) {
int l = c << 1;
int r = l + 1;
t[l] = max(t[l], rmq[l] - prop[c]);
t[r] = max(t[r], rmq[r] - prop[c]);
prop[l] = min(prop[l], prop[c]);
prop[r] = min(prop[r], prop[c]);
}
void update(int x, int y, int val, int c = 1, int b = 1, int e = n) {
if(x <= b && e <= y) {
prop[c] = min(prop[c], val);
t[c] = max(t[c], rmq[c] - prop[c]);
return ;
}
if(y < b || e < x) return ;
pushdown(c);
int l = c << 1;
int r = l + 1;
int m = (b + e) >> 1;
update(x, y, val, l, b, m);
update(x, y, val, r, m+1, e);
t[c] = max(t[l], t[r]);
}
int query(int x, int y, int c = 1, int b = 1, int e = n) {
if(x <= b && e <= y) {
return t[c];
}
if(y < b || e < x) return -inf;
pushdown(c);
int m = (b + e) >> 1;
int l = c << 1;
int r = l + 1;
return max(query(x, y, l, b, m), query(x, y, r, m+1, e));
}
void build(int c = 1, int b = 1, int e = n) {
if(b == e) {
rmq[c] = p[b];
return ;
}
int m = (b + e) >> 1;
int l = c << 1;
int r = l + 1;
build(l, b, m);
build(r, m+1, e);
rmq[c] = max(rmq[l], rmq[r]);
}
int main(int argc, char const *argv[])
{
scanf("%d", &n);
scanf("%s", s);
for(int i = 1; i <= n; i++) {
a[i] = s[i - 1] == 'C' ? 1 : -1;
}
int q;
scanf("%d", &q);
int id = 0;
while(q--) {
int l, r;
scanf("%d %d", &l, &r);
g[l].emplace_back(r, id++);
}
fill(opt, opt + n + 1, inf);
fill(prop, prop + (4 * n) + 1, inf);
fill(t, t + (4 * n) + 1, -inf);
int add = 0;
for(int i = n; i >= 1; i--) {
p[i] = -add;
add += a[i];
}
build();
add = 0;
for(int i = n; i >= 1; i--) {
add += a[i];
update(i, n, min(p[i], -add));
for(auto j : g[i]) {
int mx = query(i, j.first);
ans[j.second] = mx - (p[j.first] + add);
}
}
for(int i = 0; i < id; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
Compilation message
election.cpp: In function 'int main(int, const char**)':
election.cpp:80:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
election.cpp:81:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", s);
~~~~~^~~~~~~~~
election.cpp:86:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
~~~~~^~~~~~~~~~
election.cpp:90:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &l, &r);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
12280 KB |
Output is correct |
2 |
Correct |
15 ms |
12400 KB |
Output is correct |
3 |
Correct |
17 ms |
12400 KB |
Output is correct |
4 |
Correct |
17 ms |
12552 KB |
Output is correct |
5 |
Correct |
19 ms |
12644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
12280 KB |
Output is correct |
2 |
Correct |
15 ms |
12400 KB |
Output is correct |
3 |
Correct |
17 ms |
12400 KB |
Output is correct |
4 |
Correct |
17 ms |
12552 KB |
Output is correct |
5 |
Correct |
19 ms |
12644 KB |
Output is correct |
6 |
Correct |
153 ms |
19620 KB |
Output is correct |
7 |
Correct |
103 ms |
19932 KB |
Output is correct |
8 |
Correct |
115 ms |
20936 KB |
Output is correct |
9 |
Correct |
114 ms |
22164 KB |
Output is correct |
10 |
Correct |
123 ms |
23144 KB |
Output is correct |
11 |
Correct |
121 ms |
24256 KB |
Output is correct |
12 |
Correct |
165 ms |
25200 KB |
Output is correct |
13 |
Correct |
125 ms |
26176 KB |
Output is correct |
14 |
Correct |
144 ms |
27064 KB |
Output is correct |
15 |
Correct |
137 ms |
28168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
12280 KB |
Output is correct |
2 |
Correct |
15 ms |
12400 KB |
Output is correct |
3 |
Correct |
17 ms |
12400 KB |
Output is correct |
4 |
Correct |
17 ms |
12552 KB |
Output is correct |
5 |
Correct |
19 ms |
12644 KB |
Output is correct |
6 |
Correct |
153 ms |
19620 KB |
Output is correct |
7 |
Correct |
103 ms |
19932 KB |
Output is correct |
8 |
Correct |
115 ms |
20936 KB |
Output is correct |
9 |
Correct |
114 ms |
22164 KB |
Output is correct |
10 |
Correct |
123 ms |
23144 KB |
Output is correct |
11 |
Correct |
121 ms |
24256 KB |
Output is correct |
12 |
Correct |
165 ms |
25200 KB |
Output is correct |
13 |
Correct |
125 ms |
26176 KB |
Output is correct |
14 |
Correct |
144 ms |
27064 KB |
Output is correct |
15 |
Correct |
137 ms |
28168 KB |
Output is correct |
16 |
Correct |
1316 ms |
70244 KB |
Output is correct |
17 |
Correct |
948 ms |
73868 KB |
Output is correct |
18 |
Correct |
1031 ms |
82104 KB |
Output is correct |
19 |
Correct |
871 ms |
91408 KB |
Output is correct |
20 |
Correct |
1072 ms |
99424 KB |
Output is correct |
21 |
Correct |
1168 ms |
108652 KB |
Output is correct |
22 |
Correct |
1198 ms |
116440 KB |
Output is correct |
23 |
Correct |
1112 ms |
124172 KB |
Output is correct |
24 |
Correct |
1090 ms |
131196 KB |
Output is correct |
25 |
Correct |
1012 ms |
138108 KB |
Output is correct |