Submission #703690

#TimeUsernameProblemLanguageResultExecution timeMemory
703690Chal1shkanElection (BOI18_election)C++14
82 / 100
62 ms10120 KiB
# include <bits/stdc++.h>
 
# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
# define deb(x) cerr << #x  << " = " << x << endl; 
# define pll pair <ll, ll>

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
 
const ll maxn = 1e5 + 25;
const ll inf = 1e18 + 0;
const ll mod = 1e9 + 7;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};
 
using namespace std;

ll n, q;
string s;
struct node
{
	ll pref, suff, sum, ans;
} t[maxn * 4];

node combine (node l, node r)
{
	node res;
	res.pref = max(l.pref, l.sum + r.pref);
	res.suff = max(r.suff, r.sum + l.suff);
	res.sum = l.sum + r.sum;
	res.ans = max ({l.ans + r.sum, r.ans + l.sum, l.pref + r.suff});
	return res;
}

void build (ll v = 1, ll tl = 1, ll tr = n)
{
	if (tl == tr)
	{
		ll val = max((ll)(s[tl - 1] == 'T' ? 1 : -1), 0LL);
		t[v] = {val, val, (s[tl - 1] == 'T' ? 1 : -1), val};
		return;
	}
	else
	{
		ll tm = (tl + tr) >> 1;
		build (v * 2, tl, tm);;
		build (v * 2 + 1, tm + 1, tr);
		t[v] = combine(t[v * 2], t[v * 2 + 1]);
	}
}

node get (ll l, ll r, ll v = 1, ll tl = 1, ll tr = n)
{
	if (tr < l || r < tl) return {0, 0, 0, 0};
	if (l <= tl && tr <= r) return t[v];
	ll tm = (tl + tr) >> 1;
	return combine(get(l, r, v * 2, tl, tm), get(l, r, v * 2  +1, tm + 1, tr));
}

void ma1n (/* SABR */)
{
	cin >> n >> s >> q;
	build();
	while (q--)
	{
		ll l, r;
		cin >> l >> r;
		cout << get(l, r).ans << nl;
	}
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
//  freopen("file.in", "r", stdin);
//  freopen("file.out", "w", stdout);
    int ttt = 1;	
//  cin >> ttt;
    for (int test = 1; test <= ttt; ++test)
    {
//      cout << "Case " << test << ":" << ' ';
        ma1n();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...