This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <string>
#include <bitset>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
#include <iomanip>
#include <assert.h>
using namespace std;
#define pb push_back
#define mp make_pair
typedef pair<int,int> pii;
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
#define fi first
#define se second
#define fe first
#define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);}
#define Edg int M=0,fst[SZ],vb[SZ],nxt[SZ];void ad_de(int a,int b){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;}void adde(int a,int b){ad_de(a,b);ad_de(b,a);}
#define Edgc int M=0,fst[SZ],vb[SZ],nxt[SZ],vc[SZ];void ad_de(int a,int b,int c){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;vc[M]=c;}void adde(int a,int b,int c){ad_de(a,b,c);ad_de(b,a,c);}
#define es(x,e) (int e=fst[x];e;e=nxt[e])
#define esb(x,e,b) (int e=fst[x],b=vb[e];e;e=nxt[e],b=vb[e])
#define SZ 2234567
int n,k,a[SZ],b[SZ],t[SZ],ls[SZ],ln,A[SZ],B[SZ];
int id[SZ];
bool cmp(int x,int y)
{
return max(a[x],b[x])>max(a[y],b[y]);
}
pii tt[SZ];
int bs[SZ];
void eb(int x)
{
for(;x<SZ;x+=x&-x) bs[x]^=1;
}
int qb(int x)
{
int s=0;
for(;x>=1;x-=x&-x)
s^=bs[x];
return s;
}
int qb(int l,int r)
{
return qb(r)^qb(l-1);
}
pii sg[SZ];
pii operator + (pii a,pii b)
{
return pii(min(a.fi,b.fi),
max(a.se,b.se));
}
const int M=1048576;
pii qry(int l,int r)
{
pii aa(2e9,-2e9);
if(l>r) return aa;
for(l+=M-1,r+=M+1;l^r^1;l>>=1,r>>=1)
{
if(~l&1) aa=aa+sg[l^1];
if(r&1) aa=aa+sg[r^1];
}
return aa;
}
int main()
{
for(int i=0;i<SZ;++i)
sg[i]=pii(2e9,-2e9);
// n=1000,k=1000;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;++i)
// a[i]=rand()*22768+rand(),
// b[i]=rand()*22768+rand(),
scanf("%d%d",a+i,b+i),
ls[++ln]=a[i],
ls[++ln]=b[i],
A[i]=a[i],
B[i]=b[i];
for(int i=1;i<=k;++i)
// t[i]=rand()*22768+rand(),
scanf("%d",t+i),
ls[++ln]=t[i];
sort(ls+1,ls+1+ln);
#define LS(w) (w=lower_bound(ls+1,ls+1+ln,w)-ls)
for(int i=1;i<=n;++i)
LS(a[i]),LS(b[i]);
for(int i=1;i<=k;++i)
LS(t[i]);
ll ans=0;
for(int i=1;i<=n;++i) id[i]=i;
sort(id+1,id+1+n,cmp);
for(int i=1;i<=k;++i)
tt[i]=pii(t[i],i);
sort(tt+1,tt+1+k);
for(int i=1;i<=k;++i)
{
int w=t[i]+M; pii v(i,i);
sg[w]=sg[w]+v;
}
for(int i=M-1;i>=1;--i)
sg[i]=sg[i+i]+sg[i+i+1];
int u=k;
for(int uu=1;uu<=n;++uu)
{
int i=id[uu],
mi=min(a[i],b[i]),
mx=max(a[i],b[i]),
c=(a[i]==mi),f=c;
while(u&&tt[u].fi>=mx)
eb(tt[u--].se);
pii s=qry(mi,mx-1);
if(s.fi>s.se)
f^=qb(1,k);
else
f=qb(s.se+1,k);
if(f) ans+=min(A[i],B[i]);
else ans+=max(A[i],B[i]);
}
printf("%lld\n",ans);
}
Compilation message (stderr)
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:80:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&k);
~~~~~^~~~~~~~~~~~~~
fortune_telling2.cpp:87:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",a+i,b+i),
~~~~~~~~~~~~~~~~~~~~~~~
ls[++ln]=a[i],
~~~~~~~~~~~~~~~
ls[++ln]=b[i],
~~~~~~~~~~~~~~~
A[i]=a[i],
~~~~~~~~~^~
B[i]=b[i];
~~~~~~~~~
fortune_telling2.cpp:91:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",t+i),
~~~~~~~~~~~~~~~^~
ls[++ln]=t[i];
~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |