Submission #52224

# Submission time Handle Problem Language Result Execution time Memory
52224 2018-06-25T05:31:51 Z kriii Fortune Telling 2 (JOI14_fortune_telling2) C++17
100 / 100
678 ms 39704 KB
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;

const int Z = 1 << 18;
vector<int> G[Z * 2];
int N, K, A[200200], B[200200], T[200200];

int cnt(int x, int l, int r)
{
	auto &v = G[x];
	return upper_bound(v.begin(), v.end(), r) - lower_bound(v.begin(), v.end(), l);
}

int cnt(int x, int y, int l, int r)
{
	int res = 0;
	x += Z; y += Z;
	while (x < y) {
		if (x & 1) res += cnt(x++, l, r);
		if (~y & 1) res += cnt(y--, l, r);
		x /= 2; y /= 2;
	} if (x == y) res += cnt(x, l, r);
	return res;
}

int get(int l, int r)
{
	int x = 1;
	while (x < Z) {
		x *= 2; x++;
		if (cnt(x, l, r) == 0) x--;
	}
	return x - Z;
}

int main()
{
	scanf("%d %d", &N, &K);
	for (int i = 0; i < N; i++) scanf("%d %d", &A[i], &B[i]);
	for (int j = 0; j < K; j++) {
		scanf("%d", &T[j]);
		G[j + Z].push_back(T[j]);
	}

	for (int i = Z - 1; i >= 1; i--) {
		vector<int> &a = G[i * 2];
		vector<int> &b = G[i * 2 + 1];
		vector<int> &c = G[i]; c.reserve(a.size() + b.size());
		int p = 0, q = 0;
		while (p < a.size() && q < b.size()) {
			if (a[p] < b[q]) c.push_back(a[p++]);
			else c.push_back(b[q++]);
		}
		while (p < a.size()) c.push_back(a[p++]);
		while (q < b.size()) c.push_back(b[q++]);
	}

	long long ans = 0;
	for (int i = 0; i < N; i++) {
		int p = 0;
		if (min(A[i], B[i]) != max(A[i], B[i])) {
			p = get(min(A[i], B[i]), max(A[i], B[i]) - 1);
		}
		int q = cnt(p, K-1, max(A[i], B[i]), 0x7fffffff);
		if (p && A[i] < B[i]) swap(A[i], B[i]);
		if (q & 1) swap(A[i], B[i]);
		ans += A[i];
	}
	printf("%lld\n", ans);

	return 0;
}

Compilation message

fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:52:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while (p < a.size() && q < b.size()) {
          ~~^~~~~~~~~~
fortune_telling2.cpp:52:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while (p < a.size() && q < b.size()) {
                          ~~^~~~~~~~~~
fortune_telling2.cpp:56:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while (p < a.size()) c.push_back(a[p++]);
          ~~^~~~~~~~~~
fortune_telling2.cpp:57:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while (q < b.size()) c.push_back(b[q++]);
          ~~^~~~~~~~~~
fortune_telling2.cpp:40:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &N, &K);
  ~~~~~^~~~~~~~~~~~~~~~~
fortune_telling2.cpp:41:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (int i = 0; i < N; i++) scanf("%d %d", &A[i], &B[i]);
                              ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:43:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &T[j]);
   ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 16 ms 12664 KB Output is correct
2 Correct 20 ms 12904 KB Output is correct
3 Correct 17 ms 12904 KB Output is correct
4 Correct 16 ms 13028 KB Output is correct
5 Correct 16 ms 13028 KB Output is correct
6 Correct 16 ms 13048 KB Output is correct
7 Correct 17 ms 13048 KB Output is correct
8 Correct 16 ms 13048 KB Output is correct
9 Correct 15 ms 13048 KB Output is correct
10 Correct 16 ms 13048 KB Output is correct
11 Correct 15 ms 13048 KB Output is correct
12 Correct 16 ms 13048 KB Output is correct
13 Correct 17 ms 13048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 12664 KB Output is correct
2 Correct 20 ms 12904 KB Output is correct
3 Correct 17 ms 12904 KB Output is correct
4 Correct 16 ms 13028 KB Output is correct
5 Correct 16 ms 13028 KB Output is correct
6 Correct 16 ms 13048 KB Output is correct
7 Correct 17 ms 13048 KB Output is correct
8 Correct 16 ms 13048 KB Output is correct
9 Correct 15 ms 13048 KB Output is correct
10 Correct 16 ms 13048 KB Output is correct
11 Correct 15 ms 13048 KB Output is correct
12 Correct 16 ms 13048 KB Output is correct
13 Correct 17 ms 13048 KB Output is correct
14 Correct 34 ms 14188 KB Output is correct
15 Correct 50 ms 15572 KB Output is correct
16 Correct 76 ms 17004 KB Output is correct
17 Correct 106 ms 18376 KB Output is correct
18 Correct 91 ms 18376 KB Output is correct
19 Correct 81 ms 18376 KB Output is correct
20 Correct 98 ms 18376 KB Output is correct
21 Correct 77 ms 18376 KB Output is correct
22 Correct 56 ms 18376 KB Output is correct
23 Correct 58 ms 18376 KB Output is correct
24 Correct 55 ms 18376 KB Output is correct
25 Correct 59 ms 18376 KB Output is correct
26 Correct 99 ms 18376 KB Output is correct
27 Correct 173 ms 18376 KB Output is correct
28 Correct 92 ms 18376 KB Output is correct
29 Correct 171 ms 18376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 12664 KB Output is correct
2 Correct 20 ms 12904 KB Output is correct
3 Correct 17 ms 12904 KB Output is correct
4 Correct 16 ms 13028 KB Output is correct
5 Correct 16 ms 13028 KB Output is correct
6 Correct 16 ms 13048 KB Output is correct
7 Correct 17 ms 13048 KB Output is correct
8 Correct 16 ms 13048 KB Output is correct
9 Correct 15 ms 13048 KB Output is correct
10 Correct 16 ms 13048 KB Output is correct
11 Correct 15 ms 13048 KB Output is correct
12 Correct 16 ms 13048 KB Output is correct
13 Correct 17 ms 13048 KB Output is correct
14 Correct 34 ms 14188 KB Output is correct
15 Correct 50 ms 15572 KB Output is correct
16 Correct 76 ms 17004 KB Output is correct
17 Correct 106 ms 18376 KB Output is correct
18 Correct 91 ms 18376 KB Output is correct
19 Correct 81 ms 18376 KB Output is correct
20 Correct 98 ms 18376 KB Output is correct
21 Correct 77 ms 18376 KB Output is correct
22 Correct 56 ms 18376 KB Output is correct
23 Correct 58 ms 18376 KB Output is correct
24 Correct 55 ms 18376 KB Output is correct
25 Correct 59 ms 18376 KB Output is correct
26 Correct 99 ms 18376 KB Output is correct
27 Correct 173 ms 18376 KB Output is correct
28 Correct 92 ms 18376 KB Output is correct
29 Correct 171 ms 18376 KB Output is correct
30 Correct 144 ms 38116 KB Output is correct
31 Correct 198 ms 38396 KB Output is correct
32 Correct 284 ms 38820 KB Output is correct
33 Correct 441 ms 39676 KB Output is correct
34 Correct 127 ms 39676 KB Output is correct
35 Correct 459 ms 39676 KB Output is correct
36 Correct 449 ms 39676 KB Output is correct
37 Correct 452 ms 39676 KB Output is correct
38 Correct 453 ms 39676 KB Output is correct
39 Correct 474 ms 39676 KB Output is correct
40 Correct 346 ms 39676 KB Output is correct
41 Correct 427 ms 39676 KB Output is correct
42 Correct 541 ms 39676 KB Output is correct
43 Correct 254 ms 39676 KB Output is correct
44 Correct 285 ms 39676 KB Output is correct
45 Correct 231 ms 39676 KB Output is correct
46 Correct 256 ms 39676 KB Output is correct
47 Correct 317 ms 39704 KB Output is correct
48 Correct 678 ms 39704 KB Output is correct
49 Correct 536 ms 39704 KB Output is correct