Submission #434765

# Submission time Handle Problem Language Result Execution time Memory
434765 2021-06-21T18:35:33 Z soroush Fortune Telling 2 (JOI14_fortune_telling2) C++17
100 / 100
281 ms 14736 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int , int > pii;

const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;

#define endl '\n'
#define ms(x , y) memset(x , y , sizeof x)
#define dokme(x) cout << x , exit(0);
#define lc (v << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define ff first 
#define ss second


int n , k , t[maxn] , cnt[maxn << 2];
pii a[maxn] , mx[maxn << 2];


void build(int v = 1 , int l = 1 , int r = k + 1){	
	if(r - l == 1){
		cnt[v] = 0, mx[v] = {t[l] , l};
		return;
	}
	build(lc , l , mid);
	build(rc , mid , r);
	cnt[v] = cnt[lc] + cnt[rc];
	mx[v] = max(mx[lc] , mx[rc]);
}

void shift(int pos , int v = 1 , int l = 1 , int r = k + 1){
	if(r - l == 1){
		cnt[v] = 1 , mx[v] = {-1 , l};
		return;
	}
	if(pos < mid)
		shift(pos , lc , l , mid);
	else
		shift(pos , rc , mid , r);
	cnt[v] = cnt[lc] + cnt[rc];
	mx[v] = max(mx[lc] , mx[rc]);
}

int get(int L , int R , int v = 1 , int l = 1 , int r = k + 1){
	if(r <= L or R <= l)return 0;
	if(L <= l and r <= R)return cnt[v];
	return get(L , R , lc , l , mid) + get(L , R , rc , mid , r);
}

int dfs(int x , int v = 1 , int l = 1 , int r = k + 1){
	if(r - l == 1)
		return l;
	if(mx[rc].ff >= x)return dfs(x , rc , mid, r);
	return dfs(x , lc , l , mid);
}

int32_t main(){
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> n >> k;
	for(int i = 1 ; i <= n ; i ++)
		cin >> a[i].ff >> a[i].ss;
	sort(a + 1 , a + 1 + n , [](pii a , pii b){return max(a.ff , a.ss) > max(b.ff , b.ss);});
	for(int i = 1 ; i <= k ; i ++)
		cin >> t[i];
	build();
	ll ans = 0;
	for(int i = 1 ; i <= n ; i ++){
		bool swp = a[i].ff < a[i].ss;
		if(swp)swap(a[i].ff , a[i].ss);
		while(mx[1].ff >= a[i].ff)
			shift(mx[1].ss);
		if(mx[1].ff >= a[i].ss)
			ans += ((get(dfs(a[i].ss) , k + 1) % 2) ? a[i].ss : a[i].ff);
		else
			ans += ((swp) ? ((cnt[1]%2) ? a[i].ff : a[i].ss) : (((cnt[1]%2) ? a[i].ss : a[i].ff)));
	}
	cout << ans;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 328 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 328 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 10 ms 1100 KB Output is correct
15 Correct 22 ms 1904 KB Output is correct
16 Correct 31 ms 2336 KB Output is correct
17 Correct 41 ms 3268 KB Output is correct
18 Correct 41 ms 3340 KB Output is correct
19 Correct 37 ms 3336 KB Output is correct
20 Correct 50 ms 3280 KB Output is correct
21 Correct 36 ms 3352 KB Output is correct
22 Correct 35 ms 3012 KB Output is correct
23 Correct 33 ms 3020 KB Output is correct
24 Correct 35 ms 3016 KB Output is correct
25 Correct 28 ms 3012 KB Output is correct
26 Correct 36 ms 3192 KB Output is correct
27 Correct 40 ms 3268 KB Output is correct
28 Correct 43 ms 3296 KB Output is correct
29 Correct 37 ms 3320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 328 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 2 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 10 ms 1100 KB Output is correct
15 Correct 22 ms 1904 KB Output is correct
16 Correct 31 ms 2336 KB Output is correct
17 Correct 41 ms 3268 KB Output is correct
18 Correct 41 ms 3340 KB Output is correct
19 Correct 37 ms 3336 KB Output is correct
20 Correct 50 ms 3280 KB Output is correct
21 Correct 36 ms 3352 KB Output is correct
22 Correct 35 ms 3012 KB Output is correct
23 Correct 33 ms 3020 KB Output is correct
24 Correct 35 ms 3016 KB Output is correct
25 Correct 28 ms 3012 KB Output is correct
26 Correct 36 ms 3192 KB Output is correct
27 Correct 40 ms 3268 KB Output is correct
28 Correct 43 ms 3296 KB Output is correct
29 Correct 37 ms 3320 KB Output is correct
30 Correct 114 ms 8340 KB Output is correct
31 Correct 141 ms 8736 KB Output is correct
32 Correct 186 ms 9048 KB Output is correct
33 Correct 239 ms 9796 KB Output is correct
34 Correct 46 ms 9080 KB Output is correct
35 Correct 257 ms 14608 KB Output is correct
36 Correct 281 ms 14532 KB Output is correct
37 Correct 228 ms 14520 KB Output is correct
38 Correct 212 ms 14620 KB Output is correct
39 Correct 238 ms 14624 KB Output is correct
40 Correct 165 ms 14412 KB Output is correct
41 Correct 220 ms 14656 KB Output is correct
42 Correct 229 ms 14524 KB Output is correct
43 Correct 135 ms 13852 KB Output is correct
44 Correct 137 ms 13932 KB Output is correct
45 Correct 134 ms 13848 KB Output is correct
46 Correct 176 ms 12648 KB Output is correct
47 Correct 198 ms 12484 KB Output is correct
48 Correct 235 ms 14736 KB Output is correct
49 Correct 225 ms 14616 KB Output is correct