Submission #342242

# Submission time Handle Problem Language Result Execution time Memory
342242 2021-01-01T16:36:36 Z VodkaInTheJar Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
436 ms 26852 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define endl '\n'

using namespace std;

const int maxn = 2e5 + 3; 

int n, k; 
pair <int, int> a[maxn];
int t[maxn];
void read()
{
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
	cin >> a[i].first >> a[i].second;
	
	for (int i = 1; i <= k; i++)
	cin >> t[i];
}

int tr[maxn * 8];
void merge(int v)
{
	tr[v] = max(tr[v * 2], tr[v * 2 + 1]);
}

void update(int v, int l, int r, int pos, int val)
{
	if (l == r)
	{
		tr[v] = max(tr[v], val);
		return;
	}
	
	int mid = (l + r) / 2;
	if (pos <= mid)
	update(v * 2, l, mid, pos, val);
	
	else 
	update(v * 2 + 1, mid + 1, r, pos, val);
	
	merge(v);
}

int get(int v, int l, int r, int ll, int rr)
{
	if (l > rr || r < ll || ll > rr)
	return 0;
	
	if (l >= ll && r <= rr)
	return tr[v];
	
	int mid = (l + r) / 2;
	return max(get(v * 2, l, mid, ll, rr), get(v * 2 + 1, mid + 1, r, ll, rr));
}

int fenw[maxn * 2];
void update_fenw(int pos)
{
	for (; pos <= 2 * n + 1; pos += pos & -pos)
	fenw[pos]++; 
}

int get_pr(int pos)
{
	int ans = 0;
	for (; pos >= 1; pos -= pos & -pos)
	ans += fenw[pos];
	
	return ans;
}

vector <int> v;
vector <int> queries[maxn * 2];
void solve()
{
	for (int i = 1; i <= n; i++)
	{
		v.push_back(a[i].first);
		v.push_back(a[i].second);
	}
	
	sort (v.begin(), v.end());
	
	for (int i = 1; i <= k; i++)
	{
		int pos = lower_bound(v.begin(), v.end(), t[i]) - v.begin();
		if (pos != 2 * n && v[pos] == t[i])
		t[i] = pos + 1;
	
		else 
		t[i] = pos;  
		
	    update(1, 1, 2 * n, t[i], i);
	}
	
	for (int i = 1; i <= n; i++)
	{
		int f = lower_bound(v.begin(), v.end(), a[i].first) - v.begin() + 1, s = lower_bound(v.begin(), v.end(), a[i].second) - v.begin() + 1;
		queries[get(1, 1, 2 * n, min(f, s), max(f, s) - 1)].push_back(i);
	}
	
	long long ans = 0;
	for (int i = k; i >= 0; i--)
	{
		for (auto j: queries[i])
		{
			int curr_pos = lower_bound(v.begin(), v.end(), max(a[j].first, a[j].second)) - v.begin() + 2; 
			int cnt = k - i - get_pr(curr_pos - 1);
			
			if (i == 0)
			ans += (cnt & 1) ? (long long)a[j].second : (long long)a[j].first;
			
			else 
			ans += (cnt & 1) ? (long long)min(a[j].first, a[j].second) : (long long)max(a[j].first, a[j].second);
		}
		
		if (i != 0)
		update_fenw(t[i] + 1);
	}
	
	cout << ans << endl; 
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	read();
	solve();
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 7 ms 9836 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 8 ms 9836 KB Output is correct
5 Correct 8 ms 9836 KB Output is correct
6 Correct 7 ms 9836 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
8 Correct 7 ms 9836 KB Output is correct
9 Correct 9 ms 9836 KB Output is correct
10 Correct 7 ms 9836 KB Output is correct
11 Correct 7 ms 9836 KB Output is correct
12 Correct 7 ms 9836 KB Output is correct
13 Correct 7 ms 9836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 7 ms 9836 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 8 ms 9836 KB Output is correct
5 Correct 8 ms 9836 KB Output is correct
6 Correct 7 ms 9836 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
8 Correct 7 ms 9836 KB Output is correct
9 Correct 9 ms 9836 KB Output is correct
10 Correct 7 ms 9836 KB Output is correct
11 Correct 7 ms 9836 KB Output is correct
12 Correct 7 ms 9836 KB Output is correct
13 Correct 7 ms 9836 KB Output is correct
14 Correct 21 ms 10476 KB Output is correct
15 Correct 36 ms 10992 KB Output is correct
16 Correct 55 ms 11248 KB Output is correct
17 Correct 74 ms 13292 KB Output is correct
18 Correct 76 ms 13312 KB Output is correct
19 Correct 75 ms 13420 KB Output is correct
20 Correct 70 ms 13292 KB Output is correct
21 Correct 67 ms 13420 KB Output is correct
22 Correct 50 ms 12908 KB Output is correct
23 Correct 54 ms 12908 KB Output is correct
24 Correct 55 ms 12908 KB Output is correct
25 Correct 49 ms 13036 KB Output is correct
26 Correct 50 ms 12652 KB Output is correct
27 Correct 64 ms 13292 KB Output is correct
28 Correct 55 ms 12780 KB Output is correct
29 Correct 67 ms 13420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 7 ms 9836 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 8 ms 9836 KB Output is correct
5 Correct 8 ms 9836 KB Output is correct
6 Correct 7 ms 9836 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
8 Correct 7 ms 9836 KB Output is correct
9 Correct 9 ms 9836 KB Output is correct
10 Correct 7 ms 9836 KB Output is correct
11 Correct 7 ms 9836 KB Output is correct
12 Correct 7 ms 9836 KB Output is correct
13 Correct 7 ms 9836 KB Output is correct
14 Correct 21 ms 10476 KB Output is correct
15 Correct 36 ms 10992 KB Output is correct
16 Correct 55 ms 11248 KB Output is correct
17 Correct 74 ms 13292 KB Output is correct
18 Correct 76 ms 13312 KB Output is correct
19 Correct 75 ms 13420 KB Output is correct
20 Correct 70 ms 13292 KB Output is correct
21 Correct 67 ms 13420 KB Output is correct
22 Correct 50 ms 12908 KB Output is correct
23 Correct 54 ms 12908 KB Output is correct
24 Correct 55 ms 12908 KB Output is correct
25 Correct 49 ms 13036 KB Output is correct
26 Correct 50 ms 12652 KB Output is correct
27 Correct 64 ms 13292 KB Output is correct
28 Correct 55 ms 12780 KB Output is correct
29 Correct 67 ms 13420 KB Output is correct
30 Correct 95 ms 13292 KB Output is correct
31 Correct 160 ms 15980 KB Output is correct
32 Correct 249 ms 19612 KB Output is correct
33 Correct 423 ms 26468 KB Output is correct
34 Correct 33 ms 12524 KB Output is correct
35 Correct 431 ms 26468 KB Output is correct
36 Correct 436 ms 26340 KB Output is correct
37 Correct 425 ms 26212 KB Output is correct
38 Correct 421 ms 26340 KB Output is correct
39 Correct 416 ms 26340 KB Output is correct
40 Correct 374 ms 26852 KB Output is correct
41 Correct 413 ms 26212 KB Output is correct
42 Correct 421 ms 26212 KB Output is correct
43 Correct 217 ms 22756 KB Output is correct
44 Correct 219 ms 23524 KB Output is correct
45 Correct 218 ms 24824 KB Output is correct
46 Correct 284 ms 24420 KB Output is correct
47 Correct 274 ms 24292 KB Output is correct
48 Correct 310 ms 23868 KB Output is correct
49 Correct 294 ms 23780 KB Output is correct