Submission #317582

# Submission time Handle Problem Language Result Execution time Memory
317582 2020-10-30T03:13:54 Z bin1st090104 Fortune Telling 2 (JOI14_fortune_telling2) C++11
100 / 100
350 ms 9916 KB
#include <bits/stdc++.h>

#define Size(x) ((int)(x).size())
#define endl '\n'

using namespace std;

template<typename T> using vc=vector<T>;
template<typename T> using vc1=vc<T>;
template<typename T> using vc2=vc<vc1<T>>;
template<typename T> using vc3=vc<vc2<T>>;
template<typename T> using vc4=vc<vc3<T>>;
template<typename T> using cr=const T&;
using pii=pair<int,int>;
using lli=long long int;
using pli=pair<lli,int>;

template<typename X,typename Y>
bool minimize(X&x,cr<Y>y){
	return (y<x?x=y,true:false);
}

template<typename X,typename Y>
bool maximize(X&x,cr<Y>y){
	return (y>x?x=y,true:false);
}

const int maxN=2e5;
const int maxM=2e5;
const int inf=0x3f3f3f3f;
const lli INF=0x3f3f3f3f3f3f3f3f;

struct Interval_Tree_Sum{
	vc<int> Node;
	int N;
	Interval_Tree_Sum(int __N){
		N=__N;
		Node.resize(4*N+3,0);
		return ;
	}
	void Upd(int i,int l,int r,int p,int v){
		if(p<l||r<p)
			return;
		if(l==r){
			Node[i]=v;
			return ;
		}
		int m=(l+r)>>1;
		if(p<=m)
			Upd(i<<1,l,m,p,v);
		else
			Upd(i<<1|1,m+1,r,p,v);
		Node[i]=Node[i<<1]+Node[i<<1|1];
		return ;
	}
	void Upd(int p,int v){
		Upd(1,1,N,p,v);
	}
	int Get(int i,int l,int r,int u,int v){
		if(v<l||r<u||u>v)
			return 0;
		if(u<=l&&r<=v)
			return Node[i];
		int m=(l+r)>>1;
		return Get(i<<1,l,m,u,v)+Get(i<<1|1,m+1,r,u,v);
	}
	int Get(int u,int v){
		return Get(1,1,N,u,v);
	}
};

struct Interval_Tree_Max{
	vc<int> Node;
	int N;
	Interval_Tree_Max(int __N){
		N=__N;
		Node.resize(4*N+3);
	}
	void Upd(int i,int l,int r,int p,int v){
		if(p<l||r<p)
			return ;
		if(l==r){
			Node[i]=v;
			return ;
		}
		int m=(l+r)>>1;
		if(p<=m)
			Upd(i<<1,l,m,p,v);
		else
			Upd(i<<1|1,m+1,r,p,v);
		Node[i]=max(Node[i<<1],Node[i<<1|1]);
		return ;
	}
	void Upd(int p,int v){
		Upd(1,1,N,p,v);
		return ;
	}
	int Get(int i,int l,int r,int u,int v){
		if(v<l||r<u||u>v)
			return -1;
		if(u<=l&&r<=v)
			return Node[i];
		int m=(l+r)>>1;
		return max(Get(i<<1,l,m,u,v),Get(i<<1|1,m+1,r,u,v));
	}
	int Get(int u,int v){
		return Get(1,1,N,u,v);
	}
};

int N,K;
pii a[maxN+3];
pii t[maxN+3];

int main(){
	ios_base::sync_with_stdio(false);cin.tie(nullptr);
	//freopen("t.inp","r",stdin);freopen("t.out","w",stdout);
	cin>>N>>K;
	for(int i=1;i<=N;i++)
		cin>>a[i].first>>a[i].second;
	sort(a+1,a+N+1,[]
		(cr<pii>x,cr<pii>y){
			return max(x.first,x.second)>max(y.first,y.second);
		});
	for(int i=1;i<=K;i++){
		cin>>t[i].first;
		t[i].second=i;
	}
	sort(t+1,t+K+1,[]
		(cr<pii>x,cr<pii>y){
			return x.first<y.first;
	});
	Interval_Tree_Sum ITS(K);
	Interval_Tree_Max ITM(K);
	for(int i=1;i<=K;i++)
		ITM.Upd(i,t[i].second);
	lli res=0;
	int pos=K;
	for(int i=1;i<=N;i++){
		while(pos&&t[pos].first>=max(a[i].first,a[i].second)){
			ITS.Upd(t[pos].second,1);
			pos--;
		}
		int l=lower_bound(t+1,t+K+1,pii(min(a[i].first,a[i].second),-1))-t;
		int r=lower_bound(t+1,t+K+1,pii(max(a[i].first,a[i].second),-1))-t-1;
		if(l<=r){
			int maxpos=ITM.Get(l,r);
			int d=ITS.Get(maxpos+1,K);
			if(d&1)
				res+=min(a[i].first,a[i].second);
			else
				res+=max(a[i].first,a[i].second);
		}
		else{
			int d=ITS.Get(1,K);
			if(d&1)
				res+=a[i].second;
			else
				res+=a[i].first;
		}
	}
	cout<<res;
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 13 ms 768 KB Output is correct
15 Correct 27 ms 1536 KB Output is correct
16 Correct 41 ms 1792 KB Output is correct
17 Correct 56 ms 2304 KB Output is correct
18 Correct 56 ms 2176 KB Output is correct
19 Correct 54 ms 2168 KB Output is correct
20 Correct 55 ms 2176 KB Output is correct
21 Correct 46 ms 2168 KB Output is correct
22 Correct 41 ms 2176 KB Output is correct
23 Correct 43 ms 2176 KB Output is correct
24 Correct 46 ms 2176 KB Output is correct
25 Correct 40 ms 2176 KB Output is correct
26 Correct 44 ms 2176 KB Output is correct
27 Correct 47 ms 2176 KB Output is correct
28 Correct 50 ms 2296 KB Output is correct
29 Correct 42 ms 2248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 13 ms 768 KB Output is correct
15 Correct 27 ms 1536 KB Output is correct
16 Correct 41 ms 1792 KB Output is correct
17 Correct 56 ms 2304 KB Output is correct
18 Correct 56 ms 2176 KB Output is correct
19 Correct 54 ms 2168 KB Output is correct
20 Correct 55 ms 2176 KB Output is correct
21 Correct 46 ms 2168 KB Output is correct
22 Correct 41 ms 2176 KB Output is correct
23 Correct 43 ms 2176 KB Output is correct
24 Correct 46 ms 2176 KB Output is correct
25 Correct 40 ms 2176 KB Output is correct
26 Correct 44 ms 2176 KB Output is correct
27 Correct 47 ms 2176 KB Output is correct
28 Correct 50 ms 2296 KB Output is correct
29 Correct 42 ms 2248 KB Output is correct
30 Correct 141 ms 8300 KB Output is correct
31 Correct 184 ms 8696 KB Output is correct
32 Correct 234 ms 8952 KB Output is correct
33 Correct 350 ms 9848 KB Output is correct
34 Correct 83 ms 8184 KB Output is correct
35 Correct 342 ms 9848 KB Output is correct
36 Correct 349 ms 9848 KB Output is correct
37 Correct 345 ms 9848 KB Output is correct
38 Correct 310 ms 9916 KB Output is correct
39 Correct 331 ms 9720 KB Output is correct
40 Correct 260 ms 9848 KB Output is correct
41 Correct 295 ms 9848 KB Output is correct
42 Correct 313 ms 9720 KB Output is correct
43 Correct 187 ms 9720 KB Output is correct
44 Correct 191 ms 9720 KB Output is correct
45 Correct 190 ms 9720 KB Output is correct
46 Correct 255 ms 9800 KB Output is correct
47 Correct 260 ms 9796 KB Output is correct
48 Correct 273 ms 9720 KB Output is correct
49 Correct 259 ms 9800 KB Output is correct