#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=3e5;
const ll inf=1e18;
const ll mod=1e9+7;
ll n,k;
struct qq
{
ll a,b;
bool ok;
ll id;
bool operator<(const qq&o)
{
return b<o.b;
}
}x[maxN];
struct vd
{
ll t,id;
bool operator<(const vd&o)
{
return t<o.t;
}
}y[maxN];
ll st[4*maxN];
void upd(ll pos,ll val,ll id=1,ll l=1,ll r=k)
{
if(l==r)
{
st[id]=val;
return;
}
ll mid=l+r>>1;
if(pos<=mid)upd(pos,val,id*2,l,mid);
else upd(pos,val,id*2+1,mid+1,r);
st[id]=max(st[id*2],st[id*2+1]);
}
ll bs(ll val,ll id=1,ll l=1,ll r=k)
{
if(st[id]<val) return l-1;
if(l==r) return l;
ll mid=l+r>>1;
ll pos=bs(val,id*2+1,mid+1,r);
if(pos==mid) return bs(val,id*2,l,mid);
return pos;
}
ll bit[maxN];
void update(ll i,ll val)
{
while(i<=k)
{
bit[i]+=val;
i+=(i&(-i));
}
}
ll get(ll i)
{
ll ans=0;
while(i>0)
{
ans+=bit[i];
i-=(i&(-i));
}
return ans;
}
void solve()
{
cin >> n >> k;
for(int i=1;i<=n;i++)
{
cin >> x[i].a >> x[i].b;
x[i].ok=false;
if(x[i].a>x[i].b)
{
swap(x[i].a,x[i].b);
x[i].ok=true;
}
x[i].id=i;
}
for(int i=1;i<=k;i++)
{
cin >> y[i].t;
y[i].id=i;
upd(i,y[i].t);
}
sort(x+1,x+n+1);
sort(y+1,y+k+1);
ll j=k;
ll ans=0;
for(int i=n;i>=1;i--)
{
while(j>0&&y[j].t>=x[i].b)
{
update(y[j].id,1);
upd(y[j].id,-inf);
j--;
}
ll pos=bs(x[i].a);
ll sum=get(k);
if(x[i].ok==true) sum++;
if(pos!=0)
{
sum+=get(pos);
if(x[i].ok==false) sum++;
}
sum%=2;
if(sum==0) ans+=x[i].a;
else ans+=x[i].b;
}
cout << ans;
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message
fortune_telling2.cpp: In function 'void upd(ll, ll, ll, ll, ll)':
fortune_telling2.cpp:40:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
40 | ll mid=l+r>>1;
| ~^~
fortune_telling2.cpp: In function 'll bs(ll, ll, ll, ll)':
fortune_telling2.cpp:49:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
49 | ll mid=l+r>>1;
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
356 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
356 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
8 ms |
1456 KB |
Output is correct |
15 |
Correct |
16 ms |
2524 KB |
Output is correct |
16 |
Correct |
25 ms |
3344 KB |
Output is correct |
17 |
Correct |
34 ms |
4716 KB |
Output is correct |
18 |
Correct |
32 ms |
4696 KB |
Output is correct |
19 |
Correct |
29 ms |
4596 KB |
Output is correct |
20 |
Correct |
34 ms |
4596 KB |
Output is correct |
21 |
Correct |
26 ms |
4688 KB |
Output is correct |
22 |
Correct |
22 ms |
4204 KB |
Output is correct |
23 |
Correct |
23 ms |
4176 KB |
Output is correct |
24 |
Correct |
25 ms |
4248 KB |
Output is correct |
25 |
Correct |
25 ms |
4376 KB |
Output is correct |
26 |
Correct |
28 ms |
4340 KB |
Output is correct |
27 |
Correct |
30 ms |
4684 KB |
Output is correct |
28 |
Correct |
31 ms |
4808 KB |
Output is correct |
29 |
Correct |
31 ms |
4628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
356 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
8 ms |
1456 KB |
Output is correct |
15 |
Correct |
16 ms |
2524 KB |
Output is correct |
16 |
Correct |
25 ms |
3344 KB |
Output is correct |
17 |
Correct |
34 ms |
4716 KB |
Output is correct |
18 |
Correct |
32 ms |
4696 KB |
Output is correct |
19 |
Correct |
29 ms |
4596 KB |
Output is correct |
20 |
Correct |
34 ms |
4596 KB |
Output is correct |
21 |
Correct |
26 ms |
4688 KB |
Output is correct |
22 |
Correct |
22 ms |
4204 KB |
Output is correct |
23 |
Correct |
23 ms |
4176 KB |
Output is correct |
24 |
Correct |
25 ms |
4248 KB |
Output is correct |
25 |
Correct |
25 ms |
4376 KB |
Output is correct |
26 |
Correct |
28 ms |
4340 KB |
Output is correct |
27 |
Correct |
30 ms |
4684 KB |
Output is correct |
28 |
Correct |
31 ms |
4808 KB |
Output is correct |
29 |
Correct |
31 ms |
4628 KB |
Output is correct |
30 |
Correct |
105 ms |
11504 KB |
Output is correct |
31 |
Correct |
120 ms |
13644 KB |
Output is correct |
32 |
Correct |
137 ms |
16028 KB |
Output is correct |
33 |
Correct |
181 ms |
21180 KB |
Output is correct |
34 |
Correct |
66 ms |
10956 KB |
Output is correct |
35 |
Correct |
173 ms |
21240 KB |
Output is correct |
36 |
Correct |
175 ms |
21192 KB |
Output is correct |
37 |
Correct |
175 ms |
21144 KB |
Output is correct |
38 |
Correct |
158 ms |
21216 KB |
Output is correct |
39 |
Correct |
163 ms |
21200 KB |
Output is correct |
40 |
Correct |
133 ms |
20928 KB |
Output is correct |
41 |
Correct |
165 ms |
21248 KB |
Output is correct |
42 |
Correct |
168 ms |
21196 KB |
Output is correct |
43 |
Correct |
108 ms |
19368 KB |
Output is correct |
44 |
Correct |
110 ms |
19540 KB |
Output is correct |
45 |
Correct |
106 ms |
19932 KB |
Output is correct |
46 |
Correct |
137 ms |
19408 KB |
Output is correct |
47 |
Correct |
146 ms |
19164 KB |
Output is correct |
48 |
Correct |
165 ms |
21268 KB |
Output is correct |
49 |
Correct |
166 ms |
21248 KB |
Output is correct |