이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;
const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 600010, LOG=20;
int n, m, k, u, v, x, y, t, a, b;
ll ans;
int L[MAXN], R[MAXN], T[MAXN];
int seg[MAXN<<2], fen[MAXN];
vector<int> Q[MAXN], comp;
void Set(int id, int tl, int tr, int pos, int val){
if (pos<tl || tr<=pos) return ;
if (tr-tl==1){
seg[id]=val;
return ;
}
int mid=(tl+tr)>>1;
Set(id<<1, tl, mid, pos, val);
Set(id<<1 | 1, mid, tr, pos, val);
seg[id]=max(seg[id<<1], seg[id<<1 | 1]);
}
int Get(int id, int tl, int tr, int l, int r){
if (r<=tl || tr<=l) return seg[0];
if (l<=tl && tr<=r) return seg[id];
int mid=(tl+tr)>>1;
return max(Get(id<<1, tl, mid, l, r), Get(id<<1 | 1, mid, tr, l, r));
}
void add(int pos){ for (; pos<MAXN; pos+=pos&-pos) fen[pos]++;}
int get(int pos){
int res=0;
for (; pos; pos-=pos&-pos) res+=fen[pos];
return res;
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin>>n>>m;
for (int i=1; i<=n; i++) cin>>L[i]>>R[i], comp.pb(L[i]), comp.pb(R[i]);
for (int i=1; i<=m; i++) cin>>T[i], comp.pb(T[i]);
comp.pb(-1);
sort(all(comp));
comp.resize(unique(all(comp))-comp.begin());
for (int i=1; i<=n; i++){
L[i]=lower_bound(all(comp), L[i])-comp.begin();
R[i]=lower_bound(all(comp), R[i])-comp.begin();
swap(L[i], R[i]);
}
for (int i=1; i<=m; i++){
T[i]=lower_bound(all(comp), T[i])-comp.begin();
Set(1, 0, MAXN, T[i], i);
}
for (int i=1; i<=n; i++){
int t=Get(1, 0, MAXN, min(L[i], R[i]), max(L[i], R[i]));
if (t && L[i]>R[i]) swap(L[i], R[i]);
Q[t].pb(i);
//debug2(i, t)
}
for (int i=m; ~i; i--){
for (int id:Q[i]){
int x=get(MAXN-1)-get(max(L[id], R[id])-1);
if (x&1) swap(L[id], R[id]);
ans+=comp[R[id]];
//cerr<<"found "<<id<<" --> "<<comp[R[id]]<<'\n';
}
if (i) add(T[i]);
}
cout<<ans<<'\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |