Submission #751489

# Submission time Handle Problem Language Result Execution time Memory
751489 2023-05-31T16:18:24 Z aebov Fortune Telling 2 (JOI14_fortune_telling2) C++17
100 / 100
1478 ms 54736 KB
//#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O2")
//#pragma GCC optimize("fast-math")
#include<iostream>
#include<vector>
#include<cmath>
#include<queue>
#include<utility>
#include<algorithm>
#include<bitset>
#include<map>
#include<iomanip>
#include<set>
#include<string.h>
#include<stack>
#include <list>
#include <numeric>
#include <random>
#include<unordered_map>
#define all(x) x.begin(), x.end()
#define ln (e-s+1)
#define lc (id*2)
#define rc (id*2 + 1)
#define md ((e+s)/2)
#define dm ((e+s)/2+1)
#define pb push_back
#define ll long long 
#define endl '\n'
#define mk make_pair
#define F first
#define pll pair<ll, ll>
#define S second
#define pvv pair<vector<int> , vector<int> > 
#define pii pair<int,int>
#define plll pair<ll, pair<int, int> >
#define ld long double
#define mat vector<vector<ll>>
//#define int long long 
using namespace std;

const int N = (int)2e5 + 5, mod = (int) 1e9 + 7;
int n, k, a[N], b[N], t[N], mn, mx;
vector<int> seg[N << 2];

void build(int id = 1,int s = 1,int e = k){
	if(ln == 1){
		seg[id].pb(t[s]);
		return;
	}build(lc, s, md), build(rc, dm, e);
	for(int x : seg[lc]) seg[id].pb(x);
	for(int x : seg[rc]) seg[id].pb(x);
	sort(seg[id].begin(), seg[id].end());
}
pii get(int l2,int r2,int id = 1,int s = 1,int e = k){
	if(l2 > e || r2 < s) return {mod, 0};
	if( l2 <= s && e <= r2){
		int l = 0, r = seg[id].size() - 1, ans = -1;
		while(l <= r){
			int mid = (l+r) >> 1;
			if(seg[id][mid] >= mn) ans = mid, r = mid - 1;
			else l = mid + 1;
		}
		l = 0, r = seg[id].size() - 1;int ans2 = seg[id].size();
		while(l <= r){
			int mid = (l+r) >> 1;
			if(seg[id][mid] >= mx) ans2 = mid, r = mid - 1;
			else l = mid + 1;
		}
		if(ans == -1) return {mod , 0};
		return {seg[id][ans], seg[id].size() - ans2};
	}
	auto g1 = get(l2, r2, lc, s, md), g2 = get(l2, r2, rc, dm, e);
	return {min(g1.F, g2.F), g1.S + g2.S} ;
}
int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> k;
	for(int i = 1; i <= n; i ++) cin >> a[i] >> b[i];
	for(int i = 1; i <= k; i ++) cin >> t[i];
	build();
	ll ret = 0;
	for(int i = 1; i <= n; i ++){
		int l = 1, r = k, ans = k+1;
		mn = min(a[i], b[i]), mx = max(a[i], b[i]);
		while(l <= r){
			int mid = (l+r) >> 1;
			if(get(mid, k).F >= mx) ans = mid, r = mid - 1;
			else l = mid + 1;
		}
	//	cout << "few : " <<  ans << endl;
		if(get(1, ans - 1).F < mx){
			if(get(ans, k).S %2 == 0) ret += mx;
			else ret += mn;
		}
		else{
			if(get(ans , k).S % 2 == 0) ret += a[i];
			else ret += b[i];
		}
	//	cout << ret << endl;
	}cout << ret << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19212 KB Output is correct
2 Correct 13 ms 19220 KB Output is correct
3 Correct 14 ms 19260 KB Output is correct
4 Correct 15 ms 19156 KB Output is correct
5 Correct 14 ms 19192 KB Output is correct
6 Correct 14 ms 19264 KB Output is correct
7 Correct 15 ms 19260 KB Output is correct
8 Correct 12 ms 19156 KB Output is correct
9 Correct 12 ms 19156 KB Output is correct
10 Correct 13 ms 19156 KB Output is correct
11 Correct 13 ms 19156 KB Output is correct
12 Correct 13 ms 19156 KB Output is correct
13 Correct 15 ms 19156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19212 KB Output is correct
2 Correct 13 ms 19220 KB Output is correct
3 Correct 14 ms 19260 KB Output is correct
4 Correct 15 ms 19156 KB Output is correct
5 Correct 14 ms 19192 KB Output is correct
6 Correct 14 ms 19264 KB Output is correct
7 Correct 15 ms 19260 KB Output is correct
8 Correct 12 ms 19156 KB Output is correct
9 Correct 12 ms 19156 KB Output is correct
10 Correct 13 ms 19156 KB Output is correct
11 Correct 13 ms 19156 KB Output is correct
12 Correct 13 ms 19156 KB Output is correct
13 Correct 15 ms 19156 KB Output is correct
14 Correct 49 ms 20868 KB Output is correct
15 Correct 95 ms 22716 KB Output is correct
16 Correct 144 ms 23864 KB Output is correct
17 Correct 209 ms 26380 KB Output is correct
18 Correct 203 ms 26376 KB Output is correct
19 Correct 196 ms 26316 KB Output is correct
20 Correct 235 ms 26308 KB Output is correct
21 Correct 160 ms 26292 KB Output is correct
22 Correct 131 ms 25932 KB Output is correct
23 Correct 134 ms 25916 KB Output is correct
24 Correct 145 ms 25912 KB Output is correct
25 Correct 122 ms 25932 KB Output is correct
26 Correct 201 ms 26164 KB Output is correct
27 Correct 296 ms 26312 KB Output is correct
28 Correct 205 ms 26316 KB Output is correct
29 Correct 394 ms 26316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 19212 KB Output is correct
2 Correct 13 ms 19220 KB Output is correct
3 Correct 14 ms 19260 KB Output is correct
4 Correct 15 ms 19156 KB Output is correct
5 Correct 14 ms 19192 KB Output is correct
6 Correct 14 ms 19264 KB Output is correct
7 Correct 15 ms 19260 KB Output is correct
8 Correct 12 ms 19156 KB Output is correct
9 Correct 12 ms 19156 KB Output is correct
10 Correct 13 ms 19156 KB Output is correct
11 Correct 13 ms 19156 KB Output is correct
12 Correct 13 ms 19156 KB Output is correct
13 Correct 15 ms 19156 KB Output is correct
14 Correct 49 ms 20868 KB Output is correct
15 Correct 95 ms 22716 KB Output is correct
16 Correct 144 ms 23864 KB Output is correct
17 Correct 209 ms 26380 KB Output is correct
18 Correct 203 ms 26376 KB Output is correct
19 Correct 196 ms 26316 KB Output is correct
20 Correct 235 ms 26308 KB Output is correct
21 Correct 160 ms 26292 KB Output is correct
22 Correct 131 ms 25932 KB Output is correct
23 Correct 134 ms 25916 KB Output is correct
24 Correct 145 ms 25912 KB Output is correct
25 Correct 122 ms 25932 KB Output is correct
26 Correct 201 ms 26164 KB Output is correct
27 Correct 296 ms 26312 KB Output is correct
28 Correct 205 ms 26316 KB Output is correct
29 Correct 394 ms 26316 KB Output is correct
30 Correct 280 ms 49456 KB Output is correct
31 Correct 480 ms 50600 KB Output is correct
32 Correct 753 ms 51900 KB Output is correct
33 Correct 1315 ms 54668 KB Output is correct
34 Correct 233 ms 49220 KB Output is correct
35 Correct 1386 ms 54600 KB Output is correct
36 Correct 1275 ms 54668 KB Output is correct
37 Correct 1370 ms 54672 KB Output is correct
38 Correct 1353 ms 54720 KB Output is correct
39 Correct 1455 ms 54628 KB Output is correct
40 Correct 889 ms 54496 KB Output is correct
41 Correct 1400 ms 54736 KB Output is correct
42 Correct 1431 ms 54724 KB Output is correct
43 Correct 686 ms 54024 KB Output is correct
44 Correct 660 ms 54016 KB Output is correct
45 Correct 686 ms 53956 KB Output is correct
46 Correct 812 ms 52780 KB Output is correct
47 Correct 1016 ms 52624 KB Output is correct
48 Correct 1478 ms 54728 KB Output is correct
49 Correct 1321 ms 54704 KB Output is correct