Submission #656673

# Submission time Handle Problem Language Result Execution time Memory
656673 2022-11-08T03:23:46 Z SanguineChameleon Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
251 ms 24348 KB
// BEGIN BOILERPLATE CODE

#include <bits/stdc++.h>
using namespace std;

#ifdef KAMIRULEZ
	const bool kami_loc = true;
	const long long kami_seed = 69420;
#else
	const bool kami_loc = false;
	const long long kami_seed = chrono::steady_clock::now().time_since_epoch().count();
#endif

const string kami_fi = "kamirulez.inp";
const string kami_fo = "kamirulez.out";
mt19937_64 kami_gen(kami_seed);

long long rand_range(long long rmin, long long rmax) {
	uniform_int_distribution<long long> rdist(rmin, rmax);
	return rdist(kami_gen);
}

void file_io(string fi, string fo) {
	if (fi != "" && (!kami_loc || fi == kami_fi)) {
		freopen(fi.c_str(), "r", stdin);
	}
	if (fo != "" && (!kami_loc || fo == kami_fo)) {
		freopen(fo.c_str(), "w", stdout);
	}
}

void set_up() {
	if (kami_loc) {
		file_io(kami_fi, kami_fo);
	}
	ios_base::sync_with_stdio(0);
	cin.tie(0);
}

void just_do_it();

void just_exec_it() {
	if (kami_loc) {
		auto pstart = chrono::steady_clock::now();
		just_do_it();
		auto pend = chrono::steady_clock::now();
		long long ptime = chrono::duration_cast<chrono::milliseconds>(pend - pstart).count();
		string bar(50, '=');
		cout << '\n' << bar << '\n';
		cout << "Time: " << ptime << " ms" << '\n';
	}
	else {
		just_do_it();
	}
}

int main() {
	set_up();
	just_exec_it();
	return 0;
}

// END BOILERPLATE CODE

// BEGIN MAIN CODE

const int ms = 2e5 + 20;
pair<int, int> a[ms];
pair<int, int> b[ms];
int f[ms];
int qs[ms];
int ma[ms];
int bit[ms * 4];

void update1(int px, int pp) {
	for (int i = px; i < ms * 4; i += i & (-i)) {
		bit[i] = max(bit[i], pp);
	}
}

int get1(int px) {
	int res = 0;
	for (int i = px; i > 0; i -= i & (-i)) {
		res = max(res, bit[i]);
	}
	return res;
}

void update2(int px) {
	for (int i = px; i < ms * 4; i += i & (-i)) {
		bit[i]++;
	}
}

int get2(int px) {
	int res = 0;
	for (int i = px; i > 0; i -= i & (-i)) {
		res += bit[i];
	}
	return res;
}

void just_do_it() {
	int n, q;
	cin >> n >> q;
	vector<pair<int, pair<int, int>>> p;
	for (int i = 1; i <= n; i++) {
		cin >> a[i].first >> a[i].second;
		if (a[i].first <= a[i].second) {
			f[i] = 0;
		}
		else {
			swap(a[i].first, a[i].second);
			f[i] = 1;
		}
		b[i] = a[i];
		p.push_back({a[i].first, {i, 1}});
		p.push_back({a[i].second - 1, {i, -1}});
		p.push_back({a[i].second, {i, 2}});
	}
	for (int i = 1; i <= q; i++) {
		cin >> qs[i];
		p.push_back({qs[i], {i, 3}});
	}
	sort(p.begin(), p.end());
	int cc = 1;
	int sz = p.size();
	for (int i = 0; i < sz; i++) {
		if (p[i].second.second == 1) {
			a[p[i].second.first].first = cc;
		}
		if (p[i].second.second == 2) {
			a[p[i].second.first].second = cc;
		}
		if (p[i].second.second == 3) {
			qs[p[i].second.first] = cc;
		}
		if (i < sz - 1 && p[i].first != p[i + 1].first) {
			cc++;
		}
	}
	p.clear();
	for (int i = 1; i <= n; i++) {
		p.push_back({a[i].first, {1, i}});
	}
	for (int i = 1; i <= q; i++) {
		p.push_back({qs[i], {2, i}});
	}
	sort(p.begin(), p.end(), greater<pair<int, pair<int, int>>>());
	for (auto x: p) {
		if (x.second.first == 2) {
			update1(x.first, x.second.second);
		}
		else {
			ma[x.second.second] = get1(a[x.second.second].second - 1);
		}
	}
	for (int i = 1; i <= n; i++) {
		if (ma[i] > 0) {
			f[i] = 1;
		}
	}
	fill_n(bit, ms * 4, 0);
	p.clear();
	for (int i = 1; i <= n; i++) {
		p.push_back({a[i].second, {1, i}});
	}
	for (int i = 1; i <= q; i++) {
		p.push_back({qs[i], {2, i}});
	}
	sort(p.begin(), p.end(), greater<pair<int, pair<int, int>>>());
	cc = 0;
	for (auto x: p) {
		if (x.second.first == 2) {
			update2(x.second.second);
			cc++;
		}
		else {
			f[x.second.second] += cc - get2(ma[x.second.second]);
			f[x.second.second] %= 2;
		}
	}
	long long res = 0;
	for (int i = 1; i <= n; i++) {
		if (f[i] == 0) {
			res += b[i].first;
		}
		else {
			res += b[i].second;
		}
	}
	cout << res;
}

// END MAIN CODE

Compilation message

fortune_telling2.cpp: In function 'void file_io(std::string, std::string)':
fortune_telling2.cpp:25:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |   freopen(fi.c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:28:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |   freopen(fo.c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 3 ms 3540 KB Output is correct
3 Correct 3 ms 3540 KB Output is correct
4 Correct 3 ms 3540 KB Output is correct
5 Correct 3 ms 3540 KB Output is correct
6 Correct 2 ms 3540 KB Output is correct
7 Correct 3 ms 3540 KB Output is correct
8 Correct 2 ms 3540 KB Output is correct
9 Correct 2 ms 3536 KB Output is correct
10 Correct 3 ms 3540 KB Output is correct
11 Correct 3 ms 3540 KB Output is correct
12 Correct 3 ms 3540 KB Output is correct
13 Correct 3 ms 3540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 3 ms 3540 KB Output is correct
3 Correct 3 ms 3540 KB Output is correct
4 Correct 3 ms 3540 KB Output is correct
5 Correct 3 ms 3540 KB Output is correct
6 Correct 2 ms 3540 KB Output is correct
7 Correct 3 ms 3540 KB Output is correct
8 Correct 2 ms 3540 KB Output is correct
9 Correct 2 ms 3536 KB Output is correct
10 Correct 3 ms 3540 KB Output is correct
11 Correct 3 ms 3540 KB Output is correct
12 Correct 3 ms 3540 KB Output is correct
13 Correct 3 ms 3540 KB Output is correct
14 Correct 13 ms 4556 KB Output is correct
15 Correct 24 ms 5672 KB Output is correct
16 Correct 34 ms 6592 KB Output is correct
17 Correct 47 ms 7716 KB Output is correct
18 Correct 48 ms 7672 KB Output is correct
19 Correct 48 ms 7668 KB Output is correct
20 Correct 47 ms 7656 KB Output is correct
21 Correct 44 ms 7612 KB Output is correct
22 Correct 39 ms 7260 KB Output is correct
23 Correct 41 ms 7204 KB Output is correct
24 Correct 43 ms 7196 KB Output is correct
25 Correct 39 ms 7204 KB Output is correct
26 Correct 41 ms 7224 KB Output is correct
27 Correct 47 ms 7732 KB Output is correct
28 Correct 44 ms 7648 KB Output is correct
29 Correct 56 ms 7656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3412 KB Output is correct
2 Correct 3 ms 3540 KB Output is correct
3 Correct 3 ms 3540 KB Output is correct
4 Correct 3 ms 3540 KB Output is correct
5 Correct 3 ms 3540 KB Output is correct
6 Correct 2 ms 3540 KB Output is correct
7 Correct 3 ms 3540 KB Output is correct
8 Correct 2 ms 3540 KB Output is correct
9 Correct 2 ms 3536 KB Output is correct
10 Correct 3 ms 3540 KB Output is correct
11 Correct 3 ms 3540 KB Output is correct
12 Correct 3 ms 3540 KB Output is correct
13 Correct 3 ms 3540 KB Output is correct
14 Correct 13 ms 4556 KB Output is correct
15 Correct 24 ms 5672 KB Output is correct
16 Correct 34 ms 6592 KB Output is correct
17 Correct 47 ms 7716 KB Output is correct
18 Correct 48 ms 7672 KB Output is correct
19 Correct 48 ms 7668 KB Output is correct
20 Correct 47 ms 7656 KB Output is correct
21 Correct 44 ms 7612 KB Output is correct
22 Correct 39 ms 7260 KB Output is correct
23 Correct 41 ms 7204 KB Output is correct
24 Correct 43 ms 7196 KB Output is correct
25 Correct 39 ms 7204 KB Output is correct
26 Correct 41 ms 7224 KB Output is correct
27 Correct 47 ms 7732 KB Output is correct
28 Correct 44 ms 7648 KB Output is correct
29 Correct 56 ms 7656 KB Output is correct
30 Correct 93 ms 9540 KB Output is correct
31 Correct 128 ms 12508 KB Output is correct
32 Correct 162 ms 16364 KB Output is correct
33 Correct 244 ms 24192 KB Output is correct
34 Correct 84 ms 8632 KB Output is correct
35 Correct 242 ms 24248 KB Output is correct
36 Correct 251 ms 24196 KB Output is correct
37 Correct 247 ms 24192 KB Output is correct
38 Correct 239 ms 24216 KB Output is correct
39 Correct 241 ms 24348 KB Output is correct
40 Correct 234 ms 24056 KB Output is correct
41 Correct 237 ms 24216 KB Output is correct
42 Correct 242 ms 24316 KB Output is correct
43 Correct 201 ms 23540 KB Output is correct
44 Correct 190 ms 23484 KB Output is correct
45 Correct 195 ms 23460 KB Output is correct
46 Correct 211 ms 22288 KB Output is correct
47 Correct 209 ms 22172 KB Output is correct
48 Correct 233 ms 24264 KB Output is correct
49 Correct 233 ms 24296 KB Output is correct