Submission #342559

#TimeUsernameProblemLanguageResultExecution timeMemory
342559pure_memElection (BOI18_election)C++14
100 / 100
720 ms27436 KiB
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
 
#define ll long long
#define X first
#define Y second
#define MP make_pair
 
using namespace std;
 
const int N = 5e5 + 3, mod = 998244353;
const ll INF = 1e10;

struct node{
	int L, R, ans, sum;
}t[N * 4];
int n, q;
node merge(const node &x, const node &y){
	node tmp;
	tmp.L = max(x.L, x.sum + y.L);
	tmp.R = max(x.R + y.sum, y.R);
	tmp.sum = x.sum + y.sum;
	tmp.ans = max(x.L + y.R, max(x.sum + y.ans, x.ans + y.sum));	
	return tmp;
}
void build(int v, int tl, int tr){
	if(tl == tr){
		char x;
		cin >> x;
		if(x == 'T')
			t[v] = {1, 1, 1, 1};	
		else
			t[v] = {0, 0, 0, -1};
		return;
	}
	int tm = (tl + tr) / 2;
	build(v * 2, tl, tm), build(v * 2 + 1, tm + 1, tr);
	t[v] = merge(t[v * 2], t[v * 2 + 1]);
}
node get(int v, int tl, int tr, int l, int r){
	if(tl > r || l > tr)
		return {};
	if(tl >= l && tr <= r)
		return t[v];
	int tm = (tl + tr) / 2;
	return merge(get(v * 2, tl, tm, l, r), get(v * 2 + 1, tm + 1, tr, l, r));
}

int main () {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> n;
	build(1, 1, n);
	cin >> q;
	for(int l, r;q;q--){
		cin >> l >> r;
		cout << get(1, 1, n, l, r).ans << "\n";	
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...