#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);
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)
{
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];
}
else
{
ans += arr[(num + sw) & 1 ? b[i]-1 : a[i]-1];
}
}
cout<<ans;
}
Compilation message
fortune_telling2.cpp: In function 'void build(int, int, int)':
fortune_telling2.cpp:28:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
28 | int mid = l + r >> 1;
| ~~^~~
fortune_telling2.cpp: In function 'int getmax(int, int, int, int, int)':
fortune_telling2.cpp:43:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
43 | int mid = l + r >> 1;
| ~~^~~
fortune_telling2.cpp: In function 'int getnum(int, int, int, int, int, int)':
fortune_telling2.cpp:55:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
55 | int mid = l + r >> 1;
| ~~^~~
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:63:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
72624 KB |
Output is correct |
2 |
Correct |
20 ms |
72540 KB |
Output is correct |
3 |
Correct |
19 ms |
72796 KB |
Output is correct |
4 |
Correct |
20 ms |
72792 KB |
Output is correct |
5 |
Correct |
20 ms |
72796 KB |
Output is correct |
6 |
Correct |
19 ms |
72796 KB |
Output is correct |
7 |
Correct |
20 ms |
72908 KB |
Output is correct |
8 |
Correct |
19 ms |
72796 KB |
Output is correct |
9 |
Correct |
19 ms |
72792 KB |
Output is correct |
10 |
Correct |
19 ms |
72796 KB |
Output is correct |
11 |
Correct |
20 ms |
72588 KB |
Output is correct |
12 |
Correct |
19 ms |
72792 KB |
Output is correct |
13 |
Correct |
20 ms |
72672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
72624 KB |
Output is correct |
2 |
Correct |
20 ms |
72540 KB |
Output is correct |
3 |
Correct |
19 ms |
72796 KB |
Output is correct |
4 |
Correct |
20 ms |
72792 KB |
Output is correct |
5 |
Correct |
20 ms |
72796 KB |
Output is correct |
6 |
Correct |
19 ms |
72796 KB |
Output is correct |
7 |
Correct |
20 ms |
72908 KB |
Output is correct |
8 |
Correct |
19 ms |
72796 KB |
Output is correct |
9 |
Correct |
19 ms |
72792 KB |
Output is correct |
10 |
Correct |
19 ms |
72796 KB |
Output is correct |
11 |
Correct |
20 ms |
72588 KB |
Output is correct |
12 |
Correct |
19 ms |
72792 KB |
Output is correct |
13 |
Correct |
20 ms |
72672 KB |
Output is correct |
14 |
Correct |
36 ms |
76376 KB |
Output is correct |
15 |
Correct |
58 ms |
80560 KB |
Output is correct |
16 |
Correct |
86 ms |
84944 KB |
Output is correct |
17 |
Correct |
120 ms |
89552 KB |
Output is correct |
18 |
Correct |
112 ms |
89560 KB |
Output is correct |
19 |
Correct |
114 ms |
89300 KB |
Output is correct |
20 |
Correct |
112 ms |
89552 KB |
Output is correct |
21 |
Correct |
103 ms |
89296 KB |
Output is correct |
22 |
Correct |
86 ms |
89044 KB |
Output is correct |
23 |
Correct |
85 ms |
89352 KB |
Output is correct |
24 |
Correct |
107 ms |
89624 KB |
Output is correct |
25 |
Correct |
85 ms |
89040 KB |
Output is correct |
26 |
Correct |
89 ms |
87528 KB |
Output is correct |
27 |
Correct |
102 ms |
89300 KB |
Output is correct |
28 |
Correct |
108 ms |
89360 KB |
Output is correct |
29 |
Correct |
108 ms |
89296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
72624 KB |
Output is correct |
2 |
Correct |
20 ms |
72540 KB |
Output is correct |
3 |
Correct |
19 ms |
72796 KB |
Output is correct |
4 |
Correct |
20 ms |
72792 KB |
Output is correct |
5 |
Correct |
20 ms |
72796 KB |
Output is correct |
6 |
Correct |
19 ms |
72796 KB |
Output is correct |
7 |
Correct |
20 ms |
72908 KB |
Output is correct |
8 |
Correct |
19 ms |
72796 KB |
Output is correct |
9 |
Correct |
19 ms |
72792 KB |
Output is correct |
10 |
Correct |
19 ms |
72796 KB |
Output is correct |
11 |
Correct |
20 ms |
72588 KB |
Output is correct |
12 |
Correct |
19 ms |
72792 KB |
Output is correct |
13 |
Correct |
20 ms |
72672 KB |
Output is correct |
14 |
Correct |
36 ms |
76376 KB |
Output is correct |
15 |
Correct |
58 ms |
80560 KB |
Output is correct |
16 |
Correct |
86 ms |
84944 KB |
Output is correct |
17 |
Correct |
120 ms |
89552 KB |
Output is correct |
18 |
Correct |
112 ms |
89560 KB |
Output is correct |
19 |
Correct |
114 ms |
89300 KB |
Output is correct |
20 |
Correct |
112 ms |
89552 KB |
Output is correct |
21 |
Correct |
103 ms |
89296 KB |
Output is correct |
22 |
Correct |
86 ms |
89044 KB |
Output is correct |
23 |
Correct |
85 ms |
89352 KB |
Output is correct |
24 |
Correct |
107 ms |
89624 KB |
Output is correct |
25 |
Correct |
85 ms |
89040 KB |
Output is correct |
26 |
Correct |
89 ms |
87528 KB |
Output is correct |
27 |
Correct |
102 ms |
89300 KB |
Output is correct |
28 |
Correct |
108 ms |
89360 KB |
Output is correct |
29 |
Correct |
108 ms |
89296 KB |
Output is correct |
30 |
Correct |
183 ms |
108188 KB |
Output is correct |
31 |
Correct |
283 ms |
119096 KB |
Output is correct |
32 |
Correct |
434 ms |
133572 KB |
Output is correct |
33 |
Correct |
780 ms |
162360 KB |
Output is correct |
34 |
Correct |
152 ms |
105016 KB |
Output is correct |
35 |
Correct |
758 ms |
162660 KB |
Output is correct |
36 |
Correct |
778 ms |
162540 KB |
Output is correct |
37 |
Correct |
778 ms |
162516 KB |
Output is correct |
38 |
Correct |
738 ms |
162420 KB |
Output is correct |
39 |
Correct |
755 ms |
162752 KB |
Output is correct |
40 |
Correct |
695 ms |
162240 KB |
Output is correct |
41 |
Correct |
767 ms |
163576 KB |
Output is correct |
42 |
Correct |
790 ms |
162340 KB |
Output is correct |
43 |
Correct |
439 ms |
161784 KB |
Output is correct |
44 |
Correct |
433 ms |
161836 KB |
Output is correct |
45 |
Correct |
434 ms |
162748 KB |
Output is correct |
46 |
Correct |
472 ms |
162720 KB |
Output is correct |
47 |
Correct |
473 ms |
163476 KB |
Output is correct |
48 |
Correct |
625 ms |
162496 KB |
Output is correct |
49 |
Correct |
565 ms |
163972 KB |
Output is correct |