답안 #1017820

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1017820 2024-07-09T10:26:59 Z KN200711 Election (BOI18_election) C++14
100 / 100
374 ms 29968 KB
# include <bits/stdc++.h>
# define ll long long
# define ld double
# define fi first
# define se second
# define pii pair<int, int>
# define pll pair<ll, ll>
using namespace std;

int val[500001];
pair<pii, pii> seg[2000020];

void build(int lf, int rg, int nd) {
	if(lf == rg) {
		seg[nd].fi.fi = val[lf];
		seg[nd].fi.se = seg[nd].se.fi = seg[nd].se.se = max(0, val[lf]);
	} else {
		int mid = (lf + rg) / 2;
		build(lf, mid, 2*nd+1);
		build(mid+1, rg, 2*nd+2);
		
		seg[nd].fi.fi = seg[2*nd + 1].fi.fi + seg[2*nd + 2].fi.fi;
		seg[nd].fi.se = max(seg[2*nd + 1].fi.se, seg[2*nd + 1].fi.fi + seg[2*nd + 2].fi.se);
		seg[nd].se.fi = max(seg[2*nd+2].se.fi, seg[2*nd+2].fi.fi + seg[2*nd+1].se.fi);
		seg[nd].se.se = max(seg[2*nd + 1].fi.se + seg[2*nd + 2].se.fi, max(seg[2*nd + 1].fi.fi + seg[2*nd + 2].se.se, seg[2*nd + 1].se.se + seg[2*nd + 2].fi.fi));
		
	}
}

pair<pii, pii> qry(int lf, int rg, int nd, int clf, int crg) {
	if(lf > crg || clf > rg) {
		return {{0, 0}, {0, 0}};
	} else if(clf <= lf && rg <= crg) return seg[nd];
	int mid = (lf + rg) / 2;
	
	pair<pii, pii> nw, l, r;
	l = qry(lf, mid, 2*nd+1, clf, crg);
	r = qry(mid+1, rg, 2*nd+2, clf, crg);
	
	nw.fi.fi = l.fi.fi + r.fi.fi;
	nw.fi.se = max(l.fi.se, l.fi.fi + r.fi.se);
	nw.se.fi = max(r.se.fi, r.fi.fi + l.se.fi);
	nw.se.se = max(l.fi.se + r.se.fi, max(l.fi.fi + r.se.se, l.se.se + r.fi.fi));
	
	return nw;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	int N;
	cin>>N;
	
	string S;
	cin>>S;
	
	for(int i=1;i<=N;i++) {
		if(S[i - 1] == 'C') val[i] = -1;
		else val[i] = 1;
	}
	
	build(1, N, 0);
	int Q;
	cin>>Q;
	while(Q--) {
		int L, R;
		cin>>L>>R;
		
		cout<<qry(1, N, 0, L, R).se.se<<"\n";
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 37 ms 6164 KB Output is correct
7 Correct 34 ms 5972 KB Output is correct
8 Correct 36 ms 6112 KB Output is correct
9 Correct 31 ms 5980 KB Output is correct
10 Correct 36 ms 6068 KB Output is correct
11 Correct 39 ms 6228 KB Output is correct
12 Correct 37 ms 6236 KB Output is correct
13 Correct 36 ms 6236 KB Output is correct
14 Correct 36 ms 6236 KB Output is correct
15 Correct 37 ms 6224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 37 ms 6164 KB Output is correct
7 Correct 34 ms 5972 KB Output is correct
8 Correct 36 ms 6112 KB Output is correct
9 Correct 31 ms 5980 KB Output is correct
10 Correct 36 ms 6068 KB Output is correct
11 Correct 39 ms 6228 KB Output is correct
12 Correct 37 ms 6236 KB Output is correct
13 Correct 36 ms 6236 KB Output is correct
14 Correct 36 ms 6236 KB Output is correct
15 Correct 37 ms 6224 KB Output is correct
16 Correct 374 ms 28904 KB Output is correct
17 Correct 287 ms 28644 KB Output is correct
18 Correct 335 ms 28904 KB Output is correct
19 Correct 253 ms 28324 KB Output is correct
20 Correct 326 ms 28100 KB Output is correct
21 Correct 323 ms 29844 KB Output is correct
22 Correct 327 ms 29812 KB Output is correct
23 Correct 332 ms 29968 KB Output is correct
24 Correct 338 ms 29668 KB Output is correct
25 Correct 324 ms 29248 KB Output is correct