Submission #222857

# Submission time Handle Problem Language Result Execution time Memory
222857 2020-04-14T08:46:47 Z arnold518 Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
772 ms 51212 KB
#include <bits/stdc++.h>
using namespace std;

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

const int MAXN = 2e5;

int N, M;
pii A[MAXN+10];
int B[MAXN+10];
ll ans;

vector<int> tree[MAXN*4+10];

void init(int node, int tl, int tr)
{
	if(tl==tr)
	{
		tree[node].push_back(B[tl]);
		return;
	}
	int mid=tl+tr>>1;
	init(node*2, tl, mid);
	init(node*2+1, mid+1, tr);
	tree[node].resize(tree[node*2].size()+tree[node*2+1].size());
	merge(tree[node*2].begin(), tree[node*2].end(), tree[node*2+1].begin(), tree[node*2+1].end(), tree[node].begin());
}

int query1(int node, int tl, int tr, int l, int r)
{
	if(tl==tr) return tl;
	int mid=tl+tr>>1;
	if(lower_bound(tree[node*2+1].begin(), tree[node*2+1].end(), r)-lower_bound(tree[node*2+1].begin(), tree[node*2+1].end(), l)) return query1(node*2+1, mid+1, tr, l, r);
	else return query1(node*2, tl, mid, l, r);
}

int query2(int node, int tl, int tr, int l, int r, int k)
{
	if(r<tl || tr<l) return 0;
	if(l<=tl && tr<=r) return tree[node].end()-lower_bound(tree[node].begin(), tree[node].end(), k);
	int mid=tl+tr>>1;
	return query2(node*2, tl, mid, l, r, k)+query2(node*2+1, mid+1, tr, l, r, k);
}

int main()
{
	int i, j;

	scanf("%d%d", &N, &M);
	for(i=1; i<=N; i++) scanf("%d%d", &A[i].first, &A[i].second);
	for(i=1; i<=M; i++) scanf("%d", &B[i]);

	init(1, 1, M);
	for(i=1; i<=N; i++)
	{
		int l=A[i].first, r=A[i].second;
		if(l>r) swap(l, r);
		int t=lower_bound(tree[1].begin(), tree[1].end(), r)-lower_bound(tree[1].begin(), tree[1].end(), l);

		if(t)
		{
			int p=query1(1, 1, M, l, r);
			int q=query2(1, 1, M, p+1, M, r);
			if(q%2) ans+=l;
			else ans+=r;
		}
		else
		{
			int q=query2(1, 1, M, 1, M, r);
			if(q%2) ans+=A[i].second;
			else ans+=A[i].first;
		}
	}
	printf("%lld\n", ans);
}

Compilation message

fortune_telling2.cpp: In function 'void init(int, int, int)':
fortune_telling2.cpp:24:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid=tl+tr>>1;
          ~~^~~
fortune_telling2.cpp: In function 'int query1(int, int, int, int, int)':
fortune_telling2.cpp:34:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid=tl+tr>>1;
          ~~^~~
fortune_telling2.cpp: In function 'int query2(int, int, int, int, int, int)':
fortune_telling2.cpp:43:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int mid=tl+tr>>1;
          ~~^~~
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:49:9: warning: unused variable 'j' [-Wunused-variable]
  int i, j;
         ^
fortune_telling2.cpp:51:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &N, &M);
  ~~~~~^~~~~~~~~~~~~~~~
fortune_telling2.cpp:52:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(i=1; i<=N; i++) scanf("%d%d", &A[i].first, &A[i].second);
                      ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:53:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(i=1; i<=M; i++) scanf("%d", &B[i]);
                      ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 17 ms 19200 KB Output is correct
2 Correct 18 ms 19200 KB Output is correct
3 Correct 19 ms 19328 KB Output is correct
4 Correct 18 ms 19200 KB Output is correct
5 Correct 18 ms 19200 KB Output is correct
6 Correct 18 ms 19200 KB Output is correct
7 Correct 19 ms 19200 KB Output is correct
8 Correct 19 ms 19200 KB Output is correct
9 Correct 18 ms 19200 KB Output is correct
10 Correct 18 ms 19200 KB Output is correct
11 Correct 19 ms 19328 KB Output is correct
12 Correct 18 ms 19200 KB Output is correct
13 Correct 19 ms 19200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 19200 KB Output is correct
2 Correct 18 ms 19200 KB Output is correct
3 Correct 19 ms 19328 KB Output is correct
4 Correct 18 ms 19200 KB Output is correct
5 Correct 18 ms 19200 KB Output is correct
6 Correct 18 ms 19200 KB Output is correct
7 Correct 19 ms 19200 KB Output is correct
8 Correct 19 ms 19200 KB Output is correct
9 Correct 18 ms 19200 KB Output is correct
10 Correct 18 ms 19200 KB Output is correct
11 Correct 19 ms 19328 KB Output is correct
12 Correct 18 ms 19200 KB Output is correct
13 Correct 19 ms 19200 KB Output is correct
14 Correct 39 ms 20608 KB Output is correct
15 Correct 66 ms 22008 KB Output is correct
16 Correct 103 ms 23676 KB Output is correct
17 Correct 128 ms 25208 KB Output is correct
18 Correct 126 ms 25208 KB Output is correct
19 Correct 123 ms 25208 KB Output is correct
20 Correct 133 ms 25336 KB Output is correct
21 Correct 108 ms 25080 KB Output is correct
22 Correct 71 ms 24732 KB Output is correct
23 Correct 73 ms 24700 KB Output is correct
24 Correct 75 ms 24696 KB Output is correct
25 Correct 67 ms 24696 KB Output is correct
26 Correct 96 ms 24960 KB Output is correct
27 Correct 133 ms 25208 KB Output is correct
28 Correct 96 ms 25208 KB Output is correct
29 Correct 96 ms 25208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 19200 KB Output is correct
2 Correct 18 ms 19200 KB Output is correct
3 Correct 19 ms 19328 KB Output is correct
4 Correct 18 ms 19200 KB Output is correct
5 Correct 18 ms 19200 KB Output is correct
6 Correct 18 ms 19200 KB Output is correct
7 Correct 19 ms 19200 KB Output is correct
8 Correct 19 ms 19200 KB Output is correct
9 Correct 18 ms 19200 KB Output is correct
10 Correct 18 ms 19200 KB Output is correct
11 Correct 19 ms 19328 KB Output is correct
12 Correct 18 ms 19200 KB Output is correct
13 Correct 19 ms 19200 KB Output is correct
14 Correct 39 ms 20608 KB Output is correct
15 Correct 66 ms 22008 KB Output is correct
16 Correct 103 ms 23676 KB Output is correct
17 Correct 128 ms 25208 KB Output is correct
18 Correct 126 ms 25208 KB Output is correct
19 Correct 123 ms 25208 KB Output is correct
20 Correct 133 ms 25336 KB Output is correct
21 Correct 108 ms 25080 KB Output is correct
22 Correct 71 ms 24732 KB Output is correct
23 Correct 73 ms 24700 KB Output is correct
24 Correct 75 ms 24696 KB Output is correct
25 Correct 67 ms 24696 KB Output is correct
26 Correct 96 ms 24960 KB Output is correct
27 Correct 133 ms 25208 KB Output is correct
28 Correct 96 ms 25208 KB Output is correct
29 Correct 96 ms 25208 KB Output is correct
30 Correct 173 ms 46072 KB Output is correct
31 Correct 294 ms 46972 KB Output is correct
32 Correct 443 ms 48376 KB Output is correct
33 Correct 749 ms 51064 KB Output is correct
34 Correct 137 ms 45692 KB Output is correct
35 Correct 772 ms 51192 KB Output is correct
36 Correct 740 ms 51040 KB Output is correct
37 Correct 739 ms 51192 KB Output is correct
38 Correct 738 ms 51192 KB Output is correct
39 Correct 767 ms 51192 KB Output is correct
40 Correct 585 ms 50872 KB Output is correct
41 Correct 741 ms 51212 KB Output is correct
42 Correct 772 ms 51064 KB Output is correct
43 Correct 303 ms 50576 KB Output is correct
44 Correct 304 ms 50300 KB Output is correct
45 Correct 300 ms 50552 KB Output is correct
46 Correct 354 ms 49144 KB Output is correct
47 Correct 395 ms 49144 KB Output is correct
48 Correct 631 ms 51064 KB Output is correct
49 Correct 533 ms 51192 KB Output is correct