Submission #519295

# Submission time Handle Problem Language Result Execution time Memory
519295 2022-01-26T07:28:53 Z amunduzbaev Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
212 ms 16084 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 && a[i] <= t[p] && t[p] < b[i]){
			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 2 ms 3404 KB Output is correct
2 Correct 2 ms 3532 KB Output is correct
3 Correct 3 ms 3532 KB Output is correct
4 Correct 4 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 3 ms 3532 KB Output is correct
8 Correct 3 ms 3532 KB Output is correct
9 Correct 3 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 3 ms 3532 KB Output is correct
13 Correct 4 ms 3532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3404 KB Output is correct
2 Correct 2 ms 3532 KB Output is correct
3 Correct 3 ms 3532 KB Output is correct
4 Correct 4 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 3 ms 3532 KB Output is correct
8 Correct 3 ms 3532 KB Output is correct
9 Correct 3 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 3 ms 3532 KB Output is correct
13 Correct 4 ms 3532 KB Output is correct
14 Correct 9 ms 3788 KB Output is correct
15 Correct 18 ms 4120 KB Output is correct
16 Correct 22 ms 4528 KB Output is correct
17 Correct 31 ms 4860 KB Output is correct
18 Correct 29 ms 4804 KB Output is correct
19 Correct 29 ms 4812 KB Output is correct
20 Correct 36 ms 4816 KB Output is correct
21 Correct 31 ms 4848 KB Output is correct
22 Correct 25 ms 4656 KB Output is correct
23 Correct 29 ms 4684 KB Output is correct
24 Correct 24 ms 4684 KB Output is correct
25 Correct 29 ms 4652 KB Output is correct
26 Correct 24 ms 4716 KB Output is correct
27 Correct 27 ms 4812 KB Output is correct
28 Correct 26 ms 4800 KB Output is correct
29 Correct 30 ms 4828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3404 KB Output is correct
2 Correct 2 ms 3532 KB Output is correct
3 Correct 3 ms 3532 KB Output is correct
4 Correct 4 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 3 ms 3532 KB Output is correct
8 Correct 3 ms 3532 KB Output is correct
9 Correct 3 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 3 ms 3532 KB Output is correct
13 Correct 4 ms 3532 KB Output is correct
14 Correct 9 ms 3788 KB Output is correct
15 Correct 18 ms 4120 KB Output is correct
16 Correct 22 ms 4528 KB Output is correct
17 Correct 31 ms 4860 KB Output is correct
18 Correct 29 ms 4804 KB Output is correct
19 Correct 29 ms 4812 KB Output is correct
20 Correct 36 ms 4816 KB Output is correct
21 Correct 31 ms 4848 KB Output is correct
22 Correct 25 ms 4656 KB Output is correct
23 Correct 29 ms 4684 KB Output is correct
24 Correct 24 ms 4684 KB Output is correct
25 Correct 29 ms 4652 KB Output is correct
26 Correct 24 ms 4716 KB Output is correct
27 Correct 27 ms 4812 KB Output is correct
28 Correct 26 ms 4800 KB Output is correct
29 Correct 30 ms 4828 KB Output is correct
30 Correct 72 ms 9344 KB Output is correct
31 Correct 106 ms 10728 KB Output is correct
32 Correct 114 ms 12420 KB Output is correct
33 Correct 170 ms 16004 KB Output is correct
34 Correct 85 ms 8944 KB Output is correct
35 Correct 169 ms 15940 KB Output is correct
36 Correct 178 ms 16004 KB Output is correct
37 Correct 210 ms 16012 KB Output is correct
38 Correct 164 ms 16084 KB Output is correct
39 Correct 202 ms 16080 KB Output is correct
40 Correct 159 ms 15796 KB Output is correct
41 Correct 168 ms 15936 KB Output is correct
42 Correct 212 ms 16016 KB Output is correct
43 Correct 134 ms 14568 KB Output is correct
44 Correct 136 ms 14552 KB Output is correct
45 Correct 142 ms 14556 KB Output is correct
46 Correct 160 ms 13300 KB Output is correct
47 Correct 154 ms 13156 KB Output is correct
48 Correct 152 ms 16064 KB Output is correct
49 Correct 139 ms 16036 KB Output is correct