Submission #233690

# Submission time Handle Problem Language Result Execution time Memory
233690 2020-05-21T11:18:08 Z almogwald Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
566 ms 29828 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#define forr(i,a,b,c) for(int i=a;i<b;i+=c)
#define fort(i,a,b) forr(i,a,b,1)
#define forn(i,n) fort(i,1,n)
#define fori(i,n) fort(i,0,n)
#define forrb(i,a,b,c) for(int i=b-1;i>=a;i-=c)
#define fortb(i,a,b) forrb(i,a,b,1)
#define fornb(i,n) fortb(i,1,n)
#define forib(i,n) fortb(i,0,n)
using namespace std;
#define into(x) cin >> x;
typedef vector<int> veci;
typedef long long lol;
typedef pair<lol,lol> point;
#define def(x) lol x; into(x)

#define logn 20
int main() {
	ios::sync_with_stdio();
	def(n) def(m)
	vector<point> x,y;
	fori(i, n) {
		def(a) def(b)
			x.push_back({ a,b });
	}
	fori(i, m) {
		def(a)
			y.push_back({ a,i });
	}
	sort(y.begin(), y.end());
	vector<veci> arr(logn, veci(m+1));
	
	veci xx(m);
	set<point> r;
	forib(i, m) {
		int ii = y[i].second+1;
		int sum = 0;
		fori(j, logn) {
			if (ii % 2 == 1) {
				sum += arr[j][ii];
				ii++;
			}
			ii /= 2;
		}
		xx[i] = sum;
		ii = y[i].second;
		fori(j, logn) {
			arr[j][ii]++;
			ii /= 2;
		}
	}
	fori(i, m) {
		arr[0][i] = y[i].second;
	}
	forn(j, logn) {
		fori(i, (m / 2) + 1) {
			arr[j][i] = max(arr[j - 1][2 * i], arr[j - 1][2 * i + 1]);
		}
	}
	veci op(m);
	fori(i, m) {
		op[y[i].second] = i;
	}
	lol sum = 0;
	fori(i, n) {
		int low = 0, high = m;
		int small = min(x[i].first, x[i].second);
		while (low < high) {
			int mid = (low + high) / 2;
			if (y[mid].first < small) {
				low = mid + 1;
			}
			else {
				high = mid;
			}
		}
		int lower = low;
		low = -1, high = m - 1;
		int big = max(x[i].first, x[i].second);
		while (low < high) {
			int mid = (low + high+1) / 2;
			if (y[mid].first >= big) {
				high = mid - 1;
			}
			else {
				low = mid;
			}
		}
		int higer = high;
		int best = -1;
		fori(j, logn) {
			if (lower > higer) {
				break;
			}
			if (lower % 2 == 1) {
				best = max(best,arr[j][lower]);
				lower++;
			}
			if (higer % 2 == 0) {
				best = max(best, arr[j][higer]);
				higer--;
				if (higer < 0) {
					break;
				}
			}
			lower /= 2;
			higer /= 2;
		}
		if (best == -1) {
			int num = m - lower;
			if (num % 2 == 1) {
				sum += x[i].second;
			}
			else {
				sum += x[i].first;
			}
		}
		else {
			int num = xx[op[best]];
			if (num % 2 == 1) {
				sum += small;
			}
			else {
				sum += big;
			}
		}
	}
	cout << sum << endl;
	return 0 ;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 6 ms 512 KB Output is correct
3 Correct 7 ms 384 KB Output is correct
4 Correct 7 ms 384 KB Output is correct
5 Correct 7 ms 384 KB Output is correct
6 Correct 7 ms 384 KB Output is correct
7 Correct 7 ms 384 KB Output is correct
8 Correct 7 ms 384 KB Output is correct
9 Correct 6 ms 384 KB Output is correct
10 Correct 6 ms 384 KB Output is correct
11 Correct 7 ms 384 KB Output is correct
12 Correct 7 ms 512 KB Output is correct
13 Correct 7 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 6 ms 512 KB Output is correct
3 Correct 7 ms 384 KB Output is correct
4 Correct 7 ms 384 KB Output is correct
5 Correct 7 ms 384 KB Output is correct
6 Correct 7 ms 384 KB Output is correct
7 Correct 7 ms 384 KB Output is correct
8 Correct 7 ms 384 KB Output is correct
9 Correct 6 ms 384 KB Output is correct
10 Correct 6 ms 384 KB Output is correct
11 Correct 7 ms 384 KB Output is correct
12 Correct 7 ms 512 KB Output is correct
13 Correct 7 ms 512 KB Output is correct
14 Correct 29 ms 1788 KB Output is correct
15 Correct 54 ms 3312 KB Output is correct
16 Correct 83 ms 4820 KB Output is correct
17 Correct 118 ms 6120 KB Output is correct
18 Correct 106 ms 6120 KB Output is correct
19 Correct 112 ms 6248 KB Output is correct
20 Correct 106 ms 6244 KB Output is correct
21 Correct 103 ms 6228 KB Output is correct
22 Correct 76 ms 5672 KB Output is correct
23 Correct 75 ms 5740 KB Output is correct
24 Correct 76 ms 5896 KB Output is correct
25 Correct 76 ms 5740 KB Output is correct
26 Correct 99 ms 6036 KB Output is correct
27 Correct 103 ms 6124 KB Output is correct
28 Correct 101 ms 6248 KB Output is correct
29 Correct 102 ms 6376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 6 ms 512 KB Output is correct
3 Correct 7 ms 384 KB Output is correct
4 Correct 7 ms 384 KB Output is correct
5 Correct 7 ms 384 KB Output is correct
6 Correct 7 ms 384 KB Output is correct
7 Correct 7 ms 384 KB Output is correct
8 Correct 7 ms 384 KB Output is correct
9 Correct 6 ms 384 KB Output is correct
10 Correct 6 ms 384 KB Output is correct
11 Correct 7 ms 384 KB Output is correct
12 Correct 7 ms 512 KB Output is correct
13 Correct 7 ms 512 KB Output is correct
14 Correct 29 ms 1788 KB Output is correct
15 Correct 54 ms 3312 KB Output is correct
16 Correct 83 ms 4820 KB Output is correct
17 Correct 118 ms 6120 KB Output is correct
18 Correct 106 ms 6120 KB Output is correct
19 Correct 112 ms 6248 KB Output is correct
20 Correct 106 ms 6244 KB Output is correct
21 Correct 103 ms 6228 KB Output is correct
22 Correct 76 ms 5672 KB Output is correct
23 Correct 75 ms 5740 KB Output is correct
24 Correct 76 ms 5896 KB Output is correct
25 Correct 76 ms 5740 KB Output is correct
26 Correct 99 ms 6036 KB Output is correct
27 Correct 103 ms 6124 KB Output is correct
28 Correct 101 ms 6248 KB Output is correct
29 Correct 102 ms 6376 KB Output is correct
30 Correct 213 ms 23008 KB Output is correct
31 Correct 285 ms 24348 KB Output is correct
32 Correct 376 ms 26308 KB Output is correct
33 Correct 564 ms 29604 KB Output is correct
34 Correct 192 ms 22624 KB Output is correct
35 Correct 551 ms 29720 KB Output is correct
36 Correct 547 ms 29652 KB Output is correct
37 Correct 553 ms 29780 KB Output is correct
38 Correct 552 ms 29780 KB Output is correct
39 Correct 566 ms 29780 KB Output is correct
40 Correct 520 ms 29828 KB Output is correct
41 Correct 546 ms 29652 KB Output is correct
42 Correct 563 ms 29752 KB Output is correct
43 Correct 424 ms 29132 KB Output is correct
44 Correct 422 ms 29016 KB Output is correct
45 Correct 415 ms 29032 KB Output is correct
46 Correct 385 ms 27736 KB Output is correct
47 Correct 373 ms 27936 KB Output is correct
48 Correct 515 ms 29820 KB Output is correct
49 Correct 499 ms 29652 KB Output is correct