#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstring>
#include <string>
#include <math.h>
using namespace std;
typedef long long ll;
typedef double D;
typedef pair<int,int> P;
#define M 1000000007
#define F first
#define S second
#define PB push_back
#define INF 100000000000000000
struct e{
ll c,a,s;
bool operator <(const e&q)const{
if(c!=q.c)return c<q.c;
return s<q.s;
}
bool operator >(const e&q)const{
if(c!=q.c)return c>q.c;
return s>q.s;
}
};
ll n,q,k,seg[1<<21],v[200005],u[200005],t[200005],BIT[200005],ans;
vector<ll>cm;
vector<e>w;
map<ll,ll>pr;
void ini(void){
for(int i=0;i<1<<21;i++)seg[i]=-1;
}
void up(int a,ll x){
a+=(1<<20)-1;
seg[a]=max(seg[a],x);
while(a>0){
a=(a-1)/2;
seg[a]=max(seg[a*2+1],seg[a*2+2]);
}
}
ll que(int a,int b,int l,int r,int o){
if(r<a||b<l)return -1;
if(a<=l&&r<=b)return seg[o];
return max(que(a,b,l,(l+r-1)/2,o*2+1),que(a,b,(l+r+1)/2,r,o*2+2));
}
void add(int a,ll x){
a++;
while(a<=q){
BIT[a]+=x;
a+=(a&-a);
}
}
ll sum(int a){
a++;
ll res=0;
while(a>0){
res+=BIT[a];
a-=(a&-a);
}
return res;
}
int main(void){
scanf("%lld%lld",&n,&q);
ini();
for(int i=0;i<n;i++){
scanf("%lld%lld",v+i,u+i);
cm.PB(v[i]);
cm.PB(u[i]);
}
for(int i=0;i<q;i++){
scanf("%lld",t+i);
cm.PB(t[i]);
}
sort(cm.begin(),cm.end());
cm.erase(unique(cm.begin(),cm.end()),cm.end());
k=cm.size();
for(int i=0;i<k;i++)pr[cm[i]]=i;
for(int i=0;i<n;i++){
v[i]=pr[v[i]];
u[i]=pr[u[i]];
w.PB(e{max(v[i],u[i]),i,0});
}
for(int i=0;i<q;i++){
t[i]=pr[t[i]];
up(t[i],i);
w.PB(e{t[i],i,1});
}
sort(w.begin(),w.end(),greater<e>());
for(int i=0;i<w.size();i++){
if(w[i].s==1){
add(w[i].a,1);
}else{
int p=w[i].a;
int a=v[p],b=u[p],x=min(a,b),y=max(a,b);
if(a==b){
ans+=cm[a];
continue;
}
int c=que(x,y-1,0,(1<<20)-1,0);
if(c==-1){
int s=sum(q-1);
if(s%2==0)ans+=cm[a];
else ans+=cm[b];
}else{
int s=sum(q-1)-sum(c);
if(s%2==0)ans+=cm[y];
else ans+=cm[x];
}
}
}
printf("%lld\n",ans);
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:95:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<w.size();i++){
~^~~~~~~~~
fortune_telling2.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld",&n,&q);
~~~~~^~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:72:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld",v+i,u+i);
~~~~~^~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:77:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld",t+i);
~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16888 KB |
Output is correct |
2 |
Correct |
16 ms |
17020 KB |
Output is correct |
3 |
Correct |
17 ms |
17208 KB |
Output is correct |
4 |
Correct |
17 ms |
17208 KB |
Output is correct |
5 |
Correct |
17 ms |
17284 KB |
Output is correct |
6 |
Correct |
17 ms |
17344 KB |
Output is correct |
7 |
Correct |
17 ms |
17472 KB |
Output is correct |
8 |
Correct |
17 ms |
17472 KB |
Output is correct |
9 |
Correct |
17 ms |
17500 KB |
Output is correct |
10 |
Correct |
17 ms |
17500 KB |
Output is correct |
11 |
Correct |
16 ms |
17500 KB |
Output is correct |
12 |
Correct |
17 ms |
17500 KB |
Output is correct |
13 |
Correct |
19 ms |
17500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16888 KB |
Output is correct |
2 |
Correct |
16 ms |
17020 KB |
Output is correct |
3 |
Correct |
17 ms |
17208 KB |
Output is correct |
4 |
Correct |
17 ms |
17208 KB |
Output is correct |
5 |
Correct |
17 ms |
17284 KB |
Output is correct |
6 |
Correct |
17 ms |
17344 KB |
Output is correct |
7 |
Correct |
17 ms |
17472 KB |
Output is correct |
8 |
Correct |
17 ms |
17472 KB |
Output is correct |
9 |
Correct |
17 ms |
17500 KB |
Output is correct |
10 |
Correct |
17 ms |
17500 KB |
Output is correct |
11 |
Correct |
16 ms |
17500 KB |
Output is correct |
12 |
Correct |
17 ms |
17500 KB |
Output is correct |
13 |
Correct |
19 ms |
17500 KB |
Output is correct |
14 |
Correct |
49 ms |
20264 KB |
Output is correct |
15 |
Correct |
102 ms |
23616 KB |
Output is correct |
16 |
Correct |
153 ms |
26456 KB |
Output is correct |
17 |
Correct |
198 ms |
30312 KB |
Output is correct |
18 |
Correct |
209 ms |
30312 KB |
Output is correct |
19 |
Correct |
201 ms |
30312 KB |
Output is correct |
20 |
Correct |
202 ms |
30492 KB |
Output is correct |
21 |
Correct |
188 ms |
30492 KB |
Output is correct |
22 |
Correct |
127 ms |
30492 KB |
Output is correct |
23 |
Correct |
119 ms |
30492 KB |
Output is correct |
24 |
Correct |
111 ms |
30492 KB |
Output is correct |
25 |
Correct |
160 ms |
30492 KB |
Output is correct |
26 |
Correct |
135 ms |
30492 KB |
Output is correct |
27 |
Correct |
156 ms |
30492 KB |
Output is correct |
28 |
Correct |
146 ms |
30492 KB |
Output is correct |
29 |
Correct |
190 ms |
30492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
16888 KB |
Output is correct |
2 |
Correct |
16 ms |
17020 KB |
Output is correct |
3 |
Correct |
17 ms |
17208 KB |
Output is correct |
4 |
Correct |
17 ms |
17208 KB |
Output is correct |
5 |
Correct |
17 ms |
17284 KB |
Output is correct |
6 |
Correct |
17 ms |
17344 KB |
Output is correct |
7 |
Correct |
17 ms |
17472 KB |
Output is correct |
8 |
Correct |
17 ms |
17472 KB |
Output is correct |
9 |
Correct |
17 ms |
17500 KB |
Output is correct |
10 |
Correct |
17 ms |
17500 KB |
Output is correct |
11 |
Correct |
16 ms |
17500 KB |
Output is correct |
12 |
Correct |
17 ms |
17500 KB |
Output is correct |
13 |
Correct |
19 ms |
17500 KB |
Output is correct |
14 |
Correct |
49 ms |
20264 KB |
Output is correct |
15 |
Correct |
102 ms |
23616 KB |
Output is correct |
16 |
Correct |
153 ms |
26456 KB |
Output is correct |
17 |
Correct |
198 ms |
30312 KB |
Output is correct |
18 |
Correct |
209 ms |
30312 KB |
Output is correct |
19 |
Correct |
201 ms |
30312 KB |
Output is correct |
20 |
Correct |
202 ms |
30492 KB |
Output is correct |
21 |
Correct |
188 ms |
30492 KB |
Output is correct |
22 |
Correct |
127 ms |
30492 KB |
Output is correct |
23 |
Correct |
119 ms |
30492 KB |
Output is correct |
24 |
Correct |
111 ms |
30492 KB |
Output is correct |
25 |
Correct |
160 ms |
30492 KB |
Output is correct |
26 |
Correct |
135 ms |
30492 KB |
Output is correct |
27 |
Correct |
156 ms |
30492 KB |
Output is correct |
28 |
Correct |
146 ms |
30492 KB |
Output is correct |
29 |
Correct |
190 ms |
30492 KB |
Output is correct |
30 |
Correct |
483 ms |
42416 KB |
Output is correct |
31 |
Correct |
625 ms |
51028 KB |
Output is correct |
32 |
Correct |
849 ms |
63736 KB |
Output is correct |
33 |
Correct |
1404 ms |
82516 KB |
Output is correct |
34 |
Correct |
386 ms |
82516 KB |
Output is correct |
35 |
Correct |
1409 ms |
90468 KB |
Output is correct |
36 |
Correct |
1436 ms |
96244 KB |
Output is correct |
37 |
Correct |
1451 ms |
102076 KB |
Output is correct |
38 |
Correct |
1392 ms |
107880 KB |
Output is correct |
39 |
Correct |
1372 ms |
113624 KB |
Output is correct |
40 |
Correct |
1223 ms |
119372 KB |
Output is correct |
41 |
Correct |
1343 ms |
125076 KB |
Output is correct |
42 |
Correct |
1381 ms |
130912 KB |
Output is correct |
43 |
Correct |
825 ms |
136004 KB |
Output is correct |
44 |
Correct |
847 ms |
141244 KB |
Output is correct |
45 |
Correct |
859 ms |
146156 KB |
Output is correct |
46 |
Correct |
763 ms |
146156 KB |
Output is correct |
47 |
Correct |
599 ms |
146156 KB |
Output is correct |
48 |
Correct |
911 ms |
147276 KB |
Output is correct |
49 |
Correct |
896 ms |
153204 KB |
Output is correct |