답안 #109204

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
109204 2019-05-05T13:43:40 Z maruii 운세 보기 2 (JOI14_fortune_telling2) C++14
100 / 100
774 ms 69196 KB
#include <bits/stdc++.h>
using namespace std;

const int SIZE = 1<<19;
struct ST{
	int data[2*SIZE];
	void update(int x, int v){
		for(x+=SIZE; x; x>>=1) data[x] = max(data[x], v);
	}
	int query(int s, int e){
		int ret = 0;
		for(s+=SIZE, e+=SIZE; s<=e; s>>=1, e>>=1){
			if( s&1) ret = max(ret, data[s++]);
			if(~e&1) ret = max(ret, data[e--]);
		}
		return ret;
	}
} seg;
struct PST{
	struct Node{
		int l,r;
		bool v;
	} node[20*SIZE];
	int cnt;
	int init(int s, int e){
		int nn = cnt++;
		if(s==e) return nn;
		int m = s+e>>1;
		node[nn].l = init(s, m), node[nn].r = init(m+1, e);
		return nn;
	}
	int update(int bef, int ns, int ne, int x){
		if(ns>x || ne<x) return bef;
		int nn = cnt++;
		auto &cur = node[nn];
		if(ns==ne){
			cur = node[bef];
			cur.v ^= 1;
			return nn;
		}
		int m = ns+ne>>1;
		cur.l = update(node[bef].l, ns, m, x), cur.r = update(node[bef].r, m+1, ne, x);
		cur.v = node[cur.l].v ^ node[cur.r].v;
		return nn;
	}
	bool query(int nn, int ns, int ne, int s, int e){
		if(ns>e || ne<s) return 0;
		auto &cur = node[nn];
		if(s<=ns && ne<=e) return cur.v;
		int m = ns+ne>>1;
		return query(cur.l, ns, m, s, e) ^ query(cur.r, m+1, ne, s, e);
	}
} pst;
int root[200002], A[200001], B[200001], T[200001];
vector<int> xx;
int main(){
	ios_base::sync_with_stdio(0), cin.tie(0);
	int N, K; cin>>N>>K;
	for(int i=0; i<N; ++i) cin>>A[i]>>B[i], xx.push_back(A[i]), xx.push_back(B[i]);
	for(int i=1; i<=K; ++i) cin>>T[i];
	sort(xx.begin(), xx.end()), xx.erase(unique(xx.begin(), xx.end()), xx.end());
	for(int i=0; i<N; ++i){
		A[i] = lower_bound(xx.begin(), xx.end(), A[i]) - xx.begin() + 1;
		B[i] = lower_bound(xx.begin(), xx.end(), B[i]) - xx.begin() + 1;
	}
	root[K+1] = pst.init(0, xx.size());
	for(int i=K; i; --i){
		T[i] = upper_bound(xx.begin(), xx.end(), T[i]) - xx.begin();
		seg.update(T[i], i);
		root[i] = pst.update(root[i+1], 0, xx.size(), T[i]);
	}
	long long ans = 0;
	for(int i=0; i<N; ++i){
		int a=A[i], b=B[i];
		if(a>b) swap(a, b);
		int t = seg.query(a, b-1), s = pst.query(root[t+1], 0, xx.size(), b, xx.size());
		if(t && s) ans += xx[a-1];
		else if(t) ans += xx[b-1];
		else if(s) ans += xx[B[i]-1];
		else ans += xx[A[i]-1];
	}
	cout<<ans;
	return 0;
}

Compilation message

fortune_telling2.cpp: In member function 'int PST::init(int, int)':
fortune_telling2.cpp:28:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int m = s+e>>1;
           ~^~
fortune_telling2.cpp: In member function 'int PST::update(int, int, int, int)':
fortune_telling2.cpp:41:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int m = ns+ne>>1;
           ~~^~~
fortune_telling2.cpp: In member function 'bool PST::query(int, int, int, int, int)':
fortune_telling2.cpp:50:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int m = ns+ne>>1;
           ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 3 ms 640 KB Output is correct
3 Correct 4 ms 640 KB Output is correct
4 Correct 3 ms 640 KB Output is correct
5 Correct 5 ms 632 KB Output is correct
6 Correct 3 ms 760 KB Output is correct
7 Correct 5 ms 640 KB Output is correct
8 Correct 4 ms 640 KB Output is correct
9 Correct 4 ms 640 KB Output is correct
10 Correct 4 ms 640 KB Output is correct
11 Correct 5 ms 640 KB Output is correct
12 Correct 4 ms 640 KB Output is correct
13 Correct 4 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 3 ms 640 KB Output is correct
3 Correct 4 ms 640 KB Output is correct
4 Correct 3 ms 640 KB Output is correct
5 Correct 5 ms 632 KB Output is correct
6 Correct 3 ms 760 KB Output is correct
7 Correct 5 ms 640 KB Output is correct
8 Correct 4 ms 640 KB Output is correct
9 Correct 4 ms 640 KB Output is correct
10 Correct 4 ms 640 KB Output is correct
11 Correct 5 ms 640 KB Output is correct
12 Correct 4 ms 640 KB Output is correct
13 Correct 4 ms 640 KB Output is correct
14 Correct 26 ms 3456 KB Output is correct
15 Correct 44 ms 6648 KB Output is correct
16 Correct 70 ms 9976 KB Output is correct
17 Correct 100 ms 13276 KB Output is correct
18 Correct 97 ms 13428 KB Output is correct
19 Correct 128 ms 13272 KB Output is correct
20 Correct 91 ms 13300 KB Output is correct
21 Correct 79 ms 13176 KB Output is correct
22 Correct 73 ms 12788 KB Output is correct
23 Correct 72 ms 12028 KB Output is correct
24 Correct 65 ms 11380 KB Output is correct
25 Correct 54 ms 12788 KB Output is correct
26 Correct 50 ms 10996 KB Output is correct
27 Correct 61 ms 11508 KB Output is correct
28 Correct 82 ms 11508 KB Output is correct
29 Correct 108 ms 12404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 512 KB Output is correct
2 Correct 3 ms 640 KB Output is correct
3 Correct 4 ms 640 KB Output is correct
4 Correct 3 ms 640 KB Output is correct
5 Correct 5 ms 632 KB Output is correct
6 Correct 3 ms 760 KB Output is correct
7 Correct 5 ms 640 KB Output is correct
8 Correct 4 ms 640 KB Output is correct
9 Correct 4 ms 640 KB Output is correct
10 Correct 4 ms 640 KB Output is correct
11 Correct 5 ms 640 KB Output is correct
12 Correct 4 ms 640 KB Output is correct
13 Correct 4 ms 640 KB Output is correct
14 Correct 26 ms 3456 KB Output is correct
15 Correct 44 ms 6648 KB Output is correct
16 Correct 70 ms 9976 KB Output is correct
17 Correct 100 ms 13276 KB Output is correct
18 Correct 97 ms 13428 KB Output is correct
19 Correct 128 ms 13272 KB Output is correct
20 Correct 91 ms 13300 KB Output is correct
21 Correct 79 ms 13176 KB Output is correct
22 Correct 73 ms 12788 KB Output is correct
23 Correct 72 ms 12028 KB Output is correct
24 Correct 65 ms 11380 KB Output is correct
25 Correct 54 ms 12788 KB Output is correct
26 Correct 50 ms 10996 KB Output is correct
27 Correct 61 ms 11508 KB Output is correct
28 Correct 82 ms 11508 KB Output is correct
29 Correct 108 ms 12404 KB Output is correct
30 Correct 157 ms 41132 KB Output is correct
31 Correct 256 ms 50472 KB Output is correct
32 Correct 456 ms 57712 KB Output is correct
33 Correct 633 ms 68684 KB Output is correct
34 Correct 54 ms 10488 KB Output is correct
35 Correct 678 ms 68660 KB Output is correct
36 Correct 674 ms 68716 KB Output is correct
37 Correct 727 ms 68712 KB Output is correct
38 Correct 616 ms 68624 KB Output is correct
39 Correct 737 ms 68840 KB Output is correct
40 Correct 493 ms 69196 KB Output is correct
41 Correct 774 ms 68712 KB Output is correct
42 Correct 761 ms 68520 KB Output is correct
43 Correct 249 ms 65256 KB Output is correct
44 Correct 236 ms 65516 KB Output is correct
45 Correct 252 ms 66408 KB Output is correct
46 Correct 481 ms 63828 KB Output is correct
47 Correct 440 ms 59112 KB Output is correct
48 Correct 468 ms 59884 KB Output is correct
49 Correct 463 ms 59848 KB Output is correct