This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimization "O2"
#pragma GCC optimization "unroll-loop"
#pragma GCC target ("avx2")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
using namespace std;
const ll N = 2e5 + 9;
const ll Log2 = 20;
const ll inf = 1e16 + 7;
typedef pair<ll,ll> LL;
struct data{
ll x,y,id,dummy;
};
data a[N];
ll n,k,st[4*N],q[N];
bool lf(data a,data b){
return max(a.x,a.y) > max(b.x,b.y);
}
void upd(ll id,ll l,ll r,ll u,ll val){
if (u < l||r < u) return;
if (l == r){
st[id] = val; return;
}
ll mid = (l + r)/2;
upd(id*2,l,mid,u,val); upd(id*2 + 1,mid + 1,r,u,val);
st[id] = max(st[id*2],st[id*2 + 1]);
}
/// last but >= val
ll WalkLast(ll id,ll l,ll r,ll val){
if (l == r){
if (st[id] >= val) return l;
return 0;
}
ll mid = (l + r)/2;
if (st[id*2 + 1] >= val) return WalkLast(id*2 + 1,mid + 1,r,val);
return WalkLast(id*2,l,mid,val);
}
ll bit[N];
void updBIT(ll p,ll val){
for (ll i = p;i <= k;i += i & -i) bit[i] += val;
}
ll Get(ll p){
ll res = 0;
for (ll i = p;i > 0;i -= i & -i) res += bit[i];
return res;
}
vector<ll> pos;
bool cmp(ll x,ll y){
return q[x] > q[y];
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#define task "test"
if (fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin>>n>>k;
for (ll i = 1;i <= n;i++){
cin>>a[i].x>>a[i].y,a[i].id = i;
if (a[i].x < a[i].y) a[i].dummy = 1,swap(a[i].x,a[i].y);
}
for (ll i = 1;i <= k;i++) cin>>q[i],upd(1,1,k,i,q[i]),pos.push_back(i);
sort(a + 1,a + n + 1,lf); sort(pos.begin(),pos.end(),cmp);
ll cur = 0,kq = 0,mn = inf;
for (ll i = 1;i <= n;i++){
while(cur < k&&q[pos[cur]] >= a[i].x){
mn = min(mn,pos[cur]);
upd(1,1,k,pos[cur],-inf); updBIT(pos[cur],1);
cur++;
}
ll nxt = WalkLast(1,1,k,a[i].y);
ll ans = Get(k) - Get(nxt); ans %= 2;
if (!nxt) ans += a[i].dummy; ans %= 2; /// strugling for a bit =))
if (ans) kq += a[i].y;
else kq += a[i].x;
}
cout<<kq;
/// feel free to use idea : https://codeforces.com/blog/entry/12781?#comment-175184
}
Compilation message (stderr)
fortune_telling2.cpp:1: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
1 | #pragma GCC optimization "O2"
|
fortune_telling2.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
2 | #pragma GCC optimization "unroll-loop"
|
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:88:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
88 | if (!nxt) ans += a[i].dummy; ans %= 2; /// strugling for a bit =))
| ^~
fortune_telling2.cpp:88:38: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
88 | if (!nxt) ans += a[i].dummy; ans %= 2; /// strugling for a bit =))
| ^~~
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |