Submission #364409

# Submission time Handle Problem Language Result Execution time Memory
364409 2021-02-09T05:33:55 Z Kevin_Zhang_TW Fortune Telling 2 (JOI14_fortune_telling2) C++17
100 / 100
871 ms 15628 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() {
		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+1) * SQ) - 1;;--j)
				if (cnt[j]) return j;
		return -1;
	}
	int getsum(int l, int r) {
		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);

	};
	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:67:2: note: in expansion of macro 'DE'
   67 |  DE(p, v);
      |  ^~
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:97:3: note: in expansion of macro 'DE'
   97 |   DE(ret[1], ret[2]);
      |   ^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 2 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 2 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 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 364 KB Output is correct
2 Correct 2 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 2 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 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 25 ms 876 KB Output is correct
15 Correct 46 ms 1260 KB Output is correct
16 Correct 78 ms 1884 KB Output is correct
17 Correct 103 ms 2284 KB Output is correct
18 Correct 106 ms 2248 KB Output is correct
19 Correct 95 ms 2284 KB Output is correct
20 Correct 95 ms 2284 KB Output is correct
21 Correct 89 ms 2284 KB Output is correct
22 Correct 34 ms 2284 KB Output is correct
23 Correct 42 ms 2412 KB Output is correct
24 Correct 35 ms 2284 KB Output is correct
25 Correct 44 ms 2284 KB Output is correct
26 Correct 48 ms 2156 KB Output is correct
27 Correct 57 ms 2284 KB Output is correct
28 Correct 56 ms 2388 KB Output is correct
29 Correct 51 ms 2284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 2 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 2 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 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 25 ms 876 KB Output is correct
15 Correct 46 ms 1260 KB Output is correct
16 Correct 78 ms 1884 KB Output is correct
17 Correct 103 ms 2284 KB Output is correct
18 Correct 106 ms 2248 KB Output is correct
19 Correct 95 ms 2284 KB Output is correct
20 Correct 95 ms 2284 KB Output is correct
21 Correct 89 ms 2284 KB Output is correct
22 Correct 34 ms 2284 KB Output is correct
23 Correct 42 ms 2412 KB Output is correct
24 Correct 35 ms 2284 KB Output is correct
25 Correct 44 ms 2284 KB Output is correct
26 Correct 48 ms 2156 KB Output is correct
27 Correct 57 ms 2284 KB Output is correct
28 Correct 56 ms 2388 KB Output is correct
29 Correct 51 ms 2284 KB Output is correct
30 Correct 389 ms 5356 KB Output is correct
31 Correct 495 ms 6380 KB Output is correct
32 Correct 624 ms 7532 KB Output is correct
33 Correct 860 ms 9836 KB Output is correct
34 Correct 44 ms 5100 KB Output is correct
35 Correct 861 ms 9964 KB Output is correct
36 Correct 871 ms 9800 KB Output is correct
37 Correct 863 ms 9796 KB Output is correct
38 Correct 604 ms 9708 KB Output is correct
39 Correct 590 ms 9836 KB Output is correct
40 Correct 500 ms 9708 KB Output is correct
41 Correct 254 ms 9708 KB Output is correct
42 Correct 502 ms 9772 KB Output is correct
43 Correct 169 ms 9068 KB Output is correct
44 Correct 166 ms 9196 KB Output is correct
45 Correct 166 ms 9452 KB Output is correct
46 Correct 192 ms 9836 KB Output is correct
47 Correct 266 ms 9708 KB Output is correct
48 Correct 300 ms 9708 KB Output is correct
49 Correct 299 ms 15628 KB Output is correct