# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
317753 | kaitran1112 | Fortune Telling 2 (JOI14_fortune_telling2) | C++14 | 377 ms | 51060 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int,int>
#define iii pair<int,ii>
#define fi first
#define se second
#define pb push_back
#define trash trash
#define ALL(x) x.begin(),x.end()
const int oo=1e9;
const int Mod=1e9+7;
const long long OO=1e18;
const int N=1e6+5;
int n, k, ans, a[N], b[N], c[N], mp[N][25], f[N];
vector<int>vc;
vector<ii>querry;
///////////////////24<3/////////////////
void upd(int x)
{
for(;x; x-=x&-x)f[x]++;
}
int get(int x)
{
int res = 0;
for(;x<=k;x+=x&-x)res+=f[x]; return res;
}
int get_max(int l, int r)
{
if(l>r)return 0;
int t = log2(r-l+1);
return max(mp[l][t], mp[r-(1<<t)+1][t]);
}
void in()
{
cin >> n >> k;
for(int i=1; i<=n; i++)cin >> a[i] >> b[i];
for(int i=1; i<=k; i++)
{
cin >> c[i]; vc.pb(c[i]);
}
sort(vc.begin(),vc.end());
for(int i=1; i<=k; i++) c[i] = lower_bound(ALL(vc),c[i]) - vc.begin()+1;
for(int i=1; i<=k; i++)mp[c[i]][0] = i;
for(int i=1; i<=20; i++)
for(int j=1; j<=k; j++)mp[j][i] = max(mp[j][i-1], mp[j+(1<<(i-1))][i-1]);
}
void solve()
{
for(int i=1; i<=n; i++)
{
int x = a[i], y = b[i]; if(x>y)swap(x,y);
x = lower_bound(ALL(vc),x) - vc.begin()+1;
y = lower_bound(ALL(vc),y) - vc.begin();
querry.pb({get_max(x,y),i});
}
sort(ALL(querry));
int cur = 0;
for(ii x : querry)
{
int pos = x.fi, i = x.se;
while(cur < pos)upd(c[cur++]);
int Max = max(a[i],b[i]); Max = lower_bound(ALL(vc),Max) - vc.begin() + 1;
int cnt = k - Max + 1;
cnt -= get(Max);
if(pos)ans += (a[i]<b[i])^(cnt%2) ? b[i] : a[i];
else ans += cnt%2 ? b[i] : a[i];
}
cout << ans;
}
///////////////////24<3/////////////////
void file()
{
if(fopen("trash.inp", "r"))
freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
// else freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
}
main()
{
//file();
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
in();
solve();
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |