Submission #519296

# Submission time Handle Problem Language Result Execution time Memory
519296 2022-01-26T07:29:09 Z amunduzbaev Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
173 ms 10436 KB
#include "bits/stdc++.h"
using namespace std;

#define ar array

const int N = 2e5 + 5;
int a[N], b[N], f[N], t[N];
struct ST{
	int tree[N<<2], sum[N<<2];
	
	void init(){
		memset(tree, 127, sizeof tree);
	}
	
	void sett(int i, int v, int lx = 0, int rx = N, int x = 1){
		if(lx == rx) { tree[x] = v, sum[x] = 1; return; }
		int m = (lx + rx) >> 1;
		if(i <= m) sett(i, v, lx, m, x<<1);
		else sett(i, v, m+1, rx, x<<1|1);
		tree[x] = min(tree[x<<1], tree[x<<1|1]);
		sum[x] = sum[x<<1] + sum[x<<1|1];
	}
	
	int get(int a, int lx = 0, int rx = N, int x = 1){
		if(lx == rx) return lx - (tree[x] >= a);
		int m = (lx + rx) >> 1;
		if(tree[x<<1|1] < a) return get(a, m+1, rx, x<<1|1);
		return get(a, lx, m, x<<1);
	}
	
	int get_s(int l, int r, int lx = 0, int rx = N, int x = 1){
		if(lx > r || rx < l) return 0;
		if(lx >= l && rx <= r) return sum[x];
		int m = (lx + rx) >> 1;
		return get_s(l, r, lx, m, x<<1) + get_s(l, r, m+1, rx, x<<1|1);
	}
}tree;

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	tree.init();
	int n, k; cin>>n>>k;
	for(int i=0;i<n;i++){
		cin>>a[i]>>b[i];
		if(a[i] > b[i]) f[i] = 1, swap(a[i], b[i]);
	}
	
	for(int i=0;i<k;i++){
		cin>>t[i];
	}
	
	vector<int> p(n); iota(p.begin(), p.end(), 0);
	vector<int> q(k); iota(q.begin(), q.end(), 0);
	sort(p.begin(), p.end(), [&](int i, int j) { return (a[i] > a[j]); });
	sort(q.begin(), q.end(), [&](int i, int j) { return (t[i] > t[j]); });
	int j = 0;
	long long res = 0;
	for(auto i : p){
		while(j<k && t[q[j]] >= a[i]){
			tree.sett(q[j], t[q[j]]);
			j++;
		}
		
		//~ int s = 0, p;
		//~ for(p=k-1;~p;p--){
			//~ if(t[p] >= b[i]) s++;
			//~ if(a[i] <= t[p] && t[p] < b[i]){
				//~ break;
			//~ }
		//~ }
		
		int p = tree.get(b[i]);
		//~ assert(p1 == p);
		
		if(~p){
			int s = tree.get_s(p + 1, N);
			if(s&1) res += a[i];
			else res += b[i];
		} else {
			if(f[i]) swap(a[i], b[i]);
			
			if(j&1) res += b[i];
			else res += a[i];
		}
	}
	
	cout<<res<<"\n";
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3404 KB Output is correct
2 Correct 2 ms 3532 KB Output is correct
3 Correct 4 ms 3440 KB Output is correct
4 Correct 2 ms 3532 KB Output is correct
5 Correct 2 ms 3532 KB Output is correct
6 Correct 2 ms 3532 KB Output is correct
7 Correct 2 ms 3532 KB Output is correct
8 Correct 2 ms 3532 KB Output is correct
9 Correct 2 ms 3532 KB Output is correct
10 Correct 2 ms 3532 KB Output is correct
11 Correct 2 ms 3532 KB Output is correct
12 Correct 2 ms 3532 KB Output is correct
13 Correct 2 ms 3532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3404 KB Output is correct
2 Correct 2 ms 3532 KB Output is correct
3 Correct 4 ms 3440 KB Output is correct
4 Correct 2 ms 3532 KB Output is correct
5 Correct 2 ms 3532 KB Output is correct
6 Correct 2 ms 3532 KB Output is correct
7 Correct 2 ms 3532 KB Output is correct
8 Correct 2 ms 3532 KB Output is correct
9 Correct 2 ms 3532 KB Output is correct
10 Correct 2 ms 3532 KB Output is correct
11 Correct 2 ms 3532 KB Output is correct
12 Correct 2 ms 3532 KB Output is correct
13 Correct 2 ms 3532 KB Output is correct
14 Correct 9 ms 3852 KB Output is correct
15 Correct 15 ms 4172 KB Output is correct
16 Correct 22 ms 4428 KB Output is correct
17 Correct 28 ms 4792 KB Output is correct
18 Correct 29 ms 4860 KB Output is correct
19 Correct 29 ms 4816 KB Output is correct
20 Correct 30 ms 4824 KB Output is correct
21 Correct 39 ms 4840 KB Output is correct
22 Correct 24 ms 4724 KB Output is correct
23 Correct 23 ms 4668 KB Output is correct
24 Correct 23 ms 4696 KB Output is correct
25 Correct 22 ms 4684 KB Output is correct
26 Correct 23 ms 4740 KB Output is correct
27 Correct 27 ms 4812 KB Output is correct
28 Correct 28 ms 4792 KB Output is correct
29 Correct 28 ms 4792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3404 KB Output is correct
2 Correct 2 ms 3532 KB Output is correct
3 Correct 4 ms 3440 KB Output is correct
4 Correct 2 ms 3532 KB Output is correct
5 Correct 2 ms 3532 KB Output is correct
6 Correct 2 ms 3532 KB Output is correct
7 Correct 2 ms 3532 KB Output is correct
8 Correct 2 ms 3532 KB Output is correct
9 Correct 2 ms 3532 KB Output is correct
10 Correct 2 ms 3532 KB Output is correct
11 Correct 2 ms 3532 KB Output is correct
12 Correct 2 ms 3532 KB Output is correct
13 Correct 2 ms 3532 KB Output is correct
14 Correct 9 ms 3852 KB Output is correct
15 Correct 15 ms 4172 KB Output is correct
16 Correct 22 ms 4428 KB Output is correct
17 Correct 28 ms 4792 KB Output is correct
18 Correct 29 ms 4860 KB Output is correct
19 Correct 29 ms 4816 KB Output is correct
20 Correct 30 ms 4824 KB Output is correct
21 Correct 39 ms 4840 KB Output is correct
22 Correct 24 ms 4724 KB Output is correct
23 Correct 23 ms 4668 KB Output is correct
24 Correct 23 ms 4696 KB Output is correct
25 Correct 22 ms 4684 KB Output is correct
26 Correct 23 ms 4740 KB Output is correct
27 Correct 27 ms 4812 KB Output is correct
28 Correct 28 ms 4792 KB Output is correct
29 Correct 28 ms 4792 KB Output is correct
30 Correct 77 ms 7312 KB Output is correct
31 Correct 103 ms 7936 KB Output is correct
32 Correct 115 ms 8696 KB Output is correct
33 Correct 162 ms 10280 KB Output is correct
34 Correct 83 ms 7076 KB Output is correct
35 Correct 173 ms 10224 KB Output is correct
36 Correct 164 ms 10308 KB Output is correct
37 Correct 172 ms 10364 KB Output is correct
38 Correct 157 ms 10316 KB Output is correct
39 Correct 162 ms 10316 KB Output is correct
40 Correct 159 ms 10372 KB Output is correct
41 Correct 165 ms 10436 KB Output is correct
42 Correct 160 ms 10220 KB Output is correct
43 Correct 125 ms 9480 KB Output is correct
44 Correct 131 ms 9540 KB Output is correct
45 Correct 141 ms 9508 KB Output is correct
46 Correct 125 ms 9428 KB Output is correct
47 Correct 158 ms 9624 KB Output is correct
48 Correct 137 ms 10180 KB Output is correct
49 Correct 124 ms 10152 KB Output is correct