Submission #364405

# Submission time Handle Problem Language Result Execution time Memory
364405 2021-02-09T05:31:59 Z Kevin_Zhang_TW Fortune Telling 2 (JOI14_fortune_telling2) C++17
35 / 100
3000 ms 15852 KB
//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) {  cerr << a << ' ', kout(b...); }
template<class T> void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L) == R], ++L; }
#else
#define DE(...) 0
#define debug(...) 0
#endif

const int MAX_N = 200010, SQ = 600;

int a[MAX_N], b[MAX_N], mt[MAX_N], n, m;

struct ASK {
	int a, b, c, d;
}qry[MAX_N];
bool operator < (ASK a, ASK b) {
	int l1 = a.c, r1 = a.d;
	int l2 = b.c, r2 = b.d;
	int aid = l1 / SQ, bid = l2 / SQ;
	if (aid != bid) return aid < bid;
	return (r1 < r2) ^ (aid & 1);
}
pair<int,int> lisan[MAX_N];

struct DS {
	int sum[SQ], cnt[MAX_N], all, n;
	void add(int i, int d) {
		sum[i / SQ] += d;
		cnt[i] += d;
		all += d;
	}
	int find_mx() {
		for (int i = n;i >= 0;--i) if (cnt[i]) return i;
		return -1;


		int id = n / SQ;
		int RB = id * SQ;
		for (int i = n;i >= RB;--i)
			if (cnt[i]) return i;
		for (int i = id - 1;i >= 0;--i)
			if (sum[i]) for (int j = (i * SQ) - 1;;--j)
				if (cnt[j]) return j;
		return -1;
	}
	int getsum(int l, int r) {
		{
			int res = 0;
			for (int i = l;i <= r;++i) res += cnt[i];
			return res;
		}
		assert(r == n);
		int res = all;
		--l;
		int lid = l / SQ;
		for (int i = 0;i < lid;++i) res -= sum[i];
		for (int i = lid * SQ;i <= l;++i) res -= cnt[i];
		return res;
	}
}RMQ[3];

int sum[MAX_N];
array<int, 3> getnxt() {
	int p = RMQ[1].find_mx();
	int v = RMQ[2].getsum(p + 1, m) & 1;
	DE(p, v);
	if (p != -1) {
		if (v == 1) return {-1, 1, 1};
		else return {-1, 2, 2};
	}
	if (v == 1) return {-1, 2, 1};
	else return {-1, 1, 2};
}
ll solveall() {
	sort(qry, qry + n);
	ll res = 0;

	RMQ[1].n = RMQ[2].n = m;

	auto addin = [&](int pos, int d) {
		int rp = lisan[pos].second;
		if (sum[pos]) RMQ[ sum[pos] ].add(rp, -1);
		sum[pos] += d;
		if (sum[pos]) RMQ[ sum[pos] ].add(rp, 1);
		assert(sum[pos] >= 0);

		DE(pos, sum[pos], rp);
		//tree.modi(lisan[pos].second, sum[pos]);
	};
	int cl = m, cr = m;

	for (int i = 0;i < n;++i) {
		auto [a, b, l, r] = qry[i];
		while (cl > l) addin(--cl, 1);
		while (cr > r) addin(--cr, 1);
		while (cr < r) addin(cr++,-1);
		while (cl < l) addin(cl++,-1);
		auto ret = getnxt();
		DE(ret[1], ret[2]);
		ll ans ;
		if (a <= b) ans = ret[1] == 1 ? min(a, b) : max(a, b);
		else ans = ret[2] == 1 ? min(a, b) : max(a, b);
		res += ans;
	}
	return res;
}
int32_t main() {
	cin.tie(0)->sync_with_stdio(0);
	cin >> n >> m;
	for (int i = 0;i < n;++i)
		cin >> a[i] >> b[i];
	for (int i = 0;i < m;++i) {
		cin >> mt[i];
		lisan[i] = {mt[i], i};
	}
	sort(lisan, lisan + m);
	for (int i = 0;i < n;++i) {
		int c = a[i], d = b[i];
		if (c > d) swap(c, d);
		c = lower_bound(lisan, lisan + m, make_pair(c, -1)) - lisan;
		d = lower_bound(lisan, lisan + m, make_pair(d, -1)) - lisan;
		qry[i] = {a[i], b[i], c, d};
	}
	cout << solveall() << '\n';
}

Compilation message

fortune_telling2.cpp: In function 'std::array<int, 3> getnxt()':
fortune_telling2.cpp:15:17: warning: statement has no effect [-Wunused-value]
   15 | #define DE(...) 0
      |                 ^
fortune_telling2.cpp:76:2: note: in expansion of macro 'DE'
   76 |  DE(p, v);
      |  ^~
fortune_telling2.cpp: In lambda function:
fortune_telling2.cpp:15:17: warning: statement has no effect [-Wunused-value]
   15 | #define DE(...) 0
      |                 ^
fortune_telling2.cpp:97:3: note: in expansion of macro 'DE'
   97 |   DE(pos, sum[pos], rp);
      |   ^~
fortune_telling2.cpp: In function 'll solveall()':
fortune_telling2.cpp:15:17: warning: statement has no effect [-Wunused-value]
   15 | #define DE(...) 0
      |                 ^
fortune_telling2.cpp:109:3: note: in expansion of macro 'DE'
  109 |   DE(ret[1], ret[2]);
      |   ^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 492 KB Output is correct
6 Correct 2 ms 492 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 2 ms 492 KB Output is correct
12 Correct 2 ms 492 KB Output is correct
13 Correct 2 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 492 KB Output is correct
6 Correct 2 ms 492 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 2 ms 492 KB Output is correct
12 Correct 2 ms 492 KB Output is correct
13 Correct 2 ms 492 KB Output is correct
14 Correct 20 ms 876 KB Output is correct
15 Correct 44 ms 1260 KB Output is correct
16 Correct 68 ms 1920 KB Output is correct
17 Correct 93 ms 2284 KB Output is correct
18 Correct 95 ms 2284 KB Output is correct
19 Correct 84 ms 2284 KB Output is correct
20 Correct 86 ms 2284 KB Output is correct
21 Correct 78 ms 2284 KB Output is correct
22 Correct 24 ms 2284 KB Output is correct
23 Correct 25 ms 2412 KB Output is correct
24 Correct 25 ms 2284 KB Output is correct
25 Correct 24 ms 2284 KB Output is correct
26 Correct 677 ms 2156 KB Output is correct
27 Correct 835 ms 2412 KB Output is correct
28 Correct 839 ms 2412 KB Output is correct
29 Correct 1460 ms 2412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 492 KB Output is correct
5 Correct 2 ms 492 KB Output is correct
6 Correct 2 ms 492 KB Output is correct
7 Correct 2 ms 492 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 2 ms 492 KB Output is correct
10 Correct 2 ms 492 KB Output is correct
11 Correct 2 ms 492 KB Output is correct
12 Correct 2 ms 492 KB Output is correct
13 Correct 2 ms 492 KB Output is correct
14 Correct 20 ms 876 KB Output is correct
15 Correct 44 ms 1260 KB Output is correct
16 Correct 68 ms 1920 KB Output is correct
17 Correct 93 ms 2284 KB Output is correct
18 Correct 95 ms 2284 KB Output is correct
19 Correct 84 ms 2284 KB Output is correct
20 Correct 86 ms 2284 KB Output is correct
21 Correct 78 ms 2284 KB Output is correct
22 Correct 24 ms 2284 KB Output is correct
23 Correct 25 ms 2412 KB Output is correct
24 Correct 25 ms 2284 KB Output is correct
25 Correct 24 ms 2284 KB Output is correct
26 Correct 677 ms 2156 KB Output is correct
27 Correct 835 ms 2412 KB Output is correct
28 Correct 839 ms 2412 KB Output is correct
29 Correct 1460 ms 2412 KB Output is correct
30 Correct 376 ms 5228 KB Output is correct
31 Correct 467 ms 9324 KB Output is correct
32 Correct 572 ms 11372 KB Output is correct
33 Correct 783 ms 15760 KB Output is correct
34 Correct 46 ms 7020 KB Output is correct
35 Correct 788 ms 15724 KB Output is correct
36 Correct 821 ms 15724 KB Output is correct
37 Correct 783 ms 15768 KB Output is correct
38 Correct 518 ms 15852 KB Output is correct
39 Correct 533 ms 15596 KB Output is correct
40 Correct 429 ms 15616 KB Output is correct
41 Correct 204 ms 15596 KB Output is correct
42 Correct 429 ms 15596 KB Output is correct
43 Correct 112 ms 14288 KB Output is correct
44 Correct 113 ms 14316 KB Output is correct
45 Correct 112 ms 14572 KB Output is correct
46 Correct 133 ms 13676 KB Output is correct
47 Correct 497 ms 13548 KB Output is correct
48 Execution timed out 3073 ms 15844 KB Time limit exceeded
49 Halted 0 ms 0 KB -