#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define inf (int)1e8
#define bitcount(x) __builtin_popcountll(x)
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(), x.end()
#define ss(x) (int)x.size()
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
void add(int &x, int k)
{
x += k;
x %= mod;
if(x < 0) x += mod;
}
void del(int &x, int k)
{
x -= k;
x %= mod;
if(x < 0) x += mod;
}
struct node
{
int p,s,sum,maxx;
node()
{
p = s = sum = maxx = 0;
}
node operator + (const node &b)
{
node ans;
ans.p = max(p,sum + b.p);
ans.s = max(b.s,b.sum + s);
ans.sum = sum + b.sum;
ans.maxx = max({maxx + b.sum,sum + b.maxx,p + b.s});
return ans;
}
}st[4 * maxn];
int n,q,a[maxn];
void build(int id,int l,int r)
{
if(l == r)
{
st[id].sum = a[l];
st[id].maxx = st[id].p = st[id].s = max(0,a[l]);
return;
}
int mid = l + r >> 1;
build(id << 1,l,mid);
build(id << 1 | 1,mid + 1,r);
st[id] = st[id << 1] + st[id << 1 | 1];
}
node get(int id,int l,int r,int u,int v)
{
if(l > v or r < u) return node();
if(u <= l and r <= v) return st[id];
int mid = l + r >> 1;
return get(id << 1,l,mid,u,v) + get(id << 1 | 1,mid + 1,r,u,v);
}
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
fo(i,1,n)
{
char t;
cin >> t;
a[i] = (t == 'T' ? 1 : -1);
}
cin >> q;
build(1,1,n);
while(q--)
{
int l,r;
cin >> l >> r;
cout << get(1,1,n,l,r).maxx;en;
}
}
Compilation message
election.cpp: In function 'void build(int, int, int)':
election.cpp:82:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
82 | int mid = l + r >> 1;
| ~~^~~
election.cpp: In function 'node get(int, int, int, int, int)':
election.cpp:91:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
91 | int mid = l + r >> 1;
| ~~^~~
election.cpp: At global scope:
election.cpp:94:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
94 | main()
| ^~~~
election.cpp: In function 'int main()':
election.cpp:99:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
election.cpp:100:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
63056 KB |
Output is correct |
2 |
Correct |
31 ms |
63064 KB |
Output is correct |
3 |
Correct |
26 ms |
63068 KB |
Output is correct |
4 |
Correct |
25 ms |
63056 KB |
Output is correct |
5 |
Correct |
26 ms |
62900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
63056 KB |
Output is correct |
2 |
Correct |
31 ms |
63064 KB |
Output is correct |
3 |
Correct |
26 ms |
63068 KB |
Output is correct |
4 |
Correct |
25 ms |
63056 KB |
Output is correct |
5 |
Correct |
26 ms |
62900 KB |
Output is correct |
6 |
Correct |
60 ms |
64424 KB |
Output is correct |
7 |
Correct |
57 ms |
64344 KB |
Output is correct |
8 |
Correct |
58 ms |
64288 KB |
Output is correct |
9 |
Correct |
60 ms |
64340 KB |
Output is correct |
10 |
Correct |
58 ms |
64588 KB |
Output is correct |
11 |
Correct |
59 ms |
64592 KB |
Output is correct |
12 |
Correct |
58 ms |
64564 KB |
Output is correct |
13 |
Correct |
64 ms |
64592 KB |
Output is correct |
14 |
Correct |
59 ms |
64592 KB |
Output is correct |
15 |
Correct |
57 ms |
64336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
63056 KB |
Output is correct |
2 |
Correct |
31 ms |
63064 KB |
Output is correct |
3 |
Correct |
26 ms |
63068 KB |
Output is correct |
4 |
Correct |
25 ms |
63056 KB |
Output is correct |
5 |
Correct |
26 ms |
62900 KB |
Output is correct |
6 |
Correct |
60 ms |
64424 KB |
Output is correct |
7 |
Correct |
57 ms |
64344 KB |
Output is correct |
8 |
Correct |
58 ms |
64288 KB |
Output is correct |
9 |
Correct |
60 ms |
64340 KB |
Output is correct |
10 |
Correct |
58 ms |
64588 KB |
Output is correct |
11 |
Correct |
59 ms |
64592 KB |
Output is correct |
12 |
Correct |
58 ms |
64564 KB |
Output is correct |
13 |
Correct |
64 ms |
64592 KB |
Output is correct |
14 |
Correct |
59 ms |
64592 KB |
Output is correct |
15 |
Correct |
57 ms |
64336 KB |
Output is correct |
16 |
Correct |
324 ms |
74616 KB |
Output is correct |
17 |
Correct |
292 ms |
74276 KB |
Output is correct |
18 |
Correct |
341 ms |
74324 KB |
Output is correct |
19 |
Correct |
265 ms |
73952 KB |
Output is correct |
20 |
Correct |
314 ms |
73536 KB |
Output is correct |
21 |
Correct |
341 ms |
75344 KB |
Output is correct |
22 |
Correct |
329 ms |
75348 KB |
Output is correct |
23 |
Correct |
326 ms |
75604 KB |
Output is correct |
24 |
Correct |
330 ms |
75092 KB |
Output is correct |
25 |
Correct |
321 ms |
74836 KB |
Output is correct |