답안 #591649

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
591649 2022-07-07T17:30:24 Z blue Election (BOI18_election) C++17
100 / 100
713 ms 63808 KB
#include <iostream>
#include <vector>
using namespace std;
 
using vi = vector<int>;
 
const int inf = 1'000'000'000;

vi p, s;

struct sdata
{
	int pmx = -inf;
	int smx = -inf;
	int ans = -inf;
};

sdata combine(sdata a, sdata b)
{
	return {max(a.pmx, b.pmx), max(a.smx, b.smx), max(max(a.ans, b.ans), a.pmx + b.smx)};
}

struct segtree
{
	int l;
	int r;
	sdata v;

	segtree* left = NULL;
	segtree* right = NULL;

	segtree()
	{	
		;
	}

	segtree(int L, int R)
	{
		l = L;
		r = R;
		if(l == r)
			v = sdata{p[l], s[l], p[l]+s[l]};
		else
		{
			int m = (l+r)/2;
			left = new segtree(l, m);
			right = new segtree(m+1, r);
			v = combine(left->v, right->v);
		}
	}

	sdata getrange(int L, int R)
	{
		if(L <= l && r <= R)
			return v;
		else if(R <= (l+r)/2)
			return left->getrange(L, R);
		else if(L >= (l+r)/2+1)
			return right->getrange(L, R);
		else
			return combine(left->getrange(L, R), right->getrange(L, R));
	}
};
 
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
 
	int N;
	cin >> N;
 
	vi A(1+N);
	for(int i = 1; i <= N; i++)
	{
		char c;
		cin >> c;
 
		if(c == 'T')
			A[i] = +1;
		else
			A[i] = -1;
	}
 
	p = vi(1+N);
	p[0] = 0;
	for(int i = 1; i <= N; i++)
		p[i] = p[i-1] + A[i];
 
	s = vi(1+N);
	s[N] = 0;
	for(int i = N-1; i >= 0; i--)
		s[i] = s[i+1] + A[i+1];

	segtree ST(0, N);
 
 
 
	int Q;
	cin >> Q;
 
	for(int q = 1; q <= Q; q++)
	{
		int l, r;
		cin >> l >> r;

		sdata d = ST.getrange(l-1, r);
 
		int pmx = d.pmx;
 
		int c1 = pmx - p[l-1];
 
		// cerr << "c1 = " << c1 << '\n';
		// cerr << "pmx = " << pmx << '\n';
 
 
		// int c2 = 0;
		int res = +s[r] + p[l-1];

		res = max(res, d.ans);
 
 
		res -= s[r] + p[l-1];

		res = max(res, c1);
 
		cout << res << '\n';
 
 
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 540 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 540 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 58 ms 7952 KB Output is correct
7 Correct 46 ms 8388 KB Output is correct
8 Correct 62 ms 8800 KB Output is correct
9 Correct 47 ms 8820 KB Output is correct
10 Correct 62 ms 8748 KB Output is correct
11 Correct 64 ms 8884 KB Output is correct
12 Correct 59 ms 8916 KB Output is correct
13 Correct 67 ms 9008 KB Output is correct
14 Correct 51 ms 8908 KB Output is correct
15 Correct 58 ms 8828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 540 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 58 ms 7952 KB Output is correct
7 Correct 46 ms 8388 KB Output is correct
8 Correct 62 ms 8800 KB Output is correct
9 Correct 47 ms 8820 KB Output is correct
10 Correct 62 ms 8748 KB Output is correct
11 Correct 64 ms 8884 KB Output is correct
12 Correct 59 ms 8916 KB Output is correct
13 Correct 67 ms 9008 KB Output is correct
14 Correct 51 ms 8908 KB Output is correct
15 Correct 58 ms 8828 KB Output is correct
16 Correct 713 ms 62744 KB Output is correct
17 Correct 467 ms 62376 KB Output is correct
18 Correct 590 ms 62604 KB Output is correct
19 Correct 480 ms 62088 KB Output is correct
20 Correct 704 ms 61828 KB Output is correct
21 Correct 711 ms 63696 KB Output is correct
22 Correct 703 ms 63544 KB Output is correct
23 Correct 701 ms 63808 KB Output is correct
24 Correct 689 ms 63452 KB Output is correct
25 Correct 700 ms 62900 KB Output is correct