답안 #110757

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
110757 2019-05-12T07:51:02 Z autumn_eel 운세 보기 2 (JOI14_fortune_telling2) C++14
100 / 100
493 ms 31400 KB
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef pair<int,int>P;
typedef long long ll;

struct Segtree{
	int N;
	vector<int>dat;
	Segtree(int n){
		N=1;while(N<n)N<<=1;
		dat=vector<int>(2*N,-1);
	}
	void update(int k,int x){
		k+=N;
		dat[k]=x;
		while(k>1){
			k/=2;
			dat[k]=max(dat[k*2],dat[k*2+1]);
		}
	}
	int query(int l,int r){
		int res=-1;
		for(l+=N,r+=N;l<r;l>>=1,r>>=1){
			if(l&1)res=max(res,dat[l++]);
			if(r&1)res=max(res,dat[--r]);
		}
		return res;
	}
};

struct BIT{
	vector<int>bit;
	BIT(int n){
		bit=vector<int>(n+10);
	}
	void add(int k,int x){
		k++;
		while(k<bit.size()){
			bit[k]+=x;
			k+=k&-k;
		}
	}
	int sum(int k){
		k++;
		int res=0;
		while(k){
			res+=bit[k];
			k-=k&-k;
		}
		return res;
	}
};

int a[300000],b[300000];
int t[300000];
vector<P>query[300000];
int ans[300000];

int main(){
	int n,K;cin>>n>>K;
	vector<int>vs;
	rep(i,n){
		scanf("%d%d",&a[i],&b[i]);
		vs.push_back(a[i]);
		vs.push_back(b[i]);
	}
	rep(i,K){
		scanf("%d",&t[i]);
		vs.push_back(t[i]);
	}
	sort(vs.begin(),vs.end());
	vs.erase(unique(vs.begin(),vs.end()),vs.end());
	Segtree seg(vs.size());
	rep(i,K){
		int c=lower_bound(vs.begin(),vs.end(),t[i])-vs.begin();
		seg.update(c,i);
	}
	rep(i,n){
		int l=lower_bound(vs.begin(),vs.end(),a[i])-vs.begin();
		int r=lower_bound(vs.begin(),vs.end(),b[i])-vs.begin();
		int id=seg.query(min(l,r),max(l,r));
		if(a[i]<b[i]&&id!=-1)ans[i]=1;
		if(id+1<K)query[id+1].push_back(P(max(l,r),i));
	}
	BIT bit(vs.size());
	for(int i=K-1;i>=0;i--){
		int id=lower_bound(vs.begin(),vs.end(),t[i])-vs.begin();
		bit.add(0,1);
		bit.add(id+1,-1);
		for(P p:query[i]){
			ans[p.second]+=bit.sum(p.first);
		}
	}
	ll sum=0;
	rep(i,n){
		if(ans[i]%2==0)sum+=a[i];
		else sum+=b[i];
	}
	cout<<sum<<endl;
}

Compilation message

fortune_telling2.cpp: In member function 'void BIT::add(int, int)':
fortune_telling2.cpp:39:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while(k<bit.size()){
         ~^~~~~~~~~~~
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:64:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&a[i],&b[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:69:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&t[i]);
   ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 7360 KB Output is correct
2 Correct 9 ms 7424 KB Output is correct
3 Correct 10 ms 7552 KB Output is correct
4 Correct 10 ms 7536 KB Output is correct
5 Correct 11 ms 7568 KB Output is correct
6 Correct 12 ms 7516 KB Output is correct
7 Correct 12 ms 7524 KB Output is correct
8 Correct 11 ms 7524 KB Output is correct
9 Correct 12 ms 7456 KB Output is correct
10 Correct 11 ms 7464 KB Output is correct
11 Correct 12 ms 7524 KB Output is correct
12 Correct 11 ms 7548 KB Output is correct
13 Correct 11 ms 7508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 7360 KB Output is correct
2 Correct 9 ms 7424 KB Output is correct
3 Correct 10 ms 7552 KB Output is correct
4 Correct 10 ms 7536 KB Output is correct
5 Correct 11 ms 7568 KB Output is correct
6 Correct 12 ms 7516 KB Output is correct
7 Correct 12 ms 7524 KB Output is correct
8 Correct 11 ms 7524 KB Output is correct
9 Correct 12 ms 7456 KB Output is correct
10 Correct 11 ms 7464 KB Output is correct
11 Correct 12 ms 7524 KB Output is correct
12 Correct 11 ms 7548 KB Output is correct
13 Correct 11 ms 7508 KB Output is correct
14 Correct 31 ms 8420 KB Output is correct
15 Correct 44 ms 9444 KB Output is correct
16 Correct 69 ms 10732 KB Output is correct
17 Correct 82 ms 11480 KB Output is correct
18 Correct 94 ms 11364 KB Output is correct
19 Correct 77 ms 11536 KB Output is correct
20 Correct 77 ms 11684 KB Output is correct
21 Correct 77 ms 11224 KB Output is correct
22 Correct 60 ms 10784 KB Output is correct
23 Correct 49 ms 10204 KB Output is correct
24 Correct 47 ms 10352 KB Output is correct
25 Correct 64 ms 10704 KB Output is correct
26 Correct 61 ms 11120 KB Output is correct
27 Correct 67 ms 11496 KB Output is correct
28 Correct 87 ms 11588 KB Output is correct
29 Correct 80 ms 11672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 7360 KB Output is correct
2 Correct 9 ms 7424 KB Output is correct
3 Correct 10 ms 7552 KB Output is correct
4 Correct 10 ms 7536 KB Output is correct
5 Correct 11 ms 7568 KB Output is correct
6 Correct 12 ms 7516 KB Output is correct
7 Correct 12 ms 7524 KB Output is correct
8 Correct 11 ms 7524 KB Output is correct
9 Correct 12 ms 7456 KB Output is correct
10 Correct 11 ms 7464 KB Output is correct
11 Correct 12 ms 7524 KB Output is correct
12 Correct 11 ms 7548 KB Output is correct
13 Correct 11 ms 7508 KB Output is correct
14 Correct 31 ms 8420 KB Output is correct
15 Correct 44 ms 9444 KB Output is correct
16 Correct 69 ms 10732 KB Output is correct
17 Correct 82 ms 11480 KB Output is correct
18 Correct 94 ms 11364 KB Output is correct
19 Correct 77 ms 11536 KB Output is correct
20 Correct 77 ms 11684 KB Output is correct
21 Correct 77 ms 11224 KB Output is correct
22 Correct 60 ms 10784 KB Output is correct
23 Correct 49 ms 10204 KB Output is correct
24 Correct 47 ms 10352 KB Output is correct
25 Correct 64 ms 10704 KB Output is correct
26 Correct 61 ms 11120 KB Output is correct
27 Correct 67 ms 11496 KB Output is correct
28 Correct 87 ms 11588 KB Output is correct
29 Correct 80 ms 11672 KB Output is correct
30 Correct 200 ms 14440 KB Output is correct
31 Correct 226 ms 18536 KB Output is correct
32 Correct 284 ms 21040 KB Output is correct
33 Correct 475 ms 30668 KB Output is correct
34 Correct 152 ms 13928 KB Output is correct
35 Correct 441 ms 31344 KB Output is correct
36 Correct 466 ms 30464 KB Output is correct
37 Correct 469 ms 31320 KB Output is correct
38 Correct 481 ms 31400 KB Output is correct
39 Correct 493 ms 31280 KB Output is correct
40 Correct 434 ms 29140 KB Output is correct
41 Correct 438 ms 31212 KB Output is correct
42 Correct 436 ms 31324 KB Output is correct
43 Correct 266 ms 28748 KB Output is correct
44 Correct 299 ms 29004 KB Output is correct
45 Correct 289 ms 28780 KB Output is correct
46 Correct 261 ms 23488 KB Output is correct
47 Correct 250 ms 21692 KB Output is correct
48 Correct 309 ms 26628 KB Output is correct
49 Correct 320 ms 27060 KB Output is correct