#include<bits/stdc++.h>
#define f first
#define int long long
#define s second
using namespace std;
const int N=2e5+5,mod=1e9+7;
int t[N],tree_[N],tree[4*N],a[N],b[N],n,m;
vector<pair<int,int> > q,V;
set<pair<int,int> > s;
void update(int ind){
for(ind;ind>=1;ind-=ind&(-ind)){
tree_[ind]++;
}
}
int getans(int ind){
int pas=0;
for(ind;ind<=m;ind+=ind&(-ind)){
pas+=tree_[ind];
}
return pas;
}
void update(int u,int ind,int l,int r,int val){
if(l>ind || r<ind) return;
if(l==r) {
tree[u]=val;
return;
}
int mid=(l+r)/2;
update(2*u,ind,l,mid,val);
update(2*u+1,ind,mid+1,r,val);
tree[u]=max(tree[2*u],tree[2*u+1]);
}
int getans(int u,int start,int end,int l,int r){
if(l>end || r<start) return 0;
if(start<=l && r<=end) return tree[u];
int mid=(l+r)/2;
return max(getans(2*u,start,end,l,mid),getans(2*u+1,start,end,mid+1,r));
}
main(){
// t=1;
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i]; cin>>b[i];V.push_back({max(b[i],a[i]),i});
}
for(int i=1;i<=m;i++){
cin>>t[i];
q.push_back({t[i],i});
}
sort(V.rbegin(),V.rend());
sort(q.rbegin(),q.rend());
for(int i=0;i<q.size();i++){
s.insert({q[i].f,i+1});
update(1,i+1,1,m,q[i].s);
}
int l = -1,ans=0;
for(int i=0;i<V.size();i++){
while(l+1<q.size() && q[l+1].f>=V[i].f) l++,update(q[l].s);
int lst = 1;
if(s.upper_bound({min(a[V[i].s],b[V[i].s]),-1})!=s.end() &&(*s.upper_bound({min(a[V[i].s],b[V[i].s]),-1})).s>l+1 ){
lst =getans(1,l+2,(*s.upper_bound({min(a[V[i].s],b[V[i].s]),-1})).s,1,m);
if(a[V[i].s] < b[V[i].s]) swap(a[V[i].s],b[V[i].s]);
}
//cout<<V[i].s<<" "<<lst<<" "<<getans(lst)<<endl;
if(getans(lst)%2) ans+=b[V[i].s];else ans += a[V[i].s];
}
cout<<ans;
}
Compilation message
fortune_telling2.cpp: In function 'void update(long long int)':
fortune_telling2.cpp:11:6: warning: statement has no effect [-Wunused-value]
11 | for(ind;ind>=1;ind-=ind&(-ind)){
| ^~~
fortune_telling2.cpp: In function 'long long int getans(long long int)':
fortune_telling2.cpp:17:6: warning: statement has no effect [-Wunused-value]
17 | for(ind;ind<=m;ind+=ind&(-ind)){
| ^~~
fortune_telling2.cpp: At global scope:
fortune_telling2.cpp:39:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
39 | main(){
| ^
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:53:15: 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]
53 | for(int i=0;i<q.size();i++){
| ~^~~~~~~~~
fortune_telling2.cpp:58:15: 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]
58 | for(int i=0;i<V.size();i++){
| ~^~~~~~~~~
fortune_telling2.cpp:59:12: 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]
59 | while(l+1<q.size() && q[l+1].f>=V[i].f) l++,update(q[l].s);
| ~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
3 ms |
460 KB |
Output is correct |
5 |
Correct |
3 ms |
460 KB |
Output is correct |
6 |
Correct |
3 ms |
460 KB |
Output is correct |
7 |
Correct |
3 ms |
460 KB |
Output is correct |
8 |
Correct |
3 ms |
448 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
3 ms |
460 KB |
Output is correct |
13 |
Correct |
3 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
3 ms |
460 KB |
Output is correct |
5 |
Correct |
3 ms |
460 KB |
Output is correct |
6 |
Correct |
3 ms |
460 KB |
Output is correct |
7 |
Correct |
3 ms |
460 KB |
Output is correct |
8 |
Correct |
3 ms |
448 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
3 ms |
460 KB |
Output is correct |
13 |
Correct |
3 ms |
460 KB |
Output is correct |
14 |
Correct |
27 ms |
2156 KB |
Output is correct |
15 |
Correct |
60 ms |
3892 KB |
Output is correct |
16 |
Correct |
85 ms |
5384 KB |
Output is correct |
17 |
Correct |
116 ms |
7416 KB |
Output is correct |
18 |
Correct |
119 ms |
7408 KB |
Output is correct |
19 |
Correct |
117 ms |
7544 KB |
Output is correct |
20 |
Correct |
118 ms |
7516 KB |
Output is correct |
21 |
Correct |
108 ms |
7384 KB |
Output is correct |
22 |
Correct |
76 ms |
6964 KB |
Output is correct |
23 |
Correct |
82 ms |
6984 KB |
Output is correct |
24 |
Correct |
95 ms |
6964 KB |
Output is correct |
25 |
Correct |
76 ms |
6968 KB |
Output is correct |
26 |
Correct |
85 ms |
7208 KB |
Output is correct |
27 |
Correct |
93 ms |
7516 KB |
Output is correct |
28 |
Correct |
104 ms |
7540 KB |
Output is correct |
29 |
Correct |
90 ms |
7476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
3 ms |
460 KB |
Output is correct |
5 |
Correct |
3 ms |
460 KB |
Output is correct |
6 |
Correct |
3 ms |
460 KB |
Output is correct |
7 |
Correct |
3 ms |
460 KB |
Output is correct |
8 |
Correct |
3 ms |
448 KB |
Output is correct |
9 |
Correct |
2 ms |
460 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
3 ms |
460 KB |
Output is correct |
13 |
Correct |
3 ms |
460 KB |
Output is correct |
14 |
Correct |
27 ms |
2156 KB |
Output is correct |
15 |
Correct |
60 ms |
3892 KB |
Output is correct |
16 |
Correct |
85 ms |
5384 KB |
Output is correct |
17 |
Correct |
116 ms |
7416 KB |
Output is correct |
18 |
Correct |
119 ms |
7408 KB |
Output is correct |
19 |
Correct |
117 ms |
7544 KB |
Output is correct |
20 |
Correct |
118 ms |
7516 KB |
Output is correct |
21 |
Correct |
108 ms |
7384 KB |
Output is correct |
22 |
Correct |
76 ms |
6964 KB |
Output is correct |
23 |
Correct |
82 ms |
6984 KB |
Output is correct |
24 |
Correct |
95 ms |
6964 KB |
Output is correct |
25 |
Correct |
76 ms |
6968 KB |
Output is correct |
26 |
Correct |
85 ms |
7208 KB |
Output is correct |
27 |
Correct |
93 ms |
7516 KB |
Output is correct |
28 |
Correct |
104 ms |
7540 KB |
Output is correct |
29 |
Correct |
90 ms |
7476 KB |
Output is correct |
30 |
Correct |
238 ms |
25540 KB |
Output is correct |
31 |
Correct |
361 ms |
27616 KB |
Output is correct |
32 |
Correct |
479 ms |
30120 KB |
Output is correct |
33 |
Correct |
764 ms |
35260 KB |
Output is correct |
34 |
Correct |
211 ms |
25044 KB |
Output is correct |
35 |
Correct |
737 ms |
35272 KB |
Output is correct |
36 |
Correct |
728 ms |
35232 KB |
Output is correct |
37 |
Correct |
757 ms |
35372 KB |
Output is correct |
38 |
Correct |
688 ms |
35368 KB |
Output is correct |
39 |
Correct |
634 ms |
35384 KB |
Output is correct |
40 |
Correct |
600 ms |
34948 KB |
Output is correct |
41 |
Correct |
583 ms |
35288 KB |
Output is correct |
42 |
Correct |
653 ms |
35300 KB |
Output is correct |
43 |
Correct |
516 ms |
33532 KB |
Output is correct |
44 |
Correct |
519 ms |
33532 KB |
Output is correct |
45 |
Correct |
490 ms |
33952 KB |
Output is correct |
46 |
Correct |
429 ms |
33388 KB |
Output is correct |
47 |
Correct |
413 ms |
33184 KB |
Output is correct |
48 |
Correct |
517 ms |
35424 KB |
Output is correct |
49 |
Correct |
513 ms |
35324 KB |
Output is correct |