#include <bits/stdc++.h>
using namespace std;
#define int long long
#define in ({int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';n?-x:x;})
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r){return l+rng()%(r-l+1);}
#define fasty ios_base::sync_with_stdio(0),cin.tie(0);
#define forinc(a,b,c) for(int a=b,_c=c;a<=_c;++a)
#define fordec(a,b,c) for(int a=b,_c=c;a>=_c;--a)
#define forv(a,b) for(auto&a:b)
#define fi first
#define se second
#define pb push_back
#define ii pair<int,int>
#define mt make_tuple
#define all(a) a.begin(),a.end()
#define reset(f, x) memset(f, x, sizeof(f))
#define bit(x,i) ((x>>(i-1))&1)
#define on(x,i) (x|(1ll<<(i-1)))
#define off(x,i) (x&~(1<<(i-1)))
#define gg exit(0);
const int N=200010;
int n,q;
int a[N],b[N],c[N];
vector<int> val;
int T[N*4];
int bit[N],pf[N],sf[N];
vector<ii> qry;
void upd(int s,int l,int r,int i,int x){
if(l>i||i>r) return;
if(l==r){
T[s]=max(T[s],x);
return;
}
int m=(l+r)/2;
upd(2*s,l,m,i,x); upd(2*s+1,m+1,r,i,x);
T[s]=max(T[2*s],T[2*s+1]);
}
int que(int s,int l,int r,int u,int v){
if(l>v||u>r||u>v) return 0;
if(u<=l&&r<=v) return T[s];
int m=(l+r)/2;
return max(que(2*s,l,m,u,v),que(2*s+1,m+1,r,u,v));
}
void upd(int i){
i=lower_bound(all(val),i)-val.begin()+1;
for(;i;i-=i&-i) bit[i]++;
}
int que(int i){
i=lower_bound(all(val),i)-val.begin()+1;
int ret=0;
for(;i<=q;i+=i&-i) ret+=bit[i];
return ret;
}
main(){
#define task "fortune"
//freopen(task".inp","r",stdin);
//freopen(task".out","w",stdout);
n=in,q=in;
forinc(i,1,n) a[i]=in, b[i]=in;
forinc(i,1,q) val.push_back(c[i]=in);
sort(all(val)); ///, val.erase(unique(all(val)),val.end());
forinc(i,1,q){
int pos=lower_bound(all(val),c[i])-val.begin()+1;
upd(1,1,q,pos,i);
}
forinc(i,1,n){
int lf=lower_bound(all(val),min(a[i],b[i]))-val.begin()+1;
int rt=lower_bound(all(val),max(a[i],b[i]))-val.begin();
qry.push_back({pf[i]=que(1,1,q,lf,rt),i});
}
sort(all(qry));
int j=1;
forv(i,qry){
while(j<=i.fi) upd(c[j++]);
int dw=que(max(a[i.se],b[i.se]));
int tot=lower_bound(all(val),max(a[i.se],b[i.se]))-val.begin(); tot=q-tot;
sf[i.se]=tot-dw;
}
int tot=0;
forinc(i,1,n){
if(!pf[i]){
tot+=sf[i]&1 ? b[i] : a[i];
} else{
tot+=(a[i]<b[i]) ^ (sf[i]&1) ? b[i] : a[i];
}
}
cout<<tot;
}
Compilation message
fortune_telling2.cpp:61:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
6 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
6 ms |
512 KB |
Output is correct |
6 |
Correct |
6 ms |
512 KB |
Output is correct |
7 |
Correct |
6 ms |
512 KB |
Output is correct |
8 |
Correct |
6 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
6 ms |
512 KB |
Output is correct |
13 |
Correct |
6 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
6 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
6 ms |
512 KB |
Output is correct |
6 |
Correct |
6 ms |
512 KB |
Output is correct |
7 |
Correct |
6 ms |
512 KB |
Output is correct |
8 |
Correct |
6 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
6 ms |
512 KB |
Output is correct |
13 |
Correct |
6 ms |
512 KB |
Output is correct |
14 |
Correct |
22 ms |
1408 KB |
Output is correct |
15 |
Correct |
40 ms |
2424 KB |
Output is correct |
16 |
Correct |
59 ms |
3068 KB |
Output is correct |
17 |
Correct |
82 ms |
4332 KB |
Output is correct |
18 |
Correct |
78 ms |
4328 KB |
Output is correct |
19 |
Correct |
77 ms |
4332 KB |
Output is correct |
20 |
Correct |
79 ms |
4476 KB |
Output is correct |
21 |
Correct |
73 ms |
4332 KB |
Output is correct |
22 |
Correct |
57 ms |
4332 KB |
Output is correct |
23 |
Correct |
58 ms |
4332 KB |
Output is correct |
24 |
Correct |
57 ms |
4328 KB |
Output is correct |
25 |
Correct |
55 ms |
4332 KB |
Output is correct |
26 |
Correct |
61 ms |
4080 KB |
Output is correct |
27 |
Correct |
70 ms |
4328 KB |
Output is correct |
28 |
Correct |
66 ms |
4204 KB |
Output is correct |
29 |
Correct |
66 ms |
4332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
6 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
6 ms |
512 KB |
Output is correct |
6 |
Correct |
6 ms |
512 KB |
Output is correct |
7 |
Correct |
6 ms |
512 KB |
Output is correct |
8 |
Correct |
6 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
6 ms |
512 KB |
Output is correct |
13 |
Correct |
6 ms |
512 KB |
Output is correct |
14 |
Correct |
22 ms |
1408 KB |
Output is correct |
15 |
Correct |
40 ms |
2424 KB |
Output is correct |
16 |
Correct |
59 ms |
3068 KB |
Output is correct |
17 |
Correct |
82 ms |
4332 KB |
Output is correct |
18 |
Correct |
78 ms |
4328 KB |
Output is correct |
19 |
Correct |
77 ms |
4332 KB |
Output is correct |
20 |
Correct |
79 ms |
4476 KB |
Output is correct |
21 |
Correct |
73 ms |
4332 KB |
Output is correct |
22 |
Correct |
57 ms |
4332 KB |
Output is correct |
23 |
Correct |
58 ms |
4332 KB |
Output is correct |
24 |
Correct |
57 ms |
4328 KB |
Output is correct |
25 |
Correct |
55 ms |
4332 KB |
Output is correct |
26 |
Correct |
61 ms |
4080 KB |
Output is correct |
27 |
Correct |
70 ms |
4328 KB |
Output is correct |
28 |
Correct |
66 ms |
4204 KB |
Output is correct |
29 |
Correct |
66 ms |
4332 KB |
Output is correct |
30 |
Correct |
192 ms |
10084 KB |
Output is correct |
31 |
Correct |
255 ms |
15716 KB |
Output is correct |
32 |
Correct |
320 ms |
17888 KB |
Output is correct |
33 |
Correct |
496 ms |
23772 KB |
Output is correct |
34 |
Correct |
185 ms |
11236 KB |
Output is correct |
35 |
Correct |
469 ms |
24028 KB |
Output is correct |
36 |
Correct |
479 ms |
23772 KB |
Output is correct |
37 |
Correct |
464 ms |
23900 KB |
Output is correct |
38 |
Correct |
475 ms |
23908 KB |
Output is correct |
39 |
Correct |
471 ms |
23900 KB |
Output is correct |
40 |
Correct |
394 ms |
23772 KB |
Output is correct |
41 |
Correct |
472 ms |
23904 KB |
Output is correct |
42 |
Correct |
472 ms |
23900 KB |
Output is correct |
43 |
Correct |
303 ms |
23772 KB |
Output is correct |
44 |
Correct |
303 ms |
23772 KB |
Output is correct |
45 |
Correct |
302 ms |
23772 KB |
Output is correct |
46 |
Correct |
330 ms |
22756 KB |
Output is correct |
47 |
Correct |
316 ms |
22492 KB |
Output is correct |
48 |
Correct |
366 ms |
23132 KB |
Output is correct |
49 |
Correct |
357 ms |
23092 KB |
Output is correct |