#include <bits/stdc++.h>
using namespace std;
#define ld long double
#define ll long long
#define endl "\n"
const int N = 5e5,K = 20;
int a[N + 1],b[N + 1],sign[N + 1];
map<int,int> mp;
vector<int> query;
vector<pair<int,int>> seg[4*N + 1];
void buildST(int id,int l,int r) {
if(l == r) {
seg[id] = {{query[l],1}};
return;
}
int mid = (l + r)/2;
buildST(id*2,l,mid);
buildST(id*2 + 1,mid + 1,r);
seg[id] = seg[id*2];
for(auto u : seg[id*2 + 1]) {
seg[id].push_back(u);
}
sort(seg[id].begin(),seg[id].end());
for(int i = 0;i < seg[id].size();i++) {
seg[id][i].second = i + 1;
}
}
int queryST(int id,int l,int r,int u,int v,int val) {
if(v < l || u > r) return 0;
if(u <= l && v >= r) {
auto it = lower_bound(seg[id].begin(),seg[id].end(),make_pair(val,(int)-1));
if(it == seg[id].end()) return 0;
return (seg[id].size()) - (*it).second + 1;
}
int mid = (l + r)/2;
return queryST(id*2,l,mid,u,v,val) + queryST(id*2 + 1,mid + 1,r,u,v,val);
}
int val[N + 1],lst[N + 1],cnt = 0;
int sparse[N + 1][K + 1],lg[N + 1];
void buildSP() {
for(int i = 2;i <= N;i++) {
lg[i] = lg[i/2] + 1;
}
for(int i = 0;i <= cnt;i++) {
sparse[i][0] = lst[i];
}
for(int j = 1;j <= K;j++) {
for(int i = 0;i + (1<<j) <= cnt;i++) {
sparse[i][j] = max(sparse[i][j - 1],sparse[i + (1<<(j - 1))][j - 1]);
}
}
}
int querySP(int l,int r) {
int len = lg[r - l + 1];
return max(sparse[l][len],sparse[r - (1<<len) + 1][len]);
}
signed main() {
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n,k;
cin>>n>>k;
for(int i = 1;i <= n;i++) {
cin>>a[i]>>b[i];
sign[i] = 0;
if(a[i] > b[i]) {
sign[i] = 1;
swap(a[i],b[i]);
}
mp[a[i]] = -1;
mp[b[i]] = -1;
}
for(int i = 0;i < k;i++) {
int x;
cin>>x;
mp[x] = -1;
query.push_back(x);
}
for(auto u : mp) {
mp[u.first] = cnt;
val[cnt] = u.first;
cnt++;
}
for(int i = 1;i <= n;i++) {
a[i] = mp[a[i]];
b[i] = mp[b[i]];
}
for(int i = 0;i < query.size();i++) {
query[i] = mp[query[i]];
}
for(int i = 0;i <= cnt;i++) {
lst[i] = -1;
}
for(int i = 0;i < query.size();i++) {
lst[query[i]] = max(lst[query[i]],i);
}
ll res = 0;
buildSP();
buildST(1,0,query.size() - 1);
// for(int i = 1;i <= n;i++) {
// cout<<a[i]<<" "<<b[i]<<endl;
// }
// for(auto u : query) {
// cout<<u<<endl;
// }
for(int i = 1;i <= n;i++) {
if(a[i] == b[i]) {
res += val[a[i]];
continue;
}
// cout<<a[i]<<" "<<b[i]<<endl;
int nw = querySP(a[i],b[i] - 1);
int w = sign[i];
if(nw != -1) w = 1;
int cnt = queryST(1,0,query.size() - 1,nw + 1,query.size() - 1,b[i]);
w ^= (cnt%2);
if(w == 0) {
res += (ll)val[a[i]];
// cout<<val[a[i]]<<" "<<cnt<<" "<<nw<<endl;
} else {
res += (ll)val[b[i]];
// cout<<val[b[i]]<<" "<<cnt<<" "<<nw<<endl;
}
}
cout<<res<<endl;
return 0;
}
Compilation message
fortune_telling2.cpp: In function 'void buildST(int, int, int)':
fortune_telling2.cpp:32:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(int i = 0;i < seg[id].size();i++) {
| ~~^~~~~~~~~~~~~~~~
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:105:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for(int i = 0;i < query.size();i++) {
| ~~^~~~~~~~~~~~~~
fortune_telling2.cpp:112:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
112 | for(int i = 0;i < query.size();i++) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
49492 KB |
Output is correct |
2 |
Correct |
28 ms |
49612 KB |
Output is correct |
3 |
Correct |
27 ms |
49748 KB |
Output is correct |
4 |
Correct |
28 ms |
49816 KB |
Output is correct |
5 |
Correct |
26 ms |
49748 KB |
Output is correct |
6 |
Correct |
26 ms |
49832 KB |
Output is correct |
7 |
Correct |
26 ms |
49744 KB |
Output is correct |
8 |
Correct |
27 ms |
49820 KB |
Output is correct |
9 |
Correct |
26 ms |
49748 KB |
Output is correct |
10 |
Correct |
25 ms |
49572 KB |
Output is correct |
11 |
Correct |
27 ms |
49620 KB |
Output is correct |
12 |
Correct |
25 ms |
49616 KB |
Output is correct |
13 |
Correct |
27 ms |
49664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
49492 KB |
Output is correct |
2 |
Correct |
28 ms |
49612 KB |
Output is correct |
3 |
Correct |
27 ms |
49748 KB |
Output is correct |
4 |
Correct |
28 ms |
49816 KB |
Output is correct |
5 |
Correct |
26 ms |
49748 KB |
Output is correct |
6 |
Correct |
26 ms |
49832 KB |
Output is correct |
7 |
Correct |
26 ms |
49744 KB |
Output is correct |
8 |
Correct |
27 ms |
49820 KB |
Output is correct |
9 |
Correct |
26 ms |
49748 KB |
Output is correct |
10 |
Correct |
25 ms |
49572 KB |
Output is correct |
11 |
Correct |
27 ms |
49620 KB |
Output is correct |
12 |
Correct |
25 ms |
49616 KB |
Output is correct |
13 |
Correct |
27 ms |
49664 KB |
Output is correct |
14 |
Correct |
50 ms |
55180 KB |
Output is correct |
15 |
Correct |
80 ms |
61224 KB |
Output is correct |
16 |
Correct |
121 ms |
67260 KB |
Output is correct |
17 |
Correct |
173 ms |
73336 KB |
Output is correct |
18 |
Correct |
187 ms |
73284 KB |
Output is correct |
19 |
Correct |
167 ms |
73356 KB |
Output is correct |
20 |
Correct |
188 ms |
73288 KB |
Output is correct |
21 |
Correct |
188 ms |
73360 KB |
Output is correct |
22 |
Correct |
124 ms |
67860 KB |
Output is correct |
23 |
Correct |
113 ms |
65212 KB |
Output is correct |
24 |
Correct |
101 ms |
63060 KB |
Output is correct |
25 |
Correct |
128 ms |
69372 KB |
Output is correct |
26 |
Correct |
114 ms |
66856 KB |
Output is correct |
27 |
Correct |
135 ms |
67832 KB |
Output is correct |
28 |
Correct |
119 ms |
67908 KB |
Output is correct |
29 |
Correct |
151 ms |
70628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
49492 KB |
Output is correct |
2 |
Correct |
28 ms |
49612 KB |
Output is correct |
3 |
Correct |
27 ms |
49748 KB |
Output is correct |
4 |
Correct |
28 ms |
49816 KB |
Output is correct |
5 |
Correct |
26 ms |
49748 KB |
Output is correct |
6 |
Correct |
26 ms |
49832 KB |
Output is correct |
7 |
Correct |
26 ms |
49744 KB |
Output is correct |
8 |
Correct |
27 ms |
49820 KB |
Output is correct |
9 |
Correct |
26 ms |
49748 KB |
Output is correct |
10 |
Correct |
25 ms |
49572 KB |
Output is correct |
11 |
Correct |
27 ms |
49620 KB |
Output is correct |
12 |
Correct |
25 ms |
49616 KB |
Output is correct |
13 |
Correct |
27 ms |
49664 KB |
Output is correct |
14 |
Correct |
50 ms |
55180 KB |
Output is correct |
15 |
Correct |
80 ms |
61224 KB |
Output is correct |
16 |
Correct |
121 ms |
67260 KB |
Output is correct |
17 |
Correct |
173 ms |
73336 KB |
Output is correct |
18 |
Correct |
187 ms |
73284 KB |
Output is correct |
19 |
Correct |
167 ms |
73356 KB |
Output is correct |
20 |
Correct |
188 ms |
73288 KB |
Output is correct |
21 |
Correct |
188 ms |
73360 KB |
Output is correct |
22 |
Correct |
124 ms |
67860 KB |
Output is correct |
23 |
Correct |
113 ms |
65212 KB |
Output is correct |
24 |
Correct |
101 ms |
63060 KB |
Output is correct |
25 |
Correct |
128 ms |
69372 KB |
Output is correct |
26 |
Correct |
114 ms |
66856 KB |
Output is correct |
27 |
Correct |
135 ms |
67832 KB |
Output is correct |
28 |
Correct |
119 ms |
67908 KB |
Output is correct |
29 |
Correct |
151 ms |
70628 KB |
Output is correct |
30 |
Correct |
499 ms |
118772 KB |
Output is correct |
31 |
Correct |
659 ms |
130192 KB |
Output is correct |
32 |
Correct |
821 ms |
144468 KB |
Output is correct |
33 |
Runtime error |
1008 ms |
254428 KB |
Execution killed with signal 11 |
34 |
Halted |
0 ms |
0 KB |
- |