Submission #670617

# Submission time Handle Problem Language Result Execution time Memory
670617 2022-12-09T18:41:39 Z kirakaminski968 Election (BOI18_election) C++17
100 / 100
471 ms 27868 KB
#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;

struct Node {
	int l_max, r_max, tot, ans;

	Node operator+(Node b) {
		Node ret;
		ret.l_max = max(l_max, b.l_max + tot);
		ret.r_max = max(r_max + b.tot, b.r_max);
		ret.tot = tot + b.tot;
		ret.ans = max(max(ans + b.tot, b.ans + tot), l_max + b.r_max);
		return ret;
	}
};

Node segtree[2000001];
char s[500001];
int n;

void build(int node = 1, int l = 1, int r = n) {
	if (l == r) {
		if (s[l] == 'T') segtree[node] = {1, 1, 1, 1};
		else segtree[node] = {0, 0, -1, 0};
	} else {
		int mid = (l + r) / 2;
		build(node * 2, l, mid);
		build(node * 2 + 1, mid + 1, r);
		segtree[node] = segtree[node * 2] + segtree[node * 2 + 1];
	}
}

Node query(int a, int b, int node = 1, int l = 1, int r = n) {
	if (l > b || r < a) return {0, 0, 0, 0};
	if (l >= a && r <= b) return segtree[node];
	int mid = (l + r) / 2;
	return query(a, b, node * 2, l, mid) + query(a, b, node * 2 + 1, mid + 1, r);
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	FOR(i, 1, n + 1) cin >> s[i];
	build();
	int q;
	cin >> q;
	while (q--) {
		int a, b;
		cin >> a >> b;
		cout << query(a, b).ans << '\n';
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 432 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 432 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 54 ms 5676 KB Output is correct
7 Correct 43 ms 5600 KB Output is correct
8 Correct 46 ms 5580 KB Output is correct
9 Correct 41 ms 5560 KB Output is correct
10 Correct 46 ms 5524 KB Output is correct
11 Correct 45 ms 5780 KB Output is correct
12 Correct 44 ms 5680 KB Output is correct
13 Correct 48 ms 5752 KB Output is correct
14 Correct 44 ms 5708 KB Output is correct
15 Correct 45 ms 5700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 432 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 54 ms 5676 KB Output is correct
7 Correct 43 ms 5600 KB Output is correct
8 Correct 46 ms 5580 KB Output is correct
9 Correct 41 ms 5560 KB Output is correct
10 Correct 46 ms 5524 KB Output is correct
11 Correct 45 ms 5780 KB Output is correct
12 Correct 44 ms 5680 KB Output is correct
13 Correct 48 ms 5752 KB Output is correct
14 Correct 44 ms 5708 KB Output is correct
15 Correct 45 ms 5700 KB Output is correct
16 Correct 430 ms 26828 KB Output is correct
17 Correct 358 ms 26396 KB Output is correct
18 Correct 410 ms 26640 KB Output is correct
19 Correct 329 ms 26092 KB Output is correct
20 Correct 433 ms 25960 KB Output is correct
21 Correct 442 ms 27712 KB Output is correct
22 Correct 422 ms 27564 KB Output is correct
23 Correct 471 ms 27868 KB Output is correct
24 Correct 435 ms 27584 KB Output is correct
25 Correct 419 ms 26956 KB Output is correct