답안 #148078

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
148078 2019-08-31T13:42:27 Z WhipppedCream 운세 보기 2 (JOI14_fortune_telling2) C++17
100 / 100
1303 ms 128860 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
using namespace std;
#define X first
#define Y second
#define pb push_back
typedef pair<int, int> ii;
typedef long long ll;
 
int n, m;
 
const int maxn = 2e5+5;
 
struct node
{
	int val;
	node *left, *right;
	node(int a, node *b, node *c)
	{
		val = a;
		left = b;
		right = c;
	}
	node* insert(int x, int L = 0, int R = m-1) 
	{
		if(L<= x && x<= R)
		{
			if(L == R) return new node(val+1, NULL, NULL);
			int M = (L+R)/2;
			return new node(val+1, left->insert(x, L, M), right->insert(x, M+1, R));
		}
		return this;
	}
	int ask(int i, int j, int L = 0, int R = m-1)
	{
		if(i> R || j< L) return 0;
		if(i<= L && R<= j) return val;
		int M = (L+R)/2;
		int x = left->ask(i, j, L, M);
		int y = right->ask(i, j, M+1, R);
		return x+y;
	}
};
 
node* zero = new node(0, NULL, NULL);
 
int A[maxn], B[maxn];
 
int op[maxn];
node* ps[maxn];
vector< ii > foo;
 
int main()
{
	scanf("%d %d", &n, &m);
	for(int i = 1; i<= n; i++) scanf("%d %d", &A[i], &B[i]);
	zero->left = zero->right = zero;
	for(int i = 0; i< m; i++)
	{
		scanf("%d", &op[i]);
		foo.pb(ii(op[i], i));
	}
	sort(foo.begin(), foo.end());
	ps[m] = zero;
	for(int i = m-1; i>= 0; i--)
	{
		ps[i] = ps[i+1];
		ps[i] = ps[i]->insert(foo[i].Y);
		// printf("insert %d\n", foo[i].Y);
	}
	ll ans = 0;
	for(int i = 1; i<= n; i++)
	{
		int a = A[i], b = B[i];
		if(a> b) swap(a, b);
		int pa = lower_bound(foo.begin(), foo.end(), ii(a, 0))-foo.begin();
		int pb = lower_bound(foo.begin(), foo.end(), ii(b, 0))-foo.begin();
		// printf("pa pb %d %d\n", pa, pb);
		int lo = 0, hi = m-1;
		while(lo< hi)
		{
			int mid = (lo+hi+1)/2;
			if((ps[pa])->ask(mid, m-1) == (ps[pb])->ask(mid, m-1)) hi = mid-1;
			else lo = mid;
		}
		// printf("lo = %d\n", lo);
		if(ps[pa]->ask(lo, m-1) == ps[pb]->ask(lo, m-1)) 
		{
			int swaps = ps[pa]->ask(0, m-1);
			if(swaps%2) ans += B[i];
			else ans += A[i];
			continue;
		}
		int swaps = ps[pa]->ask(lo+1, m-1);
		// printf("swaps %d\n", swaps);
		if(swaps%2) ans += a;
		else ans += b;
	}
	printf("%lld\n", ans);
}

Compilation message

fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:56: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:57:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i = 1; i<= n; i++) scanf("%d %d", &A[i], &B[i]);
                             ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:61:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &op[i]);
   ~~~~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 888 KB Output is correct
2 Correct 4 ms 732 KB Output is correct
3 Correct 5 ms 760 KB Output is correct
4 Correct 5 ms 760 KB Output is correct
5 Correct 4 ms 760 KB Output is correct
6 Correct 4 ms 760 KB Output is correct
7 Correct 5 ms 732 KB Output is correct
8 Correct 4 ms 760 KB Output is correct
9 Correct 4 ms 760 KB Output is correct
10 Correct 4 ms 760 KB Output is correct
11 Correct 5 ms 760 KB Output is correct
12 Correct 5 ms 760 KB Output is correct
13 Correct 5 ms 760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 888 KB Output is correct
2 Correct 4 ms 732 KB Output is correct
3 Correct 5 ms 760 KB Output is correct
4 Correct 5 ms 760 KB Output is correct
5 Correct 4 ms 760 KB Output is correct
6 Correct 4 ms 760 KB Output is correct
7 Correct 5 ms 732 KB Output is correct
8 Correct 4 ms 760 KB Output is correct
9 Correct 4 ms 760 KB Output is correct
10 Correct 4 ms 760 KB Output is correct
11 Correct 5 ms 760 KB Output is correct
12 Correct 5 ms 760 KB Output is correct
13 Correct 5 ms 760 KB Output is correct
14 Correct 36 ms 5368 KB Output is correct
15 Correct 76 ms 11016 KB Output is correct
16 Correct 139 ms 17064 KB Output is correct
17 Correct 170 ms 23024 KB Output is correct
18 Correct 185 ms 23136 KB Output is correct
19 Correct 165 ms 23152 KB Output is correct
20 Correct 176 ms 23152 KB Output is correct
21 Correct 151 ms 23024 KB Output is correct
22 Correct 135 ms 22768 KB Output is correct
23 Correct 135 ms 22672 KB Output is correct
24 Correct 137 ms 22640 KB Output is correct
25 Correct 131 ms 22636 KB Output is correct
26 Correct 170 ms 23008 KB Output is correct
27 Correct 211 ms 23152 KB Output is correct
28 Correct 181 ms 23024 KB Output is correct
29 Correct 236 ms 23092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 888 KB Output is correct
2 Correct 4 ms 732 KB Output is correct
3 Correct 5 ms 760 KB Output is correct
4 Correct 5 ms 760 KB Output is correct
5 Correct 4 ms 760 KB Output is correct
6 Correct 4 ms 760 KB Output is correct
7 Correct 5 ms 732 KB Output is correct
8 Correct 4 ms 760 KB Output is correct
9 Correct 4 ms 760 KB Output is correct
10 Correct 4 ms 760 KB Output is correct
11 Correct 5 ms 760 KB Output is correct
12 Correct 5 ms 760 KB Output is correct
13 Correct 5 ms 760 KB Output is correct
14 Correct 36 ms 5368 KB Output is correct
15 Correct 76 ms 11016 KB Output is correct
16 Correct 139 ms 17064 KB Output is correct
17 Correct 170 ms 23024 KB Output is correct
18 Correct 185 ms 23136 KB Output is correct
19 Correct 165 ms 23152 KB Output is correct
20 Correct 176 ms 23152 KB Output is correct
21 Correct 151 ms 23024 KB Output is correct
22 Correct 135 ms 22768 KB Output is correct
23 Correct 135 ms 22672 KB Output is correct
24 Correct 137 ms 22640 KB Output is correct
25 Correct 131 ms 22636 KB Output is correct
26 Correct 170 ms 23008 KB Output is correct
27 Correct 211 ms 23152 KB Output is correct
28 Correct 181 ms 23024 KB Output is correct
29 Correct 236 ms 23092 KB Output is correct
30 Correct 428 ms 123528 KB Output is correct
31 Correct 584 ms 124552 KB Output is correct
32 Correct 821 ms 126000 KB Output is correct
33 Correct 1208 ms 128860 KB Output is correct
34 Correct 371 ms 123260 KB Output is correct
35 Correct 1208 ms 128800 KB Output is correct
36 Correct 1195 ms 128748 KB Output is correct
37 Correct 1279 ms 128744 KB Output is correct
38 Correct 1226 ms 128696 KB Output is correct
39 Correct 1267 ms 128604 KB Output is correct
40 Correct 1008 ms 128520 KB Output is correct
41 Correct 1216 ms 128712 KB Output is correct
42 Correct 1258 ms 128696 KB Output is correct
43 Correct 831 ms 128104 KB Output is correct
44 Correct 817 ms 127956 KB Output is correct
45 Correct 819 ms 127876 KB Output is correct
46 Correct 880 ms 126864 KB Output is correct
47 Correct 900 ms 126672 KB Output is correct
48 Correct 1303 ms 128756 KB Output is correct
49 Correct 1236 ms 128760 KB Output is correct