Submission #714777

# Submission time Handle Problem Language Result Execution time Memory
714777 2023-03-25T09:12:35 Z dsyz Fortune Telling 2 (JOI14_fortune_telling2) C++17
100 / 100
312 ms 33872 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAXN (1000005)
bool cmp(pair<ll,ll> a,pair<ll,ll> b){
	return max(a.first,a.second) > max(b.first,b.second);
}

ll fw[MAXN];
void upd(int x,ll nval) {
    for(;x<MAXN;x+=x&(-x)) fw[x]+=nval;
}
ll query(int x) {
    ll res = 0;
    for(;x;x-=x&(-x)) res += fw[x];
    return res;
}
ll sum(int a,int b) {
    return query(b) - query(a-1);
}

struct node{
	int s, e, m;
	int val;
	node *l, *r; 
	node (int S, int E){
		s = S, e = E, m = (s+e)/2;
		val = 0;
		if(s != e){
			l = new node(s, m);
			r = new node(m+1, e);
		}
	}
	void update(int X, int V){ 
		if(s == e) val = V;
		else{
			if(X <= m) l->update(X, V);
			else r->update(X, V);
			val = max(l->val,r->val);
		}
	}
	int query(int S, int E){
		if(s == S && e == E) return val;
		else if(E <= m) return l->query(S, E);
		else if(S >= m+1) return r->query(S, E);
		else return max(l->query(S, m),r->query(m+1, E)); 
	}
} *root;

int main() {
	ios_base::sync_with_stdio(false);cin.tie(0);
	ll N,K;
	cin>>N>>K;
	root = new node(0,K - 1);
	vector<pair<ll,ll> > c;
	vector<pair<ll,ll> > q;
	for(ll i = 0;i < N;i++){
		ll a,b;
		cin>>a>>b;
		c.push_back({a,b});	
	}
	for(ll i = 0;i < K;i++){
		ll t;
		cin>>t;
		q.push_back({t,i});
	}
	sort(c.begin(),c.end(),cmp);
	sort(q.begin(),q.end());
	for(ll i = 0;i < K;i++){
		root -> update(i,q[i].second);
	}
	ll index = K - 1;
	ll ans = 0;
	for(auto u : c){
		ll a = u.first;
		ll b = u.second;
		while(index >= 0 && q[index].first >= max(a,b)){
			upd(q[index].second + 1,1);
			index--;
		}
		ll low = lower_bound(q.begin(),q.end(),make_pair(min(a,b),0ll)) - q.begin();
		ll high = lower_bound(q.begin(),q.end(),make_pair(max(a,b),0ll)) - q.begin();
		ll s = -1;
		if(low <= high - 1){
			s = root -> query(low,high - 1);
		}
		ll num = sum(max(1ll,s + 1),K);
		if(s == -1 && b > a) num++;
		if(num % 2 == 0) ans += max(a,b);
		else ans += min(a,b);
	}
	cout<<ans<<'\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 2 ms 464 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 2 ms 464 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 11 ms 2132 KB Output is correct
15 Correct 25 ms 3808 KB Output is correct
16 Correct 35 ms 5348 KB Output is correct
17 Correct 45 ms 7368 KB Output is correct
18 Correct 52 ms 7396 KB Output is correct
19 Correct 46 ms 7356 KB Output is correct
20 Correct 43 ms 7360 KB Output is correct
21 Correct 41 ms 7368 KB Output is correct
22 Correct 31 ms 6904 KB Output is correct
23 Correct 32 ms 6856 KB Output is correct
24 Correct 35 ms 6848 KB Output is correct
25 Correct 30 ms 6988 KB Output is correct
26 Correct 31 ms 7172 KB Output is correct
27 Correct 36 ms 7368 KB Output is correct
28 Correct 36 ms 7360 KB Output is correct
29 Correct 34 ms 7432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 2 ms 464 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 11 ms 2132 KB Output is correct
15 Correct 25 ms 3808 KB Output is correct
16 Correct 35 ms 5348 KB Output is correct
17 Correct 45 ms 7368 KB Output is correct
18 Correct 52 ms 7396 KB Output is correct
19 Correct 46 ms 7356 KB Output is correct
20 Correct 43 ms 7360 KB Output is correct
21 Correct 41 ms 7368 KB Output is correct
22 Correct 31 ms 6904 KB Output is correct
23 Correct 32 ms 6856 KB Output is correct
24 Correct 35 ms 6848 KB Output is correct
25 Correct 30 ms 6988 KB Output is correct
26 Correct 31 ms 7172 KB Output is correct
27 Correct 36 ms 7368 KB Output is correct
28 Correct 36 ms 7360 KB Output is correct
29 Correct 34 ms 7432 KB Output is correct
30 Correct 96 ms 26292 KB Output is correct
31 Correct 148 ms 27608 KB Output is correct
32 Correct 191 ms 29416 KB Output is correct
33 Correct 312 ms 33712 KB Output is correct
34 Correct 73 ms 25828 KB Output is correct
35 Correct 305 ms 33748 KB Output is correct
36 Correct 293 ms 33704 KB Output is correct
37 Correct 300 ms 33680 KB Output is correct
38 Correct 259 ms 33720 KB Output is correct
39 Correct 247 ms 33768 KB Output is correct
40 Correct 231 ms 33516 KB Output is correct
41 Correct 206 ms 33872 KB Output is correct
42 Correct 209 ms 33756 KB Output is correct
43 Correct 155 ms 33112 KB Output is correct
44 Correct 154 ms 33116 KB Output is correct
45 Correct 168 ms 32892 KB Output is correct
46 Correct 182 ms 32028 KB Output is correct
47 Correct 170 ms 31884 KB Output is correct
48 Correct 196 ms 33704 KB Output is correct
49 Correct 184 ms 33712 KB Output is correct