#pragma GCC optimize("O3")
#include<bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<61);
const int inf=(1<<30);
const int nmax=1e6+50;
const int mod=1e9+7;
using namespace std;
int a[nmax],b[nmax],q[nmax],n,m,i,j,t,sp[20][nmax],fw[nmax],sz,ans[nmax],cur,l,r;
ll sum;
vector<int>v;
vector<pair<int,int> >vc[nmax];
void upd(int i)
{
for(;i>=1;i-=i&(-i))fw[i]++;
}
int qry(int i)
{
int rs=0;
for(;i<=sz;i+=i&(-i))rs+=fw[i];
return rs;
}
int main()
{
//freopen("sol.in","r",stdin);
//freopen("sol.out","w",stdout);
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin>>n>>m;
for(i=1;i<=n;i++)
{
cin>>a[i]>>b[i];
v.pb(a[i]);
v.pb(b[i]);
}
for(i=1;i<=m;i++)
{
cin>>q[i];
v.pb(q[i]);
}
sort(v.begin(),v.end());
v.resize(unique(v.begin(),v.end())-v.begin());
sz=v.size();
for(i=1;i<=m;i++)
{
q[i]=lower_bound(v.begin(),v.end(),q[i])-v.begin()+1;
sp[0][q[i]]=++t;
}
for(i=1;sz>>i;++i)for(j=1;j+(1<<i)<=sz+1;++j)sp[i][j]=max(sp[i-1][j],sp[i-1][j+(1<<(i-1))]);
for(i=1;i<=n;i++)
{
l=min(a[i],b[i]);
r=max(a[i],b[i]);
l=lower_bound(v.begin(),v.end(),l)-v.begin()+1;
r=lower_bound(v.begin(),v.end(),r)-v.begin()+1;
cur=0;
for(j=20;j>=0;--j)if(l+(1<<j)<=r)cur=max(cur,sp[j][l]),l+=(1<<j);
if(cur!=0)
{
ans[i]=1;
if(a[i]>b[i])swap(a[i],b[i]);
}
vc[cur].pb({i,r});
}
for(i=m;i>=0;i--)
{
for(j=0;j<vc[i].size();j++)ans[vc[i][j].fr]^=qry(vc[i][j].sc)&1;
if(i)upd(q[i]);
}
for(i=1;i<=n;i++)
{
if(ans[i])sum+=b[i];
else sum+=a[i];
}
cout<<sum<<endl;
return 0;
}
/*
5 3
4 6
9 1
8 8
4 2
3 7
8
2
9
*/
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:75:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0;j<vc[i].size();j++)ans[vc[i][j].fr]^=qry(vc[i][j].sc)&1;
~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
24056 KB |
Output is correct |
2 |
Correct |
26 ms |
24136 KB |
Output is correct |
3 |
Correct |
25 ms |
24184 KB |
Output is correct |
4 |
Correct |
25 ms |
24184 KB |
Output is correct |
5 |
Correct |
22 ms |
24184 KB |
Output is correct |
6 |
Correct |
23 ms |
24184 KB |
Output is correct |
7 |
Correct |
23 ms |
24184 KB |
Output is correct |
8 |
Correct |
22 ms |
24156 KB |
Output is correct |
9 |
Correct |
22 ms |
24184 KB |
Output is correct |
10 |
Correct |
22 ms |
24056 KB |
Output is correct |
11 |
Correct |
22 ms |
24184 KB |
Output is correct |
12 |
Correct |
22 ms |
24056 KB |
Output is correct |
13 |
Correct |
22 ms |
24184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
24056 KB |
Output is correct |
2 |
Correct |
26 ms |
24136 KB |
Output is correct |
3 |
Correct |
25 ms |
24184 KB |
Output is correct |
4 |
Correct |
25 ms |
24184 KB |
Output is correct |
5 |
Correct |
22 ms |
24184 KB |
Output is correct |
6 |
Correct |
23 ms |
24184 KB |
Output is correct |
7 |
Correct |
23 ms |
24184 KB |
Output is correct |
8 |
Correct |
22 ms |
24156 KB |
Output is correct |
9 |
Correct |
22 ms |
24184 KB |
Output is correct |
10 |
Correct |
22 ms |
24056 KB |
Output is correct |
11 |
Correct |
22 ms |
24184 KB |
Output is correct |
12 |
Correct |
22 ms |
24056 KB |
Output is correct |
13 |
Correct |
22 ms |
24184 KB |
Output is correct |
14 |
Correct |
34 ms |
26672 KB |
Output is correct |
15 |
Correct |
45 ms |
29192 KB |
Output is correct |
16 |
Correct |
59 ms |
31904 KB |
Output is correct |
17 |
Correct |
74 ms |
34736 KB |
Output is correct |
18 |
Correct |
75 ms |
34672 KB |
Output is correct |
19 |
Correct |
73 ms |
34676 KB |
Output is correct |
20 |
Correct |
77 ms |
34804 KB |
Output is correct |
21 |
Correct |
72 ms |
35060 KB |
Output is correct |
22 |
Correct |
55 ms |
31476 KB |
Output is correct |
23 |
Correct |
54 ms |
30068 KB |
Output is correct |
24 |
Correct |
58 ms |
29044 KB |
Output is correct |
25 |
Correct |
56 ms |
32240 KB |
Output is correct |
26 |
Correct |
64 ms |
31092 KB |
Output is correct |
27 |
Correct |
65 ms |
31900 KB |
Output is correct |
28 |
Correct |
61 ms |
31988 KB |
Output is correct |
29 |
Correct |
69 ms |
33400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
24056 KB |
Output is correct |
2 |
Correct |
26 ms |
24136 KB |
Output is correct |
3 |
Correct |
25 ms |
24184 KB |
Output is correct |
4 |
Correct |
25 ms |
24184 KB |
Output is correct |
5 |
Correct |
22 ms |
24184 KB |
Output is correct |
6 |
Correct |
23 ms |
24184 KB |
Output is correct |
7 |
Correct |
23 ms |
24184 KB |
Output is correct |
8 |
Correct |
22 ms |
24156 KB |
Output is correct |
9 |
Correct |
22 ms |
24184 KB |
Output is correct |
10 |
Correct |
22 ms |
24056 KB |
Output is correct |
11 |
Correct |
22 ms |
24184 KB |
Output is correct |
12 |
Correct |
22 ms |
24056 KB |
Output is correct |
13 |
Correct |
22 ms |
24184 KB |
Output is correct |
14 |
Correct |
34 ms |
26672 KB |
Output is correct |
15 |
Correct |
45 ms |
29192 KB |
Output is correct |
16 |
Correct |
59 ms |
31904 KB |
Output is correct |
17 |
Correct |
74 ms |
34736 KB |
Output is correct |
18 |
Correct |
75 ms |
34672 KB |
Output is correct |
19 |
Correct |
73 ms |
34676 KB |
Output is correct |
20 |
Correct |
77 ms |
34804 KB |
Output is correct |
21 |
Correct |
72 ms |
35060 KB |
Output is correct |
22 |
Correct |
55 ms |
31476 KB |
Output is correct |
23 |
Correct |
54 ms |
30068 KB |
Output is correct |
24 |
Correct |
58 ms |
29044 KB |
Output is correct |
25 |
Correct |
56 ms |
32240 KB |
Output is correct |
26 |
Correct |
64 ms |
31092 KB |
Output is correct |
27 |
Correct |
65 ms |
31900 KB |
Output is correct |
28 |
Correct |
61 ms |
31988 KB |
Output is correct |
29 |
Correct |
69 ms |
33400 KB |
Output is correct |
30 |
Correct |
118 ms |
43548 KB |
Output is correct |
31 |
Correct |
169 ms |
51692 KB |
Output is correct |
32 |
Correct |
228 ms |
61928 KB |
Output is correct |
33 |
Correct |
364 ms |
83168 KB |
Output is correct |
34 |
Correct |
106 ms |
41452 KB |
Output is correct |
35 |
Correct |
376 ms |
83056 KB |
Output is correct |
36 |
Correct |
368 ms |
82916 KB |
Output is correct |
37 |
Correct |
396 ms |
82824 KB |
Output is correct |
38 |
Correct |
391 ms |
83040 KB |
Output is correct |
39 |
Correct |
410 ms |
82784 KB |
Output is correct |
40 |
Correct |
387 ms |
84064 KB |
Output is correct |
41 |
Correct |
397 ms |
82796 KB |
Output is correct |
42 |
Correct |
355 ms |
82528 KB |
Output is correct |
43 |
Correct |
227 ms |
81376 KB |
Output is correct |
44 |
Correct |
223 ms |
81632 KB |
Output is correct |
45 |
Correct |
244 ms |
82656 KB |
Output is correct |
46 |
Correct |
223 ms |
57312 KB |
Output is correct |
47 |
Correct |
210 ms |
49248 KB |
Output is correct |
48 |
Correct |
290 ms |
66912 KB |
Output is correct |
49 |
Correct |
290 ms |
67168 KB |
Output is correct |