Submission #714928

# Submission time Handle Problem Language Result Execution time Memory
714928 2023-03-25T13:49:48 Z MISM06 Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
335 ms 23652 KB
//0 1 1 0 1
//0 1 0 0 1
//1 0 0 1 1
//0 1 1 0 1
//uoy kcuf;
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx2")

using namespace std;

#define F 			first
#define S 			second
#define pb 			push_back
#define sze			size()
#define	all(x)		x.begin() , x.end()
#define wall__		cout << "--------------------------------------\n";
#define node		int mid = (tl + tr) >> 1, cl = v << 1, cr = v << 1 | 1
#define file_io		freopen("input.cpp", "r", stdin); freopen("output.cpp", "w", stdout);

typedef long long ll;
typedef long double dl;
typedef pair < int , int > pii;
typedef pair < int , ll > pil;
typedef pair < ll , int > pli;
typedef pair < ll , ll > pll;
typedef pair < int , pii > piii;
typedef pair < ll, pll > plll;


const ll N = 2e5 + 10;
const ll mod = 1e9 + 7;
const ll inf = 2e16;
const ll rinf = -2e16;
const ll INF = 1e9 + 10;
const ll rINF = -1e9 - 10;
const ll lg = 32;

int n, k, a[N], b[N], c[N];
pii par[N];
vector < pii > kid[N];
vector < pii > ord;

int mx[N << 2];
void build (int v, int tl, int tr) {
	if (tl == tr) {
		mx[v] = ord[tl].S;
		return;
	} node;
	build(cl, tl, mid);
	build(cr, mid + 1, tr);
	mx[v] = max(mx[cl], mx[cr]);
}
int find_mx (int l, int r, int v, int tl, int tr) {
	if (l > r) return 0;
	if (l == tl && r == tr) return mx[v];
	node;
	return max(find_mx(l, min(r, mid), cl, tl, mid), find_mx(max(l, mid + 1), r, cr, mid + 1, tr)); 
}

int sit[N], la[N << 2];
void shift (int v, int tl, int tr) {
	if (la[v] == 0) return;
	if (tl != tr) {
		la[v << 1] ^= 1;
		la[v << 1 | 1] ^= 1;
	} else sit[tl] ^= 1;
	la[v] = 0;
}
void upd (int l, int r, int v, int tl, int tr) {
	shift(v, tl, tr);
	if (l > r) return;
	if (l == tl && r == tr) {
		la[v] ^= 1;
		shift(v, tl, tr);
		return;
	} node;
	upd (l, min(r, mid), cl, tl, mid);
	upd (max(l, mid + 1), r ,cr, mid + 1, tr);
}
int pos;
int ask (int v, int tl, int tr) {
	shift(v, tl, tr);
	if (tl == tr) return sit[tl];
	node;
	if (pos <= mid) return ask(cl, tl, mid);
	else return ask(cr, mid + 1, tr);
}

void solve () {

	cin >> n >> k;
	vector < piii > g; g.pb({-1, {-1, -1}});
	for (int i = 1; i <= n; i++) {
		cin >> a[i] >> b[i];
		g.pb({max(a[i] , b[i]), {a[i], b[i]}});
	}
	sort(all(g));
	// for (auto e : g) cout << e.F << "," << e.S.F <<"," << e.S.S << " | ";
	// cout << '\n'; wall__
	for (int i = 1; i <= n; i++) {
		a[i] = g[i].S.F; b[i] = g[i].S.S;
	}
	// for (int i = 1; i <= n; i++) cout << a[i] << " , " << b[i] << " | ";
	// cout << '\n'; wall__

	for (int i = 1; i <= k; i++) {
		cin >> c[i];
		ord.pb({c[i], i});
	}
	ord.pb({-1, -1});
	sort(all(ord));
	// for (int i = 1; i <= k; i++) cout << ord[i].F << "," << ord[i].S << " | ";
	// cout << '\n'; wall__
	build(1, 1, k);
	// cout << "start of find kids : \n";
	for (int i = 1; i <= n; i++) {
		// cout << i << " : \n";
		if (a[i] == b[i]) {
			// cout << "do nothing\n"; wall__
			continue;
		}
		int x = a[i], y = b[i]; if (x > y) swap(x, y);
		// cout << a[i] << " , " << b[i] << " -- > " << x << " , " << y << '\n';
		int t = 0;
		if (a[i] < b[i]) t = 1;
		// cout << "t = " << t << '\n';
		pii X = {x, 0}, Y = {y, 0}; int l = -1, r = k + 1;
		if (ord[k].F >= x) l = lower_bound(all(ord), X) - ord.begin();
		if (ord[k].F >= y) r = lower_bound(all(ord), Y) - ord.begin();
		--r; 
		// cout << l << " , " << r << '\n';
		if (l == -1 || r == 0 || l > r) {
			// cout << "do nothing\n"; wall__
			continue;
		}
		int p = find_mx(l, r, 1, 1, k);
		// cout << "p = " << p << '\n';
		par[i] = {p, t};
		kid[p].pb({i, t});
		// wall__
	}
	// cout << "end of find kids\n";
	// wall__
	// cout << "kids : \n";
	// for (int i = 1; i <= k; i++) {
	// 	cout << i << " : ";
	// 	for (auto e : kid[i]) cout << e.F << "," << e.S << " | ";
	// 	cout << '\n';
	// }
	// wall__
	// cout << "start of FUCK!\n";
	// wall__
	for (int i = 1; i <= k; i++) {
		// cout << i << " , " << c[i] << " : \n";
		piii CC = {c[i] + 1, {0, 0}};
		auto it = lower_bound(all(g), CC);
		int id;
		if (it == g.end()) id = n;
		else {
			id = it - g.begin();
			--id;
		}
		// cout << "id = " << id << '\n';
		if (id > 0) upd(1, id, 1, 1, n);
		for (auto hah : kid[i]) {
			int v = hah.F, t= hah.S;
			// cout << v << "-" << t << " ; ";
			pos = v;
			int p = ask(1, 1, n);
			// cout << p << '\n';
			if (t != p) upd(v, v, 1, 1, n);
		}
		// wall__
	}
	// cout << "End of Fuck :(\n";
	// wall__
	ll ans = 0;
	for (int i = 1; i <= n; i++) {
		pos = i;
		int p = ask(1, 1, n);
		// cout << p;
		if (p == 1) ans += (1ll * b[i]);
		else ans += (1ll * a[i]);
	}
	// cout << '\n';
	// wall__
	cout << ans << '\n';

}

int main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	

	int t = 1;
	// cin >> t;
	while (t--) {solve();}

    return 0;
}
/*
*/
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5076 KB Output is correct
2 Correct 4 ms 5076 KB Output is correct
3 Correct 4 ms 5088 KB Output is correct
4 Correct 4 ms 5052 KB Output is correct
5 Correct 4 ms 5048 KB Output is correct
6 Correct 4 ms 5204 KB Output is correct
7 Correct 4 ms 5076 KB Output is correct
8 Correct 4 ms 5176 KB Output is correct
9 Correct 5 ms 5076 KB Output is correct
10 Correct 5 ms 5076 KB Output is correct
11 Correct 4 ms 5048 KB Output is correct
12 Correct 4 ms 5076 KB Output is correct
13 Correct 4 ms 5148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5076 KB Output is correct
2 Correct 4 ms 5076 KB Output is correct
3 Correct 4 ms 5088 KB Output is correct
4 Correct 4 ms 5052 KB Output is correct
5 Correct 4 ms 5048 KB Output is correct
6 Correct 4 ms 5204 KB Output is correct
7 Correct 4 ms 5076 KB Output is correct
8 Correct 4 ms 5176 KB Output is correct
9 Correct 5 ms 5076 KB Output is correct
10 Correct 5 ms 5076 KB Output is correct
11 Correct 4 ms 5048 KB Output is correct
12 Correct 4 ms 5076 KB Output is correct
13 Correct 4 ms 5148 KB Output is correct
14 Correct 16 ms 6164 KB Output is correct
15 Correct 31 ms 7324 KB Output is correct
16 Correct 45 ms 8052 KB Output is correct
17 Correct 61 ms 9452 KB Output is correct
18 Correct 61 ms 9448 KB Output is correct
19 Correct 52 ms 9548 KB Output is correct
20 Correct 58 ms 9436 KB Output is correct
21 Correct 45 ms 9768 KB Output is correct
22 Correct 42 ms 8900 KB Output is correct
23 Correct 47 ms 8996 KB Output is correct
24 Correct 49 ms 9012 KB Output is correct
25 Correct 41 ms 9324 KB Output is correct
26 Correct 47 ms 9124 KB Output is correct
27 Correct 49 ms 9464 KB Output is correct
28 Correct 44 ms 9492 KB Output is correct
29 Correct 46 ms 9388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5076 KB Output is correct
2 Correct 4 ms 5076 KB Output is correct
3 Correct 4 ms 5088 KB Output is correct
4 Correct 4 ms 5052 KB Output is correct
5 Correct 4 ms 5048 KB Output is correct
6 Correct 4 ms 5204 KB Output is correct
7 Correct 4 ms 5076 KB Output is correct
8 Correct 4 ms 5176 KB Output is correct
9 Correct 5 ms 5076 KB Output is correct
10 Correct 5 ms 5076 KB Output is correct
11 Correct 4 ms 5048 KB Output is correct
12 Correct 4 ms 5076 KB Output is correct
13 Correct 4 ms 5148 KB Output is correct
14 Correct 16 ms 6164 KB Output is correct
15 Correct 31 ms 7324 KB Output is correct
16 Correct 45 ms 8052 KB Output is correct
17 Correct 61 ms 9452 KB Output is correct
18 Correct 61 ms 9448 KB Output is correct
19 Correct 52 ms 9548 KB Output is correct
20 Correct 58 ms 9436 KB Output is correct
21 Correct 45 ms 9768 KB Output is correct
22 Correct 42 ms 8900 KB Output is correct
23 Correct 47 ms 8996 KB Output is correct
24 Correct 49 ms 9012 KB Output is correct
25 Correct 41 ms 9324 KB Output is correct
26 Correct 47 ms 9124 KB Output is correct
27 Correct 49 ms 9464 KB Output is correct
28 Correct 44 ms 9492 KB Output is correct
29 Correct 46 ms 9388 KB Output is correct
30 Correct 108 ms 12108 KB Output is correct
31 Correct 165 ms 14300 KB Output is correct
32 Correct 224 ms 16972 KB Output is correct
33 Correct 322 ms 22712 KB Output is correct
34 Correct 44 ms 11508 KB Output is correct
35 Correct 328 ms 22644 KB Output is correct
36 Correct 335 ms 22176 KB Output is correct
37 Correct 332 ms 22836 KB Output is correct
38 Correct 294 ms 22896 KB Output is correct
39 Correct 320 ms 22252 KB Output is correct
40 Correct 231 ms 23652 KB Output is correct
41 Correct 280 ms 22132 KB Output is correct
42 Correct 309 ms 22076 KB Output is correct
43 Correct 169 ms 23348 KB Output is correct
44 Correct 174 ms 23508 KB Output is correct
45 Correct 180 ms 23440 KB Output is correct
46 Correct 241 ms 22368 KB Output is correct
47 Correct 271 ms 21924 KB Output is correct
48 Correct 235 ms 22248 KB Output is correct
49 Correct 222 ms 22632 KB Output is correct