Submission #250280

# Submission time Handle Problem Language Result Execution time Memory
250280 2020-07-17T10:34:46 Z mode149256 Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
851 ms 136312 KB
/*input
5 1
9 1
8 8
3 7
4 6
4 2
3
*/
#include <bits/stdc++.h>
using namespace std;

namespace my_template {
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef vector<pi> vpi;
typedef vector<vpi> vpii;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef std::string str;
typedef std::vector<str> vs;

#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"

const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L;

template<typename T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
template<typename T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
template<typename T>
T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
template<typename T>
T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }

template<typename T>
void print(vector<T> vec, string name = "") {
	cout << name;
	for (auto u : vec)
		cout << u << ' ';
	cout << '\n';
}
}
using namespace my_template;

const int MOD = 1000000007;
const ll INF = std::numeric_limits<ll>::max();
const int MX = 100101;

struct FENWICK {
	vi sk;
	int N;
	FENWICK(int n) : N(n) {
		sk.resize(N + 1, 0);
	}

	void add(int i, int val) {
		for (; i <= N; i += (i) & (-i))
			sk[i] += val;
	}

	int get(int i) {
		int ret = 0;
		for (; i > 0; i -= (i) & (-i))
			ret += sk[i];
		return ret;
	}
};

struct node {
	int l, r;
	int did;
	node *left = nullptr;
	node *right = nullptr;
	node(int a, int b): l(a), r(b), did(-1) { }
	~node() {
		if (left) {
			delete left;
			left = nullptr;
		}
		if (right) {
			delete right;
			right = nullptr;
		}
	}
	inline void add_left() { if (!left) left = new node(l, (l + r) / 2); }
	inline void add_right() { if (!right) right = new node((l + r) / 2 + 1, r); }

	void add(int pos, int val) {
		if (l == r) {
			did = max(did, val);
			return;
		}

		if (pos <= (l + r) / 2) {
			add_left(); left->add(pos, val);
		} else {
			add_right(); right->add(pos, val);
		}

		did = max((left ? left->did : -1), (right ? right->did : -1));
	}

	int get(int a, int b) {
		if (r < a or b < l) return -1;
		else if (a <= l and r <= b) return did;
		else if (r <= (l + r) / 2) {
			if (right) {
				delete right;
				right = nullptr;
			}
			return (left ? left->get(a, b) : -1);
		} else if ((l + r) / 2 + 1 <= l) {
			return (right ? right->get(a, b) : -1);
		} else  {
			return max((left ? left->get(a, b) : -1), (right ? right->get(a, b) : -1));
		}
	}
};

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int N, K;
	cin >> N >> K;
	vpi sk(N);
	for (int i = 0; i < N; ++i)
	{
		cin >> sk[i].x >> sk[i].y;
	}

	node inds(1, (int)1e9);
	vpi suInd(K);
	for (int i = 0; i < K; ++i) {
		int a;
		cin >> a;
		inds.add(a, i);
		suInd[i] = {a, i};
	}

	sort(suInd.rbegin(), suInd.rend());
	sort(sk.begin(), sk.end(), [](const pi & a, const pi & b) {
		return max(a.x, a.y) > max(b.x, b.y);
	});

	FENWICK fen(K);

	ll ats = 0;
	int j = 0;
	for (int i = 0; i < N; ++i)
	{
		int a = max(sk[i].x, sk[i].y);
		int b = min(sk[i].x, sk[i].y);
		int k = inds.get(b, a - 1);

		while (j < (int)suInd.size() and suInd[j].x >= a) {
			fen.add(suInd[j].y + 1, 1);
			j++;
		}

		int kiek;
		if (k == -1) {
			kiek = fen.get(K);
			if (kiek & 1) ats += (ll)sk[i].y;
			else ats += (ll)sk[i].x;
		} else {
			kiek = fen.get(K) - fen.get(k + 1);
			if (kiek & 1) ats += (ll)b;
			else ats += (ll)a;
		}
		// printf("i = %d, a = %d, b = %d, k = %d, kiek = %d\n", i, a, b, k, kiek);
	}

	printf("%lld\n", ats);
}

/* Look for:
* special cases (n=1?)
* overflow (ll vs int?)
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* array bounds
*/
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1280 KB Output is correct
2 Correct 3 ms 1280 KB Output is correct
3 Correct 3 ms 1408 KB Output is correct
4 Correct 3 ms 1408 KB Output is correct
5 Correct 3 ms 1408 KB Output is correct
6 Correct 3 ms 1408 KB Output is correct
7 Correct 3 ms 1408 KB Output is correct
8 Correct 3 ms 1280 KB Output is correct
9 Correct 3 ms 1280 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 3 ms 1280 KB Output is correct
12 Correct 3 ms 1280 KB Output is correct
13 Correct 3 ms 1408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1280 KB Output is correct
2 Correct 3 ms 1280 KB Output is correct
3 Correct 3 ms 1408 KB Output is correct
4 Correct 3 ms 1408 KB Output is correct
5 Correct 3 ms 1408 KB Output is correct
6 Correct 3 ms 1408 KB Output is correct
7 Correct 3 ms 1408 KB Output is correct
8 Correct 3 ms 1280 KB Output is correct
9 Correct 3 ms 1280 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 3 ms 1280 KB Output is correct
12 Correct 3 ms 1280 KB Output is correct
13 Correct 3 ms 1408 KB Output is correct
14 Correct 33 ms 8832 KB Output is correct
15 Correct 75 ms 16504 KB Output is correct
16 Correct 113 ms 23672 KB Output is correct
17 Correct 152 ms 30712 KB Output is correct
18 Correct 135 ms 30740 KB Output is correct
19 Correct 154 ms 30712 KB Output is correct
20 Correct 146 ms 30712 KB Output is correct
21 Correct 137 ms 30840 KB Output is correct
22 Correct 61 ms 5496 KB Output is correct
23 Correct 65 ms 4856 KB Output is correct
24 Correct 68 ms 4344 KB Output is correct
25 Correct 67 ms 6648 KB Output is correct
26 Correct 121 ms 30328 KB Output is correct
27 Correct 120 ms 30712 KB Output is correct
28 Correct 121 ms 29560 KB Output is correct
29 Correct 132 ms 30712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1280 KB Output is correct
2 Correct 3 ms 1280 KB Output is correct
3 Correct 3 ms 1408 KB Output is correct
4 Correct 3 ms 1408 KB Output is correct
5 Correct 3 ms 1408 KB Output is correct
6 Correct 3 ms 1408 KB Output is correct
7 Correct 3 ms 1408 KB Output is correct
8 Correct 3 ms 1280 KB Output is correct
9 Correct 3 ms 1280 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 3 ms 1280 KB Output is correct
12 Correct 3 ms 1280 KB Output is correct
13 Correct 3 ms 1408 KB Output is correct
14 Correct 33 ms 8832 KB Output is correct
15 Correct 75 ms 16504 KB Output is correct
16 Correct 113 ms 23672 KB Output is correct
17 Correct 152 ms 30712 KB Output is correct
18 Correct 135 ms 30740 KB Output is correct
19 Correct 154 ms 30712 KB Output is correct
20 Correct 146 ms 30712 KB Output is correct
21 Correct 137 ms 30840 KB Output is correct
22 Correct 61 ms 5496 KB Output is correct
23 Correct 65 ms 4856 KB Output is correct
24 Correct 68 ms 4344 KB Output is correct
25 Correct 67 ms 6648 KB Output is correct
26 Correct 121 ms 30328 KB Output is correct
27 Correct 120 ms 30712 KB Output is correct
28 Correct 121 ms 29560 KB Output is correct
29 Correct 132 ms 30712 KB Output is correct
30 Correct 602 ms 128916 KB Output is correct
31 Correct 602 ms 129272 KB Output is correct
32 Correct 704 ms 133624 KB Output is correct
33 Correct 821 ms 136212 KB Output is correct
34 Correct 458 ms 130936 KB Output is correct
35 Correct 851 ms 136184 KB Output is correct
36 Correct 781 ms 136312 KB Output is correct
37 Correct 781 ms 136184 KB Output is correct
38 Correct 744 ms 136312 KB Output is correct
39 Correct 730 ms 136312 KB Output is correct
40 Correct 675 ms 135928 KB Output is correct
41 Correct 723 ms 136312 KB Output is correct
42 Correct 695 ms 136184 KB Output is correct
43 Correct 619 ms 132664 KB Output is correct
44 Correct 580 ms 126072 KB Output is correct
45 Correct 541 ms 113656 KB Output is correct
46 Correct 396 ms 27136 KB Output is correct
47 Correct 336 ms 22904 KB Output is correct
48 Correct 608 ms 134776 KB Output is correct
49 Correct 595 ms 130936 KB Output is correct