#include <iostream>
#include <vector>
#include <set>
#include <iterator>
#include <algorithm>
#define ll long long
#define MAXN 200010
#define pii pair<int, int>
#define xx first
#define yy second
#define pb push_back
using namespace std;
int n, k, a[MAXN], b[MAXN], t[MAXN], last[MAXN];
int cnt[MAXN];
int bit[3*MAXN+5], seg[4*MAXN+10];
bool sw[MAXN];
pii c[MAXN];
vector<pii> tmp;
vector<int> svi;
void build(int node, int l, int r)
{
if (l==r) { seg[node]=c[l].yy; return; }
int mid=l+(r-l)/2;
build(2*node, l, mid);
build(2*node+1, mid+1, r);
seg[node]=max(seg[2*node], seg[2*node+1]);
}
int maxQuery(int node, int l, int r, int levo, int desno)
{
if (levo>r || desno<l) return -1;
if (levo<=l && desno>=r) return seg[node];
int mid=l+(r-l)/2;
return max(maxQuery(2*node, l, mid, levo, desno),
maxQuery(2*node+1, mid+1, r, levo, desno));
}
void upd(int x, int val)
{
while (x<3*MAXN)
{
bit[x]+=val;
x+=x&(-x);
}
}
int sum(int x)
{
if (x<=0) return 0;
int ret=0;
while (x)
{
ret+=bit[x];
x-=x&(-x);
}
return ret;
}
int binarna(int x)
{
int ret=distance(svi.begin(), lower_bound(svi.begin(), svi.end(), x));
return (ret+1);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n>>k;
for (int i=1; i<=n; i++) { cin>>a[i]>>b[i]; if (a[i]>b[i]) { sw[i]=true; swap(a[i], b[i]); } }
for (int i=1; i<=k; i++) cin>>t[i];
for (int i=1; i<=k; i++) { pii par={t[i], i}; c[i]=par; }
sort(c+1, c+k+1);
///for (int i=1; i<=k; i++) { cout<<c[i].xx<<" "<<c[i].yy<<endl; }
build(1, 1, k);
for (int i=1; i<=n; i++) {
pii minpar = {a[i], 0}, maxpar = {b[i], 0};
int l = distance(c + 1, lower_bound(c + 1, c + k + 1, minpar));
int r = distance(c + 1, lower_bound(c + 1, c + k + 1, maxpar));
r--;
l++; r++;
///cout<<i<<": "<<l<<" lr "<<r<<endl;
if (l > r) last[i] = 0;
else last[i] = maxQuery(1, 1, k, l, r);
}
for (int i=1; i<=n; i++) tmp.pb({last[i], i});
sort(tmp.begin(), tmp.end()/*, greater<pii>()*/);
///for (int i=0; i<tmp.size(); i++) cout<<tmp[i].xx<<" tmp "<<tmp[i].yy<<endl;
int j=tmp.size()-1;
for (int i=1; i<=k; i++) svi.pb(t[i]);
for (int i=1; i<=n; i++) svi.pb(a[i]);
sort(svi.begin(), svi.end());
auto it=unique(svi.begin(), svi.end());
svi.resize(distance(svi.begin(), it));
///for (int i=0; i<svi.size(); i++) cout<<svi[i]<<" ";
///cout<<endl;
//multiset<int> s;
for (int i=k; i>=0; i--)
{
///cout<<"i "<<i<<endl;
if (i) { int x=binarna(t[i]); /*cout<<t[i]<<" "<<x<<endl;*/ upd(x, 1); }
while (j>=0 && tmp[j].xx==i) { /*cout<<i<<" ij "<<j<<endl;*/ cnt[tmp[j].yy]=sum(binarna(a[tmp[j].yy])-1); j--; }
///cout<<j<<" j"<<endl;
}
ll ans=0;
for (int i=1; i<=n; i++)
{
int kolko=k-last[i]-cnt[i];
if (last[i]==0 && sw[i]) swap(a[i], b[i]);
///cout<<i<<" "<<last[i]<<" "<<cnt[i]<<endl;
if (last[i]==0) { if (kolko%2) ans+=(ll)b[i]; else ans+=(ll)a[i]; }
else { if (kolko%2) ans+=(ll)a[i]; else ans+=(ll)b[i]; }
///cout<<"ans "<<ans<<endl;
}
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
412 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
412 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
12 ms |
1076 KB |
Output is correct |
15 |
Correct |
28 ms |
1724 KB |
Output is correct |
16 |
Correct |
38 ms |
2200 KB |
Output is correct |
17 |
Correct |
55 ms |
3056 KB |
Output is correct |
18 |
Correct |
55 ms |
3024 KB |
Output is correct |
19 |
Correct |
59 ms |
3172 KB |
Output is correct |
20 |
Correct |
59 ms |
3084 KB |
Output is correct |
21 |
Correct |
50 ms |
3084 KB |
Output is correct |
22 |
Correct |
38 ms |
2952 KB |
Output is correct |
23 |
Correct |
43 ms |
2924 KB |
Output is correct |
24 |
Correct |
39 ms |
2816 KB |
Output is correct |
25 |
Correct |
38 ms |
2932 KB |
Output is correct |
26 |
Correct |
42 ms |
2716 KB |
Output is correct |
27 |
Correct |
47 ms |
3040 KB |
Output is correct |
28 |
Correct |
40 ms |
2936 KB |
Output is correct |
29 |
Correct |
43 ms |
2960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
412 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
12 ms |
1076 KB |
Output is correct |
15 |
Correct |
28 ms |
1724 KB |
Output is correct |
16 |
Correct |
38 ms |
2200 KB |
Output is correct |
17 |
Correct |
55 ms |
3056 KB |
Output is correct |
18 |
Correct |
55 ms |
3024 KB |
Output is correct |
19 |
Correct |
59 ms |
3172 KB |
Output is correct |
20 |
Correct |
59 ms |
3084 KB |
Output is correct |
21 |
Correct |
50 ms |
3084 KB |
Output is correct |
22 |
Correct |
38 ms |
2952 KB |
Output is correct |
23 |
Correct |
43 ms |
2924 KB |
Output is correct |
24 |
Correct |
39 ms |
2816 KB |
Output is correct |
25 |
Correct |
38 ms |
2932 KB |
Output is correct |
26 |
Correct |
42 ms |
2716 KB |
Output is correct |
27 |
Correct |
47 ms |
3040 KB |
Output is correct |
28 |
Correct |
40 ms |
2936 KB |
Output is correct |
29 |
Correct |
43 ms |
2960 KB |
Output is correct |
30 |
Correct |
95 ms |
6688 KB |
Output is correct |
31 |
Correct |
162 ms |
8012 KB |
Output is correct |
32 |
Correct |
199 ms |
9624 KB |
Output is correct |
33 |
Correct |
316 ms |
12868 KB |
Output is correct |
34 |
Correct |
93 ms |
8444 KB |
Output is correct |
35 |
Correct |
331 ms |
18732 KB |
Output is correct |
36 |
Correct |
320 ms |
18704 KB |
Output is correct |
37 |
Correct |
342 ms |
18708 KB |
Output is correct |
38 |
Correct |
323 ms |
18708 KB |
Output is correct |
39 |
Correct |
349 ms |
18728 KB |
Output is correct |
40 |
Correct |
303 ms |
18492 KB |
Output is correct |
41 |
Correct |
327 ms |
18660 KB |
Output is correct |
42 |
Correct |
341 ms |
18616 KB |
Output is correct |
43 |
Correct |
190 ms |
17552 KB |
Output is correct |
44 |
Correct |
213 ms |
17544 KB |
Output is correct |
45 |
Correct |
202 ms |
17596 KB |
Output is correct |
46 |
Correct |
231 ms |
15960 KB |
Output is correct |
47 |
Correct |
210 ms |
15664 KB |
Output is correct |
48 |
Correct |
250 ms |
17888 KB |
Output is correct |
49 |
Correct |
257 ms |
17928 KB |
Output is correct |