#include <bits/stdc++.h>
using namespace std;
int n,k,sz;
int a[300000];
int b[300000];
int t[300000];
int bit[300000];
int comp[700000];
pair<int,int> pos[300000];
vector<int> v[900000];
void insrt(int start,int val,int i,int j,int l,int r)
{
if(i==l && j==r)
{
v[start].push_back(val);
return;
}
int m=(l+r)/2;
if(i<m)insrt(2*start,val,i,min(j,m),l,m);
if(j>m)insrt(2*start+1,val,max(i,m),j,m,r);
}
void check(int start,int j,int tt,int l,int r)
{
for(int i=0;i<v[start].size();i++)
{
if(pos[v[start][i]].first<0)
{
pos[v[start][i]].first=j;
}
}
v[start].clear();
if(r-l==1)return;
int m=(l+r)/2;
if(tt<m)check(2*start,j,tt,l,m);
else
{
check(2*start+1,j,tt,m,r);
}
}
void update(int x)
{
while(x<=sz)
{
bit[x]++;
x+=x&(-x);
}
}
int sum(int x)
{
int rez=0;
while(x>0)
{
rez+=bit[x];
x-=x&(-x);
}
return rez;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
int ind=0;
for(int i=0;i<n;i++)
{
cin >> a[i] >> b[i];
comp[ind++]=a[i];
comp[ind++]=b[i];
}
for(int i=0;i<k;i++)
{
cin >> t[i];
comp[ind++]=t[i];
}
sort(comp,comp+ind);
sz=unique(comp,comp+ind)-comp;
for(int i=0;i<n;i++)
{
a[i]=lower_bound(comp,comp+sz,a[i])-comp;
b[i]=lower_bound(comp,comp+sz,b[i])-comp;
pos[i]=make_pair(-1,i);
insrt(1,i,min(a[i],b[i]),max(a[i],b[i]),0,sz);
}
for(int i=k-1;i>=0;i--)
{
t[i]=lower_bound(comp,comp+sz,t[i])-comp;
check(1,i,t[i],0,sz);
}
sort(pos,pos+n);
reverse(pos,pos+n);
int cur=k-1;
int br=0;
long long rez=0;
for(int i=0;i<n;i++)
{
while(cur>pos[i].first)
{
update(t[cur--]+1);
br++;
}
int p=pos[i].second;
int num=(br-sum(b[p]))%2;
int beg=1;
if(cur<0)beg=0;
else
{
beg=a[p]<b[p];
}
int fin=a[p];
if((num+beg)%2)
{
fin=b[p];
}
rez+=comp[fin];
}
cout << rez;
return 0;
}
Compilation message
fortune_telling2.cpp: In function 'void check(int, int, int, int, int)':
fortune_telling2.cpp:25:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for(int i=0;i<v[start].size();i++)
| ~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
21504 KB |
Output is correct |
2 |
Correct |
16 ms |
21632 KB |
Output is correct |
3 |
Correct |
16 ms |
21760 KB |
Output is correct |
4 |
Correct |
17 ms |
21632 KB |
Output is correct |
5 |
Correct |
16 ms |
21632 KB |
Output is correct |
6 |
Correct |
18 ms |
21632 KB |
Output is correct |
7 |
Correct |
16 ms |
21632 KB |
Output is correct |
8 |
Correct |
17 ms |
21632 KB |
Output is correct |
9 |
Correct |
16 ms |
21632 KB |
Output is correct |
10 |
Correct |
16 ms |
21632 KB |
Output is correct |
11 |
Correct |
16 ms |
21632 KB |
Output is correct |
12 |
Correct |
16 ms |
21632 KB |
Output is correct |
13 |
Correct |
16 ms |
21632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
21504 KB |
Output is correct |
2 |
Correct |
16 ms |
21632 KB |
Output is correct |
3 |
Correct |
16 ms |
21760 KB |
Output is correct |
4 |
Correct |
17 ms |
21632 KB |
Output is correct |
5 |
Correct |
16 ms |
21632 KB |
Output is correct |
6 |
Correct |
18 ms |
21632 KB |
Output is correct |
7 |
Correct |
16 ms |
21632 KB |
Output is correct |
8 |
Correct |
17 ms |
21632 KB |
Output is correct |
9 |
Correct |
16 ms |
21632 KB |
Output is correct |
10 |
Correct |
16 ms |
21632 KB |
Output is correct |
11 |
Correct |
16 ms |
21632 KB |
Output is correct |
12 |
Correct |
16 ms |
21632 KB |
Output is correct |
13 |
Correct |
16 ms |
21632 KB |
Output is correct |
14 |
Correct |
38 ms |
23288 KB |
Output is correct |
15 |
Correct |
72 ms |
25208 KB |
Output is correct |
16 |
Correct |
111 ms |
27212 KB |
Output is correct |
17 |
Correct |
151 ms |
29180 KB |
Output is correct |
18 |
Correct |
148 ms |
29176 KB |
Output is correct |
19 |
Correct |
147 ms |
28920 KB |
Output is correct |
20 |
Correct |
146 ms |
28920 KB |
Output is correct |
21 |
Correct |
137 ms |
27900 KB |
Output is correct |
22 |
Correct |
94 ms |
28152 KB |
Output is correct |
23 |
Correct |
84 ms |
27156 KB |
Output is correct |
24 |
Correct |
89 ms |
27640 KB |
Output is correct |
25 |
Correct |
91 ms |
27896 KB |
Output is correct |
26 |
Correct |
88 ms |
25464 KB |
Output is correct |
27 |
Correct |
103 ms |
25720 KB |
Output is correct |
28 |
Correct |
96 ms |
26616 KB |
Output is correct |
29 |
Correct |
91 ms |
24696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
21504 KB |
Output is correct |
2 |
Correct |
16 ms |
21632 KB |
Output is correct |
3 |
Correct |
16 ms |
21760 KB |
Output is correct |
4 |
Correct |
17 ms |
21632 KB |
Output is correct |
5 |
Correct |
16 ms |
21632 KB |
Output is correct |
6 |
Correct |
18 ms |
21632 KB |
Output is correct |
7 |
Correct |
16 ms |
21632 KB |
Output is correct |
8 |
Correct |
17 ms |
21632 KB |
Output is correct |
9 |
Correct |
16 ms |
21632 KB |
Output is correct |
10 |
Correct |
16 ms |
21632 KB |
Output is correct |
11 |
Correct |
16 ms |
21632 KB |
Output is correct |
12 |
Correct |
16 ms |
21632 KB |
Output is correct |
13 |
Correct |
16 ms |
21632 KB |
Output is correct |
14 |
Correct |
38 ms |
23288 KB |
Output is correct |
15 |
Correct |
72 ms |
25208 KB |
Output is correct |
16 |
Correct |
111 ms |
27212 KB |
Output is correct |
17 |
Correct |
151 ms |
29180 KB |
Output is correct |
18 |
Correct |
148 ms |
29176 KB |
Output is correct |
19 |
Correct |
147 ms |
28920 KB |
Output is correct |
20 |
Correct |
146 ms |
28920 KB |
Output is correct |
21 |
Correct |
137 ms |
27900 KB |
Output is correct |
22 |
Correct |
94 ms |
28152 KB |
Output is correct |
23 |
Correct |
84 ms |
27156 KB |
Output is correct |
24 |
Correct |
89 ms |
27640 KB |
Output is correct |
25 |
Correct |
91 ms |
27896 KB |
Output is correct |
26 |
Correct |
88 ms |
25464 KB |
Output is correct |
27 |
Correct |
103 ms |
25720 KB |
Output is correct |
28 |
Correct |
96 ms |
26616 KB |
Output is correct |
29 |
Correct |
91 ms |
24696 KB |
Output is correct |
30 |
Correct |
172 ms |
26360 KB |
Output is correct |
31 |
Runtime error |
107 ms |
48120 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
32 |
Halted |
0 ms |
0 KB |
- |