#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
#define ykh mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count())
#define __lg(x) 63-__builtin_clzll(x)
#define pow2(x) (1LL<<x)
void __print(int x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef local
void setio(){freopen("/Users/iantsai/Library/Mobile Documents/com~apple~CloudDocs/cpp/Empty.md","r",stdin);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
void setio(){}
#define debug(x...)
#endif
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
struct line{
int a,b;
int operator()(const int x)const{
return a*x+b;
}
};
bool check(line l1,line l2,line l3){
return (l3.b-l2.b)*(l1.a-l2.a)<=(l2.b-l1.b)*(l2.a-l3.a);
}
const int mxn=2e5+5;
int n,q;
int a[mxn],b[mxn];
vector<int>temp;
struct segtree{
int seg[4*mxn];
void build(int l,int r,int id){
seg[id]=0;
if(l==r){
return ;
}
int mm=l+r>>1;
build(l,mm,id*2);
build(mm+1,r,id*2+1);
}
void modify(int l,int r,int id,int p,int v){
if(l==r){
seg[id]=max(seg[id],v);
return ;
}
int mm=l+r>>1;
if(p<=mm){
modify(l,mm,id*2,p,v);
}
else{
modify(mm+1,r,id*2+1,p,v);
}
seg[id]=max(seg[id*2],seg[id*2+1]);
}
int query(int l,int r,int id,int ql,int qr){
if(qr<ql)return 0;
if(qr<l or r<ql){
return 0;
}
if(ql<=l and r<=qr){
return seg[id];
}
int mm=l+r>>1;
return max(query(l,mm,id*2,ql,qr),query(mm+1,r,id*2+1,ql,qr));
}
}tr;
bool cmp(int i,int j){
return max(a[i],b[i])>max(a[j],b[j]);
}
int get(int x){
return lower_bound(all(temp),x)-temp.begin()+1;
}
struct BIT{
int bit[mxn];
void init(){
for(int i=0;i<mxn;i++){
bit[i]=0;
}
}
void modify(int p,int v){
for(;p<mxn;p+=p&-p){
bit[p]+=v;
}
}
int query(int p){
if(p<0)return 0;
int res=0;
for(;p;p-=p&-p){
res+=bit[p];
}
return res;
}
}browntoad;
signed main(){
setio();
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i]>>b[i];
temp.pb(min(a[i],b[i]));
temp.pb(max(a[i],b[i])-1);
}
vector<int>ord(n);
for(int i=0;i<n;i++){
ord[i]=i+1;
}
sort(all(ord),cmp);
vector<pair<int,int>>op;
for(int i=0;i<q;i++){
int x;
cin>>x;
op.pb({x,i});
temp.pb(x);
}
sort(all(temp));
temp.resize(unique(all(temp))-temp.begin());
tr.build(1,3*n,1);
for(int i=0;i<q;i++){
tr.modify(1,3*n,1,get(op[i].F),i+1);
}
sort(all(op),greater<pii>());
int ans=0;
browntoad.init();
int p=0;
for(int i=0;i<n;i++){
int id=ord[i];
while(p<q and op[p].F>=max(a[id],b[id])){
browntoad.modify(op[p].S+1,1);
p++;
}
int pos=tr.query(1,3*n,1,get(min(a[id],b[id])),get(max(a[id],b[id])-1));
int parity=(browntoad.query(q)-browntoad.query(pos)+(pos==0))%2;
if(parity%2==1){
if(a[id]<b[id]){
ans+=a[id];
}
else{
ans+=b[id];
}
}
else{
if(a[id]<b[id]){
ans+=b[id];
}
else{
ans+=a[id];
}
}
}
cout<<ans<<'\n';
}
/*
input:
*/
Compilation message
fortune_telling2.cpp: In member function 'void segtree::build(long long int, long long int, long long int)':
fortune_telling2.cpp:65:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
65 | int mm=l+r>>1;
| ~^~
fortune_telling2.cpp: In member function 'void segtree::modify(long long int, long long int, long long int, long long int, long long int)':
fortune_telling2.cpp:74:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
74 | int mm=l+r>>1;
| ~^~
fortune_telling2.cpp: In member function 'long long int segtree::query(long long int, long long int, long long int, long long int, long long int)':
fortune_telling2.cpp:91:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
91 | int mm=l+r>>1;
| ~^~
fortune_telling2.cpp: In function 'void setIO(std::string)':
fortune_telling2.cpp:42:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:43:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
6748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
6748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
6748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |