This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |