#pragma GCC optimize("O2")
#include<bits/stdc++.h>
#define fi first
#define se second
#define fo(i,a,b) for(int i=a;i<=b;++i)
#define fod(i,a,b) for(int i=a;i>=b;--i)
#define ii pair<int,int>
#define task "new"
#define ll long long
using namespace std;
const int mod=1e9+7;
const int N=2e5+5;
int n,k,a[N],b[N],t[N];
vector<int>arr,post[N*3],st[(N*3)<<2];
void build(int id,int l,int r)
{
if(l == r)
{
if(post[l].empty())
{
st[id].push_back(-1);
return ;
}
for(auto u : post[l])
st[id].push_back(u);
// if(l == 11)
// {
// for(auto u : st[id])
// cout<<u<<' ';
// cout<<'\n';
// }
//st[id].push_back(post[l] > 0 ? post[l] : -1);
return ;
}
int mid = l + r >> 1;
build(id<<1,l,mid);
build(id<<1|1,mid+1,r);
st[id].resize(st[id<<1].size() + st[id<<1|1].size());
merge(st[id<<1].begin(), st[id<<1].end(),
st[id<<1|1].begin(), st[id<<1|1].end(), st[id].begin());
}
int getmax(int id,int l,int r,int u,int v)
{
if(r < u || v < l)
return 0;
if(u <= l && r <= v)
{
return st[id].back();
}
int mid = l + r >> 1;
return max(getmax(id<<1,l,mid,u,v), getmax(id<<1|1,mid+1,r,u,v));
}
int getnum(int id,int l,int r,int u,int v,int k)
{
if(r < u || v < l)
return 0;
if(u <= l && r <= v)
{
// cout<<l<<' '<<r<<'\n';
// for(auto u : st[id])
// cout<<u<<' ';
// cout<<'\n';
int res = lower_bound(st[id].begin(),st[id].end(),k) - st[id].begin();
return st[id].size() - res;
}
int mid = l + r >> 1;
return getnum(id<<1,l,mid,u,v,k) + getnum(id<<1|1,mid+1,r,u,v,k);
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
if(fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin>>n>>k;
fo(i,1,n)
{
cin>>a[i]>>b[i];
arr.push_back(a[i]);
arr.push_back(b[i]);
}
fo(i,1,k)
{
cin>>t[i];
arr.push_back(t[i]);
}
sort(arr.begin(),arr.end());
int m = arr.size();
fo(i,1,n)
{
a[i] = upper_bound(arr.begin(),arr.end(),a[i]) - arr.begin();
b[i] = upper_bound(arr.begin(),arr.end(),b[i]) - arr.begin();
}
fo(i,1,k)
{
t[i] = upper_bound(arr.begin(),arr.end(),t[i]) - arr.begin();
post[t[i]].push_back(i);
}
build(1,1,m);
ll ans = 0;
fo(i,1,n)
{
bool sw = 0;
if(a[i] > b[i])
swap(a[i], b[i]), sw = 1;
int pos = getmax(1,1,m,a[i],b[i]-1);
int num = getnum(1,1,m,b[i],m,pos);
if(pos > 0)
{
ans += arr[num & 1 ? a[i]-1 : b[i]-1];
//cout<<arr[num & 1 ? a[i]-1 : b[i]-1]<<'\n';
}
else
{
ans += arr[(num + sw) & 1 ? b[i]-1 : a[i]-1];
//cout<<arr[(num + sw) & 1 ? b[i]-1 : a[i]-1]<<'\n';
}
}
cout<<ans;
}
//25
//24
//12
//19
//20
//8
//14
//15
Compilation message
fortune_telling2.cpp: In function 'void build(int, int, int)':
fortune_telling2.cpp:35:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
35 | int mid = l + r >> 1;
| ~~^~~
fortune_telling2.cpp: In function 'int getmax(int, int, int, int, int)':
fortune_telling2.cpp:50:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | int mid = l + r >> 1;
| ~~^~~
fortune_telling2.cpp: In function 'int getnum(int, int, int, int, int, int)':
fortune_telling2.cpp:66:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
66 | int mid = l + r >> 1;
| ~~^~~
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:74:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
72536 KB |
Output is correct |
2 |
Correct |
17 ms |
72540 KB |
Output is correct |
3 |
Correct |
17 ms |
72592 KB |
Output is correct |
4 |
Correct |
17 ms |
72796 KB |
Output is correct |
5 |
Correct |
17 ms |
72796 KB |
Output is correct |
6 |
Correct |
17 ms |
72796 KB |
Output is correct |
7 |
Correct |
18 ms |
72796 KB |
Output is correct |
8 |
Correct |
16 ms |
72676 KB |
Output is correct |
9 |
Correct |
17 ms |
72796 KB |
Output is correct |
10 |
Correct |
16 ms |
72656 KB |
Output is correct |
11 |
Correct |
17 ms |
72796 KB |
Output is correct |
12 |
Correct |
17 ms |
72796 KB |
Output is correct |
13 |
Correct |
17 ms |
72796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
72536 KB |
Output is correct |
2 |
Correct |
17 ms |
72540 KB |
Output is correct |
3 |
Correct |
17 ms |
72592 KB |
Output is correct |
4 |
Correct |
17 ms |
72796 KB |
Output is correct |
5 |
Correct |
17 ms |
72796 KB |
Output is correct |
6 |
Correct |
17 ms |
72796 KB |
Output is correct |
7 |
Correct |
18 ms |
72796 KB |
Output is correct |
8 |
Correct |
16 ms |
72676 KB |
Output is correct |
9 |
Correct |
17 ms |
72796 KB |
Output is correct |
10 |
Correct |
16 ms |
72656 KB |
Output is correct |
11 |
Correct |
17 ms |
72796 KB |
Output is correct |
12 |
Correct |
17 ms |
72796 KB |
Output is correct |
13 |
Correct |
17 ms |
72796 KB |
Output is correct |
14 |
Correct |
34 ms |
76124 KB |
Output is correct |
15 |
Correct |
59 ms |
80088 KB |
Output is correct |
16 |
Correct |
80 ms |
84180 KB |
Output is correct |
17 |
Correct |
106 ms |
88320 KB |
Output is correct |
18 |
Correct |
128 ms |
88572 KB |
Output is correct |
19 |
Correct |
108 ms |
88320 KB |
Output is correct |
20 |
Correct |
109 ms |
88272 KB |
Output is correct |
21 |
Correct |
110 ms |
88236 KB |
Output is correct |
22 |
Correct |
83 ms |
88476 KB |
Output is correct |
23 |
Correct |
83 ms |
88528 KB |
Output is correct |
24 |
Correct |
84 ms |
88496 KB |
Output is correct |
25 |
Correct |
82 ms |
88552 KB |
Output is correct |
26 |
Correct |
83 ms |
86596 KB |
Output is correct |
27 |
Correct |
104 ms |
88564 KB |
Output is correct |
28 |
Correct |
90 ms |
88388 KB |
Output is correct |
29 |
Correct |
106 ms |
88272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
72536 KB |
Output is correct |
2 |
Correct |
17 ms |
72540 KB |
Output is correct |
3 |
Correct |
17 ms |
72592 KB |
Output is correct |
4 |
Correct |
17 ms |
72796 KB |
Output is correct |
5 |
Correct |
17 ms |
72796 KB |
Output is correct |
6 |
Correct |
17 ms |
72796 KB |
Output is correct |
7 |
Correct |
18 ms |
72796 KB |
Output is correct |
8 |
Correct |
16 ms |
72676 KB |
Output is correct |
9 |
Correct |
17 ms |
72796 KB |
Output is correct |
10 |
Correct |
16 ms |
72656 KB |
Output is correct |
11 |
Correct |
17 ms |
72796 KB |
Output is correct |
12 |
Correct |
17 ms |
72796 KB |
Output is correct |
13 |
Correct |
17 ms |
72796 KB |
Output is correct |
14 |
Correct |
34 ms |
76124 KB |
Output is correct |
15 |
Correct |
59 ms |
80088 KB |
Output is correct |
16 |
Correct |
80 ms |
84180 KB |
Output is correct |
17 |
Correct |
106 ms |
88320 KB |
Output is correct |
18 |
Correct |
128 ms |
88572 KB |
Output is correct |
19 |
Correct |
108 ms |
88320 KB |
Output is correct |
20 |
Correct |
109 ms |
88272 KB |
Output is correct |
21 |
Correct |
110 ms |
88236 KB |
Output is correct |
22 |
Correct |
83 ms |
88476 KB |
Output is correct |
23 |
Correct |
83 ms |
88528 KB |
Output is correct |
24 |
Correct |
84 ms |
88496 KB |
Output is correct |
25 |
Correct |
82 ms |
88552 KB |
Output is correct |
26 |
Correct |
83 ms |
86596 KB |
Output is correct |
27 |
Correct |
104 ms |
88564 KB |
Output is correct |
28 |
Correct |
90 ms |
88388 KB |
Output is correct |
29 |
Correct |
106 ms |
88272 KB |
Output is correct |
30 |
Correct |
201 ms |
106004 KB |
Output is correct |
31 |
Correct |
291 ms |
116200 KB |
Output is correct |
32 |
Correct |
461 ms |
129584 KB |
Output is correct |
33 |
Correct |
744 ms |
157944 KB |
Output is correct |
34 |
Correct |
162 ms |
105040 KB |
Output is correct |
35 |
Correct |
767 ms |
162752 KB |
Output is correct |
36 |
Correct |
744 ms |
162384 KB |
Output is correct |
37 |
Correct |
735 ms |
162416 KB |
Output is correct |
38 |
Correct |
749 ms |
162904 KB |
Output is correct |
39 |
Correct |
776 ms |
164088 KB |
Output is correct |
40 |
Correct |
770 ms |
162560 KB |
Output is correct |
41 |
Correct |
780 ms |
163084 KB |
Output is correct |
42 |
Correct |
781 ms |
163296 KB |
Output is correct |
43 |
Correct |
446 ms |
161876 KB |
Output is correct |
44 |
Correct |
440 ms |
162752 KB |
Output is correct |
45 |
Correct |
466 ms |
162848 KB |
Output is correct |
46 |
Correct |
507 ms |
163336 KB |
Output is correct |
47 |
Correct |
472 ms |
163912 KB |
Output is correct |
48 |
Correct |
651 ms |
164136 KB |
Output is correct |
49 |
Correct |
601 ms |
163520 KB |
Output is correct |