답안 #61451

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
61451 2018-07-26T04:03:08 Z ainta(#1774) Election (BOI18_election) C++11
100 / 100
1004 ms 19732 KB
#include<cstdio>
#include<algorithm>
#include<vector>
#define N_ 501000
#define SZ 524288
using namespace std;
struct Tree {
	int L, R, S, M;
}IT[SZ + SZ];
int n, S[N_];
char p[N_];
Tree Merge(Tree a, Tree b) {
	return { min(a.L,a.S+b.L), min(b.R,b.S+a.R), a.S+b.S, max(max(a.M-b.S, b.M-a.S), -a.L-b.R)  };
}
void Put(int a, int b) {
	a += SZ;
	IT[a].L = IT[a].R = min(b, 0);
	IT[a].M = max(-b, 0);
	IT[a].S = b;
	while (a != 1) {
		a >>= 1;
		IT[a] = Merge(IT[a * 2], IT[a * 2 + 1]);
	}
}
Tree Calc(int nd, int b, int e, int s, int l) {
	if (s > l)return { 0,0,0,0 };
	if (b == s&&e == l)return IT[nd];
	int m = (b + e) >> 1;
	if (s <= m && l > m) {
		return Merge(Calc(nd * 2, b, m, s, min(m, l)), Calc(nd * 2 + 1, m + 1, e, max(m + 1, s), l));
	}
	if (s <= m)return Calc(nd * 2, b, m, s, min(m, l));
	if (l > m)return Calc(nd * 2 + 1, m + 1, e, max(m + 1, s), l);
}
int main() {
	int i;

	scanf("%d", &n);
	scanf("%s", p + 1);
	S[0] = 0;
	for (i = 1; i <= n; i++) {
		if (p[i] == 'C')Put(i, 1);
		else Put(i, -1);
	}
	int Q;
	scanf("%d", &Q);
	while (Q--) {
		int b, e;
		scanf("%d%d", &b, &e);
		Tree tt = Calc(1, 0, SZ - 1, b, e);
		printf("%d\n", tt.M);
	}
}

Compilation message

election.cpp: In function 'Tree Calc(int, int, int, int, int)':
election.cpp:34:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
election.cpp: In function 'int main()':
election.cpp:38:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
election.cpp:39:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%s", p + 1);
  ~~~~~^~~~~~~~~~~~~
election.cpp:46:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &Q);
  ~~~~~^~~~~~~~~~
election.cpp:49:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &b, &e);
   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 504 KB Output is correct
2 Correct 5 ms 616 KB Output is correct
3 Correct 6 ms 616 KB Output is correct
4 Correct 4 ms 616 KB Output is correct
5 Correct 5 ms 616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 504 KB Output is correct
2 Correct 5 ms 616 KB Output is correct
3 Correct 6 ms 616 KB Output is correct
4 Correct 4 ms 616 KB Output is correct
5 Correct 5 ms 616 KB Output is correct
6 Correct 100 ms 3044 KB Output is correct
7 Correct 130 ms 3060 KB Output is correct
8 Correct 81 ms 3060 KB Output is correct
9 Correct 105 ms 3132 KB Output is correct
10 Correct 102 ms 3132 KB Output is correct
11 Correct 90 ms 3272 KB Output is correct
12 Correct 82 ms 3272 KB Output is correct
13 Correct 106 ms 3324 KB Output is correct
14 Correct 117 ms 3324 KB Output is correct
15 Correct 117 ms 3324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 504 KB Output is correct
2 Correct 5 ms 616 KB Output is correct
3 Correct 6 ms 616 KB Output is correct
4 Correct 4 ms 616 KB Output is correct
5 Correct 5 ms 616 KB Output is correct
6 Correct 100 ms 3044 KB Output is correct
7 Correct 130 ms 3060 KB Output is correct
8 Correct 81 ms 3060 KB Output is correct
9 Correct 105 ms 3132 KB Output is correct
10 Correct 102 ms 3132 KB Output is correct
11 Correct 90 ms 3272 KB Output is correct
12 Correct 82 ms 3272 KB Output is correct
13 Correct 106 ms 3324 KB Output is correct
14 Correct 117 ms 3324 KB Output is correct
15 Correct 117 ms 3324 KB Output is correct
16 Correct 989 ms 18712 KB Output is correct
17 Correct 1004 ms 18912 KB Output is correct
18 Correct 970 ms 18912 KB Output is correct
19 Correct 684 ms 18912 KB Output is correct
20 Correct 888 ms 18912 KB Output is correct
21 Correct 803 ms 19732 KB Output is correct
22 Correct 833 ms 19732 KB Output is correct
23 Correct 960 ms 19732 KB Output is correct
24 Correct 829 ms 19732 KB Output is correct
25 Correct 911 ms 19732 KB Output is correct