Submission #231873

# Submission time Handle Problem Language Result Execution time Memory
231873 2020-05-15T06:37:16 Z iefnah06 Fortune Telling 2 (JOI14_fortune_telling2) C++11
100 / 100
740 ms 28552 KB
#include <bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;

using ll = long long;

template <typename K, typename V, typename Comp = std::less<K>>
using order_statistic_map = __gnu_pbds::tree<
	K, V, Comp,
	__gnu_pbds::rb_tree_tag,
	__gnu_pbds::tree_order_statistics_node_update
>;

template <typename K, typename Comp = std::less<K>>
using order_statistic_set = order_statistic_map<K, __gnu_pbds::null_type, Comp>;

const int MAXN = 2.1e5;
const int MAXQ = 2.1e5;
int n, q;
int ca[MAXN];
int cb[MAXN];
int qt[MAXQ];

const int S = 1 << 19;
int seg[2 * S];

void update(int i, int v) {
	if (seg[S+i] >= v) return;
	seg[S+i] = v;
	for (int a = (S+i)/2; a; a /= 2) {
		seg[a] = max(seg[2*a], seg[2*a+1]);
	}
}

int query(int l) {
	int res = -1;
	for (int a = S+l, b = S+S; a < b; a /= 2, b /= 2) {
		if (a & 1) {
			res = max(res, seg[a++]);
		}
		if (b & 1) {
			res = max(res, seg[--b]);
		}
	}
	return res;
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0);

	cin >> n >> q;
	vector<int> vals;
	for (int i = 0; i < n; i++) {
		cin >> ca[i] >> cb[i];
		vals.push_back(min(ca[i], cb[i]));
	}
	for (int i = 0; i < q; i++) {
		cin >> qt[i];
		vals.push_back(qt[i]);
	}

	sort(vals.begin(), vals.end());
	vals.erase(unique(vals.begin(), vals.end()), vals.end());
	auto lookup = [&](int v) {
		auto it = lower_bound(vals.begin(), vals.end(), v);
		assert(it != vals.end() && *it == v);
		return int(it - vals.begin());
	};

	vector<int> cards(n);
	iota(cards.begin(), cards.end(), 0);
	sort(cards.begin(), cards.end(), [&](int i, int j) {
		return max(ca[i], cb[i]) < max(ca[j], cb[j]);
	});
	vector<int> operations(q);
	iota(operations.begin(), operations.end(), 0);
	sort(operations.begin(), operations.end(), [&](int i, int j) {
		return qt[i] < qt[j];
	});

	order_statistic_set<pair<int, int>> os;
	for (int i = 0; i < q; i++) {
		os.insert({i, qt[i]});
	}
	auto operations_it = operations.begin();
	ll ans = 0;
	for (int a = 1; a < S*2; a++) {
		seg[a] = -1;
	}
	for (auto card : cards) {
		int lo = min(ca[card], cb[card]);
		int hi = max(ca[card], cb[card]);
		while (operations_it != operations.end() && qt[*operations_it] < hi) {
			os.erase({*operations_it, qt[*operations_it]});
			update(lookup(qt[*operations_it]), *operations_it);
			operations_it++;
		}

		int last_time = query(lookup(lo));
		int parity = (os.size() - os.order_of_key({last_time, -1})) % 2;
		if (last_time == -1) {
			ans += parity ? cb[card] : ca[card];
		} else {
			ans += parity ? lo : hi;
		}
	}
	cout << ans << '\n';

	return 0;
}

// Let's assume that a_i < b_i
// t_j < a_i: nothing happens
// a_i <= t_j < b_i: becomes b_i
// b_i <= t_j: flips
// For each card, let's fix the last operation of the second type, then the result only depends on the parity of (# of flips) after that operation
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4608 KB Output is correct
2 Correct 8 ms 4608 KB Output is correct
3 Correct 8 ms 4608 KB Output is correct
4 Correct 8 ms 4608 KB Output is correct
5 Correct 8 ms 4608 KB Output is correct
6 Correct 8 ms 4608 KB Output is correct
7 Correct 8 ms 4608 KB Output is correct
8 Correct 8 ms 4608 KB Output is correct
9 Correct 8 ms 4480 KB Output is correct
10 Correct 8 ms 4608 KB Output is correct
11 Correct 8 ms 4608 KB Output is correct
12 Correct 8 ms 4608 KB Output is correct
13 Correct 8 ms 4608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4608 KB Output is correct
2 Correct 8 ms 4608 KB Output is correct
3 Correct 8 ms 4608 KB Output is correct
4 Correct 8 ms 4608 KB Output is correct
5 Correct 8 ms 4608 KB Output is correct
6 Correct 8 ms 4608 KB Output is correct
7 Correct 8 ms 4608 KB Output is correct
8 Correct 8 ms 4608 KB Output is correct
9 Correct 8 ms 4480 KB Output is correct
10 Correct 8 ms 4608 KB Output is correct
11 Correct 8 ms 4608 KB Output is correct
12 Correct 8 ms 4608 KB Output is correct
13 Correct 8 ms 4608 KB Output is correct
14 Correct 25 ms 5632 KB Output is correct
15 Correct 49 ms 6776 KB Output is correct
16 Correct 72 ms 8056 KB Output is correct
17 Correct 94 ms 9204 KB Output is correct
18 Correct 97 ms 9204 KB Output is correct
19 Correct 93 ms 9380 KB Output is correct
20 Correct 94 ms 9208 KB Output is correct
21 Correct 89 ms 9204 KB Output is correct
22 Correct 81 ms 8816 KB Output is correct
23 Correct 85 ms 8816 KB Output is correct
24 Correct 80 ms 8692 KB Output is correct
25 Correct 77 ms 8820 KB Output is correct
26 Correct 77 ms 9076 KB Output is correct
27 Correct 85 ms 9204 KB Output is correct
28 Correct 76 ms 9204 KB Output is correct
29 Correct 90 ms 9360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4608 KB Output is correct
2 Correct 8 ms 4608 KB Output is correct
3 Correct 8 ms 4608 KB Output is correct
4 Correct 8 ms 4608 KB Output is correct
5 Correct 8 ms 4608 KB Output is correct
6 Correct 8 ms 4608 KB Output is correct
7 Correct 8 ms 4608 KB Output is correct
8 Correct 8 ms 4608 KB Output is correct
9 Correct 8 ms 4480 KB Output is correct
10 Correct 8 ms 4608 KB Output is correct
11 Correct 8 ms 4608 KB Output is correct
12 Correct 8 ms 4608 KB Output is correct
13 Correct 8 ms 4608 KB Output is correct
14 Correct 25 ms 5632 KB Output is correct
15 Correct 49 ms 6776 KB Output is correct
16 Correct 72 ms 8056 KB Output is correct
17 Correct 94 ms 9204 KB Output is correct
18 Correct 97 ms 9204 KB Output is correct
19 Correct 93 ms 9380 KB Output is correct
20 Correct 94 ms 9208 KB Output is correct
21 Correct 89 ms 9204 KB Output is correct
22 Correct 81 ms 8816 KB Output is correct
23 Correct 85 ms 8816 KB Output is correct
24 Correct 80 ms 8692 KB Output is correct
25 Correct 77 ms 8820 KB Output is correct
26 Correct 77 ms 9076 KB Output is correct
27 Correct 85 ms 9204 KB Output is correct
28 Correct 76 ms 9204 KB Output is correct
29 Correct 90 ms 9360 KB Output is correct
30 Correct 380 ms 21612 KB Output is correct
31 Correct 455 ms 23152 KB Output is correct
32 Correct 601 ms 24836 KB Output is correct
33 Correct 719 ms 28368 KB Output is correct
34 Correct 414 ms 21464 KB Output is correct
35 Correct 740 ms 28368 KB Output is correct
36 Correct 700 ms 28332 KB Output is correct
37 Correct 709 ms 28392 KB Output is correct
38 Correct 691 ms 28396 KB Output is correct
39 Correct 683 ms 28396 KB Output is correct
40 Correct 617 ms 28136 KB Output is correct
41 Correct 654 ms 28372 KB Output is correct
42 Correct 660 ms 28552 KB Output is correct
43 Correct 549 ms 27624 KB Output is correct
44 Correct 552 ms 27660 KB Output is correct
45 Correct 556 ms 27624 KB Output is correct
46 Correct 585 ms 26472 KB Output is correct
47 Correct 564 ms 26344 KB Output is correct
48 Correct 539 ms 28520 KB Output is correct
49 Correct 492 ms 28516 KB Output is correct