# include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define int long long
#define pii pair <int, int>
using namespace std;
const int N = 3e5 + 5;
int n,k,a[N],b[N],cnt, x[N],curj,aa,bb,mn,mx,frmt,frm,idd,ans;
vector <pii>v;
vector <pii> v1,v2;
int tree[4*N],tree1[N];
multiset <int> ms;
multiset <int> :: iterator it;
map <int, int > idx;
void upd(int idx, int val) {
for (int i = idx; i < N; i += i&(-i)) {
tree1[i] += val;
}
}
int get1(int idx) {
int pas = 0;
for (int i = idx; i > 0 ; i -= i&(-i)) {
pas += tree1[i];
}
return pas;
}
int get(int le, int ri) {
return get1(ri) - get1(le - 1);
}
void update(int node, int le, int ri, int idx, int val) {
if (le > idx || ri < idx) {
return ;
}
if (le == ri) {
tree[node] = max(tree[node],val);
return ;
}
int mid = (le + ri) / 2;
update(2*node, le, mid,idx,val);
update(2*node + 1, mid + 1, ri, idx, val);
tree[node] = max(tree[2*node],tree[2*node + 1]);
}
int getans(int node, int le, int ri, int start, int end) {
if (le > end || ri < start) return 0;
if (le >= start && ri <= end) {
return tree[node];
}
int mid = (le + ri) / 2;
return max(getans(2*node, le, mid, start,end), getans(2*node + 1, mid + 1, ri, start,end));
}
main() {
cin>>n>>k;
for (int i = 1; i <= n; i++) {
cin>>a[i]>>b[i];
v.pb({max(a[i],b[i]),i});
}
for (int i = 1; i <= k; i++) {
cin>>x[i];
v1.pb({x[i],i});
v2.pb({x[i],i});
ms.insert(x[i]);
}
ms.insert(1e9 + 1);
sort(v.begin(),v.end());reverse(v.begin(),v.end());
sort(v1.begin(),v1.end()); reverse(v1.begin(), v1.end());
v2.pb({1e9 + 1, 1});
sort(v2.begin(),v2.end());
idx[v2[0].f] = 1;
cnt = 1;
for (int i = 1; i < v2.size(); i++) {
if (v2[i].f != v2[i - 1].f) cnt++;
idx[v2[i].f] = cnt;
}
for (int i = 0; i < (int)v2.size() - 1; i++) {
update(1,1,k,idx[v2[i].f], v2[i].s);
}
curj = 0;
for (int i = 0; i < v.size(); i++) {
while(curj < v1.size() && v1[curj].f >= v[i].f) {
upd(v1[curj].s,1);
curj++;
}
aa = a[v[i].s]; bb = b[v[i].s]; mn = min(aa,bb); mx = max(aa,bb);
it = ms.lower_bound(mn); frmt = idx[*it];
it = ms.lower_bound(mx); frm = idx[(*it)] - 1;
idd = 0;
if (frmt <= frm) {
idd = getans(1,1,k,frmt,frm);
if (idd != 0) aa = mx, bb = mn;
}
if (get(idd + 1, k) % 2 == 1) swap(aa,bb);
ans += aa;
}
cout<<ans<<"\n";
}
Compilation message
fortune_telling2.cpp:52:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
52 | main() {
| ^~~~
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:71:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for (int i = 1; i < v2.size(); i++) {
| ~~^~~~~~~~~~~
fortune_telling2.cpp:79:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for (int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
fortune_telling2.cpp:80:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | while(curj < v1.size() && v1[curj].f >= v[i].f) {
| ~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
464 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
528 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
592 KB |
Output is correct |
8 |
Correct |
3 ms |
592 KB |
Output is correct |
9 |
Correct |
2 ms |
464 KB |
Output is correct |
10 |
Correct |
2 ms |
464 KB |
Output is correct |
11 |
Correct |
3 ms |
592 KB |
Output is correct |
12 |
Correct |
3 ms |
592 KB |
Output is correct |
13 |
Correct |
3 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
464 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
528 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
592 KB |
Output is correct |
8 |
Correct |
3 ms |
592 KB |
Output is correct |
9 |
Correct |
2 ms |
464 KB |
Output is correct |
10 |
Correct |
2 ms |
464 KB |
Output is correct |
11 |
Correct |
3 ms |
592 KB |
Output is correct |
12 |
Correct |
3 ms |
592 KB |
Output is correct |
13 |
Correct |
3 ms |
592 KB |
Output is correct |
14 |
Correct |
25 ms |
2764 KB |
Output is correct |
15 |
Correct |
52 ms |
5212 KB |
Output is correct |
16 |
Correct |
84 ms |
7296 KB |
Output is correct |
17 |
Correct |
117 ms |
10108 KB |
Output is correct |
18 |
Correct |
122 ms |
10060 KB |
Output is correct |
19 |
Correct |
109 ms |
10068 KB |
Output is correct |
20 |
Correct |
106 ms |
10044 KB |
Output is correct |
21 |
Correct |
103 ms |
10032 KB |
Output is correct |
22 |
Correct |
86 ms |
8832 KB |
Output is correct |
23 |
Correct |
80 ms |
8572 KB |
Output is correct |
24 |
Correct |
80 ms |
8324 KB |
Output is correct |
25 |
Correct |
79 ms |
9060 KB |
Output is correct |
26 |
Correct |
83 ms |
9736 KB |
Output is correct |
27 |
Correct |
95 ms |
9992 KB |
Output is correct |
28 |
Correct |
98 ms |
10080 KB |
Output is correct |
29 |
Correct |
90 ms |
9960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
464 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
528 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
592 KB |
Output is correct |
8 |
Correct |
3 ms |
592 KB |
Output is correct |
9 |
Correct |
2 ms |
464 KB |
Output is correct |
10 |
Correct |
2 ms |
464 KB |
Output is correct |
11 |
Correct |
3 ms |
592 KB |
Output is correct |
12 |
Correct |
3 ms |
592 KB |
Output is correct |
13 |
Correct |
3 ms |
592 KB |
Output is correct |
14 |
Correct |
25 ms |
2764 KB |
Output is correct |
15 |
Correct |
52 ms |
5212 KB |
Output is correct |
16 |
Correct |
84 ms |
7296 KB |
Output is correct |
17 |
Correct |
117 ms |
10108 KB |
Output is correct |
18 |
Correct |
122 ms |
10060 KB |
Output is correct |
19 |
Correct |
109 ms |
10068 KB |
Output is correct |
20 |
Correct |
106 ms |
10044 KB |
Output is correct |
21 |
Correct |
103 ms |
10032 KB |
Output is correct |
22 |
Correct |
86 ms |
8832 KB |
Output is correct |
23 |
Correct |
80 ms |
8572 KB |
Output is correct |
24 |
Correct |
80 ms |
8324 KB |
Output is correct |
25 |
Correct |
79 ms |
9060 KB |
Output is correct |
26 |
Correct |
83 ms |
9736 KB |
Output is correct |
27 |
Correct |
95 ms |
9992 KB |
Output is correct |
28 |
Correct |
98 ms |
10080 KB |
Output is correct |
29 |
Correct |
90 ms |
9960 KB |
Output is correct |
30 |
Correct |
333 ms |
38104 KB |
Output is correct |
31 |
Correct |
451 ms |
40436 KB |
Output is correct |
32 |
Correct |
573 ms |
42752 KB |
Output is correct |
33 |
Correct |
839 ms |
47948 KB |
Output is correct |
34 |
Correct |
277 ms |
37776 KB |
Output is correct |
35 |
Correct |
841 ms |
47836 KB |
Output is correct |
36 |
Correct |
837 ms |
47876 KB |
Output is correct |
37 |
Correct |
844 ms |
47856 KB |
Output is correct |
38 |
Correct |
730 ms |
47860 KB |
Output is correct |
39 |
Correct |
695 ms |
47788 KB |
Output is correct |
40 |
Correct |
625 ms |
47632 KB |
Output is correct |
41 |
Correct |
598 ms |
47916 KB |
Output is correct |
42 |
Correct |
642 ms |
47852 KB |
Output is correct |
43 |
Correct |
517 ms |
46132 KB |
Output is correct |
44 |
Correct |
536 ms |
46276 KB |
Output is correct |
45 |
Correct |
520 ms |
46628 KB |
Output is correct |
46 |
Correct |
507 ms |
41544 KB |
Output is correct |
47 |
Correct |
498 ms |
39816 KB |
Output is correct |
48 |
Correct |
519 ms |
47792 KB |
Output is correct |
49 |
Correct |
547 ms |
47912 KB |
Output is correct |