#include <bits/stdc++.h>
using namespace std;
int n,m,i,nr[400005],st,dr,sol,mij,solfin[200005],ceau[200005],arb[1600005];
struct wow
{
int x,y;
} v[400005];
map <int,int> m2;
pair <int,int> sal[400005];
void update (int st,int dr,int nod,int poz,int val)
{
if (st==dr)
{
arb[nod]=val;
return;
}
int mij=(st+dr)/2;
if (poz<=mij)
{
update(st,mij,2*nod,poz,val);
}
else
{
update(mij+1,dr,2*nod+1,poz,val);
}
arb[nod]=max(arb[2*nod],arb[2*nod+1]);
}
int query(int st,int dr,int nod,int ua,int ub)
{
if (ua<=st&&dr<=ub)
{
return arb[nod];
}
int r1=0,r2=0,mij=(st+dr)/2;
if (ua<=mij)
{
r1=query(st,mij,2*nod,ua,ub);
}
if (mij<ub)
{
r2=query(mij+1,dr,2*nod+1,ua,ub);
}
return max(r1,r2);
}
int ub(int x)
{
return x&(-x);
}
int aib[400005],nr2;
void upd(int x,int val)
{
int i;
for (i=x; i<=nr2; i+=ub(i))
{
aib[i]+=val;
}
}
int suma(int poz)
{
int i,sumi=0;
for (i=poz; i>=1; i-=ub(i))
{
sumi=sumi+aib[i];
}
return sumi;
}
int inv[200005];
vector <int> ev[200005];
int main()
{
ios_base :: sync_with_stdio(false);
cin.tie(0);
#ifdef HOME
ifstream cin("date.in");
ofstream cout("date.out");
#endif // HOME
cin>>n>>m;
for (i=1; i<=n; i++)
{
cin>>v[i].x>>v[i].y;
m2[v[i].x]=1;
m2[v[i].y]=1;
}
for (i=1; i<=m; i++)
{
cin>>nr[i];
m2[nr[i]]=1;
}
nr2=0;
for (auto ind: m2)
{
nr2++;
inv[nr2]=ind.first;
m2[ind.first]=nr2;
}
for (i=1; i<=n; i++)
{
v[i].x=m2[v[i].x];
v[i].y=m2[v[i].y];
}
for (i=1; i<=m; i++)
{
nr[i]=m2[nr[i]];
}
for (i=1; i<=m; i++)
{
update(1,nr2,1,nr[i],i);
}
for (i=1; i<=n; i++)
{
st=min(v[i].x,v[i].y);
dr=max(v[i].x,v[i].y);
if (st<dr)
{
ceau[i]=query(1,nr2,1,st,dr-1);
}
}
for (i=1; i<=4*nr2; i++)
{
arb[i]=0;
}
for (i=1; i<=n; i++)
{
ev[ceau[i]].push_back(i);
}
long long sum=0;
for (i=m; i>=1; i--)
{
for (int j=0; j<ev[i].size(); j++)
{
int nod=ev[i][j];
int dr=max(v[nod].x,v[nod].y);
int valu=suma(nr2)-suma(dr-1);
if (valu%2==0)
{
sum=sum+max(inv[v[nod].x],inv[v[nod].y]);
}
else
{
sum=sum+min(inv[v[nod].x],inv[v[nod].y]);
}
}
if (i!=0)
{
upd(nr[i],1);
}
}
for (int j=0; j<ev[0].size(); j++)
{
int nod=ev[0][j];
int dr=max(v[nod].x,v[nod].y);
int valu=suma(nr2)-suma(dr-1);
if (v[nod].x==min(v[nod].x,v[nod].y))
{
if (valu%2==1)
{
sum=sum+max(inv[v[nod].x],inv[v[nod].y]);
}
else
{
sum=sum+min(inv[v[nod].x],inv[v[nod].y]);
}
}
else
{
if (valu%2==0)
{
sum=sum+max(inv[v[nod].x],inv[v[nod].y]);
}
else
{
sum=sum+min(inv[v[nod].x],inv[v[nod].y]);
}
}
}
cout<<sum;
return 0;
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:130:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
130 | for (int j=0; j<ev[i].size(); j++)
| ~^~~~~~~~~~~~~
fortune_telling2.cpp:149:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
149 | for (int j=0; j<ev[0].size(); j++)
| ~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5068 KB |
Output is correct |
2 |
Correct |
5 ms |
5224 KB |
Output is correct |
3 |
Correct |
5 ms |
5312 KB |
Output is correct |
4 |
Correct |
5 ms |
5196 KB |
Output is correct |
5 |
Correct |
5 ms |
5196 KB |
Output is correct |
6 |
Correct |
5 ms |
5196 KB |
Output is correct |
7 |
Correct |
5 ms |
5324 KB |
Output is correct |
8 |
Correct |
5 ms |
5324 KB |
Output is correct |
9 |
Correct |
5 ms |
5196 KB |
Output is correct |
10 |
Correct |
4 ms |
5068 KB |
Output is correct |
11 |
Correct |
5 ms |
5196 KB |
Output is correct |
12 |
Correct |
5 ms |
5196 KB |
Output is correct |
13 |
Correct |
6 ms |
5172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5068 KB |
Output is correct |
2 |
Correct |
5 ms |
5224 KB |
Output is correct |
3 |
Correct |
5 ms |
5312 KB |
Output is correct |
4 |
Correct |
5 ms |
5196 KB |
Output is correct |
5 |
Correct |
5 ms |
5196 KB |
Output is correct |
6 |
Correct |
5 ms |
5196 KB |
Output is correct |
7 |
Correct |
5 ms |
5324 KB |
Output is correct |
8 |
Correct |
5 ms |
5324 KB |
Output is correct |
9 |
Correct |
5 ms |
5196 KB |
Output is correct |
10 |
Correct |
4 ms |
5068 KB |
Output is correct |
11 |
Correct |
5 ms |
5196 KB |
Output is correct |
12 |
Correct |
5 ms |
5196 KB |
Output is correct |
13 |
Correct |
6 ms |
5172 KB |
Output is correct |
14 |
Correct |
29 ms |
7604 KB |
Output is correct |
15 |
Correct |
61 ms |
10216 KB |
Output is correct |
16 |
Correct |
98 ms |
12876 KB |
Output is correct |
17 |
Correct |
156 ms |
15428 KB |
Output is correct |
18 |
Correct |
133 ms |
15528 KB |
Output is correct |
19 |
Correct |
151 ms |
15428 KB |
Output is correct |
20 |
Correct |
152 ms |
15444 KB |
Output is correct |
21 |
Correct |
150 ms |
15428 KB |
Output is correct |
22 |
Correct |
98 ms |
12132 KB |
Output is correct |
23 |
Correct |
87 ms |
10752 KB |
Output is correct |
24 |
Correct |
77 ms |
9636 KB |
Output is correct |
25 |
Correct |
105 ms |
13040 KB |
Output is correct |
26 |
Correct |
92 ms |
11900 KB |
Output is correct |
27 |
Correct |
116 ms |
12612 KB |
Output is correct |
28 |
Correct |
95 ms |
12704 KB |
Output is correct |
29 |
Correct |
114 ms |
14148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5068 KB |
Output is correct |
2 |
Correct |
5 ms |
5224 KB |
Output is correct |
3 |
Correct |
5 ms |
5312 KB |
Output is correct |
4 |
Correct |
5 ms |
5196 KB |
Output is correct |
5 |
Correct |
5 ms |
5196 KB |
Output is correct |
6 |
Correct |
5 ms |
5196 KB |
Output is correct |
7 |
Correct |
5 ms |
5324 KB |
Output is correct |
8 |
Correct |
5 ms |
5324 KB |
Output is correct |
9 |
Correct |
5 ms |
5196 KB |
Output is correct |
10 |
Correct |
4 ms |
5068 KB |
Output is correct |
11 |
Correct |
5 ms |
5196 KB |
Output is correct |
12 |
Correct |
5 ms |
5196 KB |
Output is correct |
13 |
Correct |
6 ms |
5172 KB |
Output is correct |
14 |
Correct |
29 ms |
7604 KB |
Output is correct |
15 |
Correct |
61 ms |
10216 KB |
Output is correct |
16 |
Correct |
98 ms |
12876 KB |
Output is correct |
17 |
Correct |
156 ms |
15428 KB |
Output is correct |
18 |
Correct |
133 ms |
15528 KB |
Output is correct |
19 |
Correct |
151 ms |
15428 KB |
Output is correct |
20 |
Correct |
152 ms |
15444 KB |
Output is correct |
21 |
Correct |
150 ms |
15428 KB |
Output is correct |
22 |
Correct |
98 ms |
12132 KB |
Output is correct |
23 |
Correct |
87 ms |
10752 KB |
Output is correct |
24 |
Correct |
77 ms |
9636 KB |
Output is correct |
25 |
Correct |
105 ms |
13040 KB |
Output is correct |
26 |
Correct |
92 ms |
11900 KB |
Output is correct |
27 |
Correct |
116 ms |
12612 KB |
Output is correct |
28 |
Correct |
95 ms |
12704 KB |
Output is correct |
29 |
Correct |
114 ms |
14148 KB |
Output is correct |
30 |
Incorrect |
374 ms |
23560 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |