#include<bits/stdc++.h>
#define fori(i,a,b) for(int i=a;i<=b;i++)
#define int long long
#define pb push_back
using namespace std;
typedef pair<int,int> ii;
typedef tuple<int,int,int> tp;
const int M = 1e6 + 10;
const int N = 1e3 + 10;
const int mod = 1e9 + 7;
int n, k, a[M], b[M];
ii x[M];
int t[4 * M];
void build(int s, int l, int r)
{
if(l == r) {
t[s] = x[l].second;
//cout << l << " " << x[l].second << "\n";
return ;
}
int mid = (l + r)/2;
build(s * 2, l, mid);
build(s * 2 + 1, mid + 1, r);
t[s] = max(t[s * 2], t[s * 2 + 1]);
}
int get(int s, int l, int r,int u,int v)
{
if(l > v || r < u) return 0;
if(u <= l && r <= v) return t[s];
int mid= (l + r)/2;
return max(get(s * 2, l, mid, u, v), get(s * 2 + 1, mid + 1, r, u, v));
}
int bit[M];
void upd_bit(int idx)
{
while(idx <= k) {
bit[idx] ++;
idx += idx & (-idx);
}
}
int get_bit(int idx)
{
int res = 0;
while(idx > 0) {
res += bit[idx];
idx -= idx & (-idx);
}
return res;
}
inline int rev(int idx,int val) {
ii tmp = {idx,val};
return upper_bound(x , x + k + 1, tmp) - x - 1;
}
vector<ii> Q[M];
int ans[M];
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen("1.inp","r")) {
freopen("1.inp","r",stdin);
freopen("1.out","w",stdout);
}
#define task ""
if(fopen(task".inp","r")) {
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> n >> k;
for(int i = 1;i <= n; i++) cin >> a[i] >> b[i];
for(int i = 1;i <= k; i++) {
cin >> x[i].first ;
x[i].second = i;
}
sort(x + 1,x + k + 1);
build(1,1,k);
for(int i = 1;i <= n; i++) {
int l = rev(min(a[i],b[i]),0) + 1, r = rev(max(a[i],b[i]) - 1,1e9);
int pos = get(1, 1, k, l, r);
//cout << l << " " << x[r].second << " " << pos << "\n";
if(l <= r) {
Q[pos].pb({i,0});
if(a[i] < b[i]) ans[i] = 1;
}
else Q[x[l].second].pb({i,1});
}
for(int i = k;i != 0; i--) {
int pos = x[i].second;
if(i != 0) upd_bit(pos);
for(ii tmp : Q[pos]) {
int idx = tmp.first, tt = tmp.second;
//cout << idx << " " << pos << " " <<(k - i + 1 - get_bit(pos)) << "\n";
if(!tt) ans[idx] = (ans[idx] + (k - i + 1 - get_bit(pos))) % 2;
else ans[idx] = (ans[idx] + k - i + 1) % 2;
}
}
int kq = 0;
for(int i = 1;i <= n; i++) {
if(ans[i]) kq += b[i];
else kq += a[i];
// cout << ans[i] << " ";
}
cout << kq;
}
Compilation message (stderr)
fortune_telling2.cpp: In function 'int32_t main()':
fortune_telling2.cpp:72:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | freopen("1.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:73:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen("1.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:78:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:79:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |