Submission #109090

# Submission time Handle Problem Language Result Execution time Memory
109090 2019-05-04T09:27:31 Z njchung99 Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
966 ms 57424 KB
#include<cstdio>
#include<algorithm>
#include<vector>
#define ll long long
using namespace std;
vector<ll> vt;
ll getidx(ll x) {
	return lower_bound(vt.begin(), vt.end(), x) - vt.begin();
}
ll seg[2400000];
ll seg1[2400000];
ll dap[200010];
ll update(ll pos, ll val, ll node, ll x, ll y) {
	if (pos < x || y < pos)return seg[node];
	if (x == y)return seg[node] += val;
	ll mid = (x + y) / 2;
	return seg[node] = update(pos, val, node * 2, x, mid) + update(pos, val, node * 2 + 1, mid + 1, y);
}
ll query(ll lo, ll hi, ll node, ll x, ll y) {
	if (hi < x || y < lo)return 0;
	if (lo <= x && y <= hi)return seg[node];
	ll mid = (x + y) / 2;
	return query(lo, hi, node * 2, x, mid) + query(lo, hi, node * 2 + 1, mid + 1, y);
}
ll update1(ll pos, ll val, ll node, ll x, ll y) {
	if (pos < x || y < pos)return seg1[node];
	if (x == y)return seg1[node] =max( val,seg1[node]);
	ll mid = (x + y) / 2;
	return seg1[node] = max(update1(pos, val, node * 2, x, mid),update1(pos, val, node * 2 + 1, mid + 1, y));
}
ll query1(ll lo, ll hi, ll node, ll x, ll y) {
	if (hi < x || y < lo)return 0;
	if (lo <= x && y <= hi)return seg1[node];
	ll mid = (x + y) / 2;
	return max(query1(lo, hi, node * 2, x, mid),query1(lo, hi, node * 2 + 1, mid + 1, y));
}
ll n, m;
pair<ll, ll> a[200100];
ll b[200010];
vector<pair<ll, ll>> vt1;
int main()
{
	scanf("%lld %lld", &n, &m);
	for (ll i = 1; i <= n; i++) {
		scanf("%lld %lld", &a[i].first, &a[i].second);
		vt.push_back(a[i].first);
		vt.push_back(a[i].second);
	}
	for (ll i = 1; i <= m; i++) {
		scanf("%lld", &b[i]);
		vt.push_back(b[i]);
	}
	sort(vt.begin(), vt.end());
	vt.erase(unique(vt.begin(), vt.end()), vt.end());
	for (ll i = 1; i <= m; i++) {
		b[i] = getidx(b[i]);
		update1(b[i], i, 1, 0, vt.size());
	}
	for (ll i = 1; i <= n; i++) {
		if (a[i].first != a[i].second) {
			ll xx=query1(getidx(min(a[i].first,a[i].second)), getidx(max(a[i].second,a[i].first)) - 1, 1, 0, vt.size());
			vt1.push_back({ xx, i});
		}
	}
	sort(vt1.begin(), vt1.end());
	ll idx = vt1.size() - 1;
	for (ll i = m; i >= 1; i--) {
		update(b[i], 1, 1, 0, vt.size());
		while (1) {
			if (idx==-1)break;
			if (vt1[idx].first != i)break;
			ll curr = vt1[idx].second;
			ll gap=query(getidx(max(a[curr].second,a[curr].first)), vt.size(), 1, 0, vt.size());
			if (gap % 2)
				dap[curr] = min(a[curr].first, a[curr].second);
			else
				dap[curr] = max(a[curr].first, a[curr].second);
			idx--;
		}
	}
	while (1) {
		if (idx == -1)break;
		ll curr = vt1[idx].second;
		ll gap = query(getidx(a[curr].second), vt.size(), 1, 0, vt.size());
		if (gap % 2)
			dap[curr] = a[curr].second;
		else
			dap[curr] = a[curr].first;
		idx--;
	}
	ll sum = 0;
	for (int i = 1; i <= n; i++) {
		if (dap[i])
			sum += dap[i];
		else
			sum += a[i].first;
	}
	printf("%lld\n", sum);
}

Compilation message

fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:43:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld %lld", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:45:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld %lld", &a[i].first, &a[i].second);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:50:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld", &b[i]);
   ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 4 ms 640 KB Output is correct
4 Correct 5 ms 640 KB Output is correct
5 Correct 4 ms 640 KB Output is correct
6 Correct 5 ms 612 KB Output is correct
7 Correct 4 ms 640 KB Output is correct
8 Correct 4 ms 640 KB Output is correct
9 Correct 4 ms 512 KB Output is correct
10 Correct 4 ms 512 KB Output is correct
11 Correct 4 ms 512 KB Output is correct
12 Correct 4 ms 512 KB Output is correct
13 Correct 4 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 4 ms 640 KB Output is correct
4 Correct 5 ms 640 KB Output is correct
5 Correct 4 ms 640 KB Output is correct
6 Correct 5 ms 612 KB Output is correct
7 Correct 4 ms 640 KB Output is correct
8 Correct 4 ms 640 KB Output is correct
9 Correct 4 ms 512 KB Output is correct
10 Correct 4 ms 512 KB Output is correct
11 Correct 4 ms 512 KB Output is correct
12 Correct 4 ms 512 KB Output is correct
13 Correct 4 ms 512 KB Output is correct
14 Correct 24 ms 2548 KB Output is correct
15 Correct 56 ms 4592 KB Output is correct
16 Correct 96 ms 7916 KB Output is correct
17 Correct 133 ms 8576 KB Output is correct
18 Correct 135 ms 8580 KB Output is correct
19 Correct 120 ms 8576 KB Output is correct
20 Correct 162 ms 8528 KB Output is correct
21 Correct 113 ms 8656 KB Output is correct
22 Correct 80 ms 8168 KB Output is correct
23 Correct 92 ms 6032 KB Output is correct
24 Correct 78 ms 6036 KB Output is correct
25 Correct 100 ms 8208 KB Output is correct
26 Correct 115 ms 8156 KB Output is correct
27 Correct 111 ms 8552 KB Output is correct
28 Correct 95 ms 8556 KB Output is correct
29 Correct 104 ms 8528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 3 ms 512 KB Output is correct
3 Correct 4 ms 640 KB Output is correct
4 Correct 5 ms 640 KB Output is correct
5 Correct 4 ms 640 KB Output is correct
6 Correct 5 ms 612 KB Output is correct
7 Correct 4 ms 640 KB Output is correct
8 Correct 4 ms 640 KB Output is correct
9 Correct 4 ms 512 KB Output is correct
10 Correct 4 ms 512 KB Output is correct
11 Correct 4 ms 512 KB Output is correct
12 Correct 4 ms 512 KB Output is correct
13 Correct 4 ms 512 KB Output is correct
14 Correct 24 ms 2548 KB Output is correct
15 Correct 56 ms 4592 KB Output is correct
16 Correct 96 ms 7916 KB Output is correct
17 Correct 133 ms 8576 KB Output is correct
18 Correct 135 ms 8580 KB Output is correct
19 Correct 120 ms 8576 KB Output is correct
20 Correct 162 ms 8528 KB Output is correct
21 Correct 113 ms 8656 KB Output is correct
22 Correct 80 ms 8168 KB Output is correct
23 Correct 92 ms 6032 KB Output is correct
24 Correct 78 ms 6036 KB Output is correct
25 Correct 100 ms 8208 KB Output is correct
26 Correct 115 ms 8156 KB Output is correct
27 Correct 111 ms 8552 KB Output is correct
28 Correct 95 ms 8556 KB Output is correct
29 Correct 104 ms 8528 KB Output is correct
30 Correct 274 ms 14820 KB Output is correct
31 Correct 406 ms 26616 KB Output is correct
32 Correct 562 ms 31452 KB Output is correct
33 Correct 920 ms 57424 KB Output is correct
34 Correct 261 ms 13796 KB Output is correct
35 Correct 816 ms 57396 KB Output is correct
36 Correct 793 ms 57320 KB Output is correct
37 Correct 966 ms 57368 KB Output is correct
38 Correct 865 ms 57424 KB Output is correct
39 Correct 938 ms 57296 KB Output is correct
40 Correct 857 ms 57028 KB Output is correct
41 Correct 901 ms 57356 KB Output is correct
42 Correct 915 ms 57300 KB Output is correct
43 Correct 409 ms 38196 KB Output is correct
44 Correct 465 ms 39204 KB Output is correct
45 Correct 518 ms 42448 KB Output is correct
46 Correct 492 ms 38888 KB Output is correct
47 Correct 466 ms 30752 KB Output is correct
48 Correct 633 ms 40784 KB Output is correct
49 Correct 599 ms 41056 KB Output is correct