//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#define int long long
//#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(lmao) lmao.begin(), lmao.end()
using namespace std;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
const int N = 2e5 + 5;
const int oo = 1e9 + 1;
const int mod = 1e9 + 7;
//const ll oo = 5e18;
int n, k, d[N], a[N], b[N], x[N], y[N], pt[N << 2], t, mx[N << 2], mi[N << 2], st[N << 2], f[N], e[N];
void update(int i,int x){
i += t - 1;
mx[i] = max(mx[i], x);
mi[i] = min(mi[i], x);
while(i > 1){
i /= 2;
mx[i] = max(mx[i << 1], mx[i << 1|1]);
mi[i] = min(mi[i << 1], mi[i << 1|1]);
}
}
void add(int i){
i += k - 1;
st[i]++;
while(i > 1){i /= 2;st[i]++;}
}
int get(int type,int l,int r){
r++;
int Max = 0, Min = 1e9;
for(l += t - 1, r += t - 1; l < r; l /= 2, r /= 2){
if(l & 1) Min = min(Min, mi[l]), Max = max(Max, mx[l]), l++;
if(r & 1) --r, Min = min(Min, mi[r]), Max = max(Max, mx[r]);
}
if(type == 0) return Min;
return Max;
}
int query(int l,int r){
if(l > r) return 0;
r++;
int ret = 0;
for(l += k - 1, r += k - 1; l < r; l /= 2, r /= 2){
if(l & 1) ret += st[l++];
if(r & 1) ret += st[--r];
}
return ret;
}
vector<int> T[N << 2], op[N << 2];
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "v"
if(fopen(task ".inp","r")){
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
cin >> n >> k;
set<int> s;
int ans = 0;
for(int i = 1; i <= n; i ++){
cin >> a[i] >> b[i];
if(a[i] == b[i]){
ans += a[i];
continue;
}
s.insert(a[i]); s.insert(b[i]);
}
for(int i = 1; i <= k; i ++){
cin >> d[i];
s.insert(d[i]);
}
for(auto j : s) pt[++t] = j;
// for(int i = 1; i <= t; i ++) cerr << pt[i] << " ";
// cerr << "\n";
for(int i = 1; i <= 2 * t; i ++) mi[i] = 1e9;
for(int i = 1; i <= k; i ++){
d[i] = lower_bound(pt + 1, pt + t + 1, d[i]) - pt;
// cerr << d[i] << " e\n";
op[d[i]].pb(i);
}
for(int i = 1; i <= n; i ++){
if(a[i] == b[i]) continue;
a[i] = lower_bound(pt + 1, pt + t + 1, a[i]) - pt;
b[i] = lower_bound(pt + 1, pt + t + 1, b[i]) - pt;
// cerr << a[i] << " " << b[i] << " y\n";
x[i] = min(a[i], b[i]);
y[i] = max(a[i], b[i]);
T[x[i]].pb(i);
}
for(int i = t; i >= 1; i --){
for(auto j : op[i]) update(d[j], j);
for(auto j : T[i]){
f[j] = get(0, x[j], y[j] - 1);
e[j] = get(1, x[j], y[j] - 1);
}
T[i].clear();
}
for(int i = 1; i <= n; i ++) T[y[i]].pb(i);
for(int i = t; i >= 1; i --){
for(auto j : op[i]) add(j);
// cerr << i << " h\n";
for(auto j : T[i]){
// cerr << j << " " << f[j] << " " << e[j] << " t\n";
if(f[j] == 1e9){
int tmp = query(1, k);
ans += pt[(tmp % 2 ? b[j] : a[j])];
// cerr << j << " " << tmp << " after\n";
}else{
int pre = query(1, f[j] - 1);
int suf = query(e[j] + 1, k);
if(pre) swap(a[j], b[j]);
if(a[j] <= b[j]){
swap(a[j], b[j]);
if(suf) swap(a[j], b[j]);
// cerr << a[j] << " " << pt[a[j]] << " after\n";
ans += pt[a[j]];
}else{
if(suf) swap(a[j], b[j]);
// cerr << a[j] << " " << pt[a[j]] << " after\n";
ans += pt[a[j]];
}
}
}
T[i].clear();
}
cout << ans;
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:68:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | freopen(task ".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:69:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | freopen(task ".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
20 ms |
52304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
20 ms |
52304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
20 ms |
52304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |