답안 #170956

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
170956 2019-12-26T19:08:46 Z ZwariowanyMarcin 운세 보기 2 (JOI14_fortune_telling2) C++14
100 / 100
541 ms 43864 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define ss(x) (int) x.size()
#define pb push_back
#define ll long long
#define cat(x) cerr << #x << " = " << x << endl
#define FOR(i, n) for(int i = 0; i < n; ++i)

using namespace std;

const int nax = 6e5 + 111;
const int pod = (1 << 20);

int n, k;
int a[nax];
int b[nax];
int q[nax];

vector <int> val;
vector <int> gao[nax];

int pos(int x) {
	return (int) (lower_bound(val.begin(), val.end(), x) - val.begin());
}

struct seg {
	int t[2 * pod];
	
	void init() {
		for(int i = 1; i < 2 * pod; ++i)
			t[i] = 0;
	}
	
	void add(int x, int c) {
		x += pod;
		t[x] = max(t[x], c);
		x /= 2;
		while(x) {
			t[x] = max(t[2 * x], t[2 * x + 1]);
			x /= 2;
		}
	}
	
	int query(int l, int r) {
		r++;
		int res = 0;
		for(l += pod, r += pod; l < r; l /= 2, r /= 2) {
			if(l & 1)
				res = max(res, t[l++]);
			if(r & 1)
				res = max(res, t[--r]);
		}
		return res;
	}
} ja;

struct fen {
	int t[nax];
	
	void init() {
		for(int i = 0; i < nax; ++i)
			t[i] = 0;
	}
	
	void add(int x, int c) {
		for(; x < nax; x += x & -x)
			t[x] += c;
	}
	
	int query(int x) {
		int res = 0;
		for(; 0 < x; x -= x & -x)
			res += t[x];
		return res;
	}
	
	int sum(int l, int r) {
		return query(r) - query(l - 1);
	}
} on;

int main() {
	ja.init();
	on.init();
	val.pb(-1);
	scanf("%d %d", &n, &k);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", a + i);
		scanf("%d", b + i);
		val.pb(a[i]);
		val.pb(b[i]);
	}
	for(int i = 1; i <= k; ++i) {
		scanf("%d", q + i);
		val.pb(q[i]);
	}
	sort(val.begin(), val.end());
	val.erase(unique(val.begin(), val.end()), val.end());
	for(int i = 1; i <= n; ++i) {
		a[i] = pos(a[i]);
		b[i] = pos(b[i]);
	}
	for(int i = 1; i <= k; ++i) {
		q[i] = pos(q[i]);
		ja.add(q[i], i);
		gao[q[i]].pb(i);
	}
	ll sum = 0;
	int Last = ss(val);
	vector <int> v;
	for(int i = 1; i <= n; ++i)
		v.pb(i);
	sort(v.begin(), v.end(), [](int x, int y) {
		return max(a[x], b[x]) < max(a[y], b[y]);
	});
	
	for(int i = n - 1; 0 <= i; --i) {
		int ind = v[i];
		for(int j = max(a[ind], b[ind]); j < Last; ++j)
			for(auto it : gao[j])
				on.add(it, 1);
		Last = max(a[ind], b[ind]);
		int wsk = ja.query(min(a[ind], b[ind]), max(a[ind], b[ind]) - 1);
		int hop = on.sum(wsk + 1, k);
		if(wsk > 0) {
			if(hop % 2 == 0)
				sum += val[max(a[ind], b[ind])];
			else
				sum += val[min(a[ind], b[ind])];
		}
		else {
			if(hop % 2 == 0)
				sum += val[a[ind]];
			else
				sum += val[b[ind]];
		}
		
	}
	printf("%lld", sum);
	
	
	
	
	return 0;
}

Compilation message

fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:88:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &k);
  ~~~~~^~~~~~~~~~~~~~~~~
fortune_telling2.cpp:90:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", a + i);
   ~~~~~^~~~~~~~~~~~~
fortune_telling2.cpp:91:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", b + i);
   ~~~~~^~~~~~~~~~~~~
fortune_telling2.cpp:96:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", q + i);
   ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 25080 KB Output is correct
2 Correct 25 ms 25080 KB Output is correct
3 Correct 26 ms 25080 KB Output is correct
4 Correct 25 ms 25080 KB Output is correct
5 Correct 26 ms 25052 KB Output is correct
6 Correct 26 ms 25080 KB Output is correct
7 Correct 26 ms 25084 KB Output is correct
8 Correct 26 ms 25080 KB Output is correct
9 Correct 29 ms 25080 KB Output is correct
10 Correct 17 ms 25056 KB Output is correct
11 Correct 25 ms 25080 KB Output is correct
12 Correct 25 ms 25080 KB Output is correct
13 Correct 26 ms 25048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 25080 KB Output is correct
2 Correct 25 ms 25080 KB Output is correct
3 Correct 26 ms 25080 KB Output is correct
4 Correct 25 ms 25080 KB Output is correct
5 Correct 26 ms 25052 KB Output is correct
6 Correct 26 ms 25080 KB Output is correct
7 Correct 26 ms 25084 KB Output is correct
8 Correct 26 ms 25080 KB Output is correct
9 Correct 29 ms 25080 KB Output is correct
10 Correct 17 ms 25056 KB Output is correct
11 Correct 25 ms 25080 KB Output is correct
12 Correct 25 ms 25080 KB Output is correct
13 Correct 26 ms 25048 KB Output is correct
14 Correct 47 ms 26104 KB Output is correct
15 Correct 68 ms 26868 KB Output is correct
16 Correct 84 ms 27812 KB Output is correct
17 Correct 105 ms 28784 KB Output is correct
18 Correct 116 ms 28832 KB Output is correct
19 Correct 103 ms 28788 KB Output is correct
20 Correct 103 ms 28784 KB Output is correct
21 Correct 100 ms 28780 KB Output is correct
22 Correct 77 ms 28016 KB Output is correct
23 Correct 76 ms 28016 KB Output is correct
24 Correct 74 ms 27892 KB Output is correct
25 Correct 78 ms 28232 KB Output is correct
26 Correct 82 ms 28656 KB Output is correct
27 Correct 91 ms 28768 KB Output is correct
28 Correct 91 ms 28788 KB Output is correct
29 Correct 97 ms 28784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 25080 KB Output is correct
2 Correct 25 ms 25080 KB Output is correct
3 Correct 26 ms 25080 KB Output is correct
4 Correct 25 ms 25080 KB Output is correct
5 Correct 26 ms 25052 KB Output is correct
6 Correct 26 ms 25080 KB Output is correct
7 Correct 26 ms 25084 KB Output is correct
8 Correct 26 ms 25080 KB Output is correct
9 Correct 29 ms 25080 KB Output is correct
10 Correct 17 ms 25056 KB Output is correct
11 Correct 25 ms 25080 KB Output is correct
12 Correct 25 ms 25080 KB Output is correct
13 Correct 26 ms 25048 KB Output is correct
14 Correct 47 ms 26104 KB Output is correct
15 Correct 68 ms 26868 KB Output is correct
16 Correct 84 ms 27812 KB Output is correct
17 Correct 105 ms 28784 KB Output is correct
18 Correct 116 ms 28832 KB Output is correct
19 Correct 103 ms 28788 KB Output is correct
20 Correct 103 ms 28784 KB Output is correct
21 Correct 100 ms 28780 KB Output is correct
22 Correct 77 ms 28016 KB Output is correct
23 Correct 76 ms 28016 KB Output is correct
24 Correct 74 ms 27892 KB Output is correct
25 Correct 78 ms 28232 KB Output is correct
26 Correct 82 ms 28656 KB Output is correct
27 Correct 91 ms 28768 KB Output is correct
28 Correct 91 ms 28788 KB Output is correct
29 Correct 97 ms 28784 KB Output is correct
30 Correct 229 ms 35176 KB Output is correct
31 Correct 289 ms 37120 KB Output is correct
32 Correct 367 ms 39240 KB Output is correct
33 Correct 541 ms 43612 KB Output is correct
34 Correct 181 ms 34808 KB Output is correct
35 Correct 527 ms 43864 KB Output is correct
36 Correct 530 ms 43612 KB Output is correct
37 Correct 525 ms 43620 KB Output is correct
38 Correct 507 ms 43528 KB Output is correct
39 Correct 522 ms 43660 KB Output is correct
40 Correct 468 ms 43580 KB Output is correct
41 Correct 503 ms 43740 KB Output is correct
42 Correct 514 ms 43612 KB Output is correct
43 Correct 328 ms 43056 KB Output is correct
44 Correct 326 ms 42844 KB Output is correct
45 Correct 330 ms 42972 KB Output is correct
46 Correct 355 ms 40028 KB Output is correct
47 Correct 395 ms 39428 KB Output is correct
48 Correct 420 ms 43612 KB Output is correct
49 Correct 422 ms 43700 KB Output is correct