#include<stdio.h>
#include<vector>
#include<algorithm>
void set2(int x, int v);
int getsum2(int f, int r);
void set1(int x, int v);
int getmax1(int f, int r);
int max(int a, int b){return a>b?a:b;}
int min(int a, int b){return a<b?a:b;}
std::vector<int> X;
int a[200001];
int b[200001];
int q[200001];
int base;
struct dat
{
int i, loc, o;
bool operator<(const dat &A) const{return loc<A.loc;}
} D[200001];
int main()
{
int n, k;
int i, j;
scanf("%d%d", &n, &k);
for(i=1; i<=n; i++)
{
scanf("%d%d", a+i, b+i);
if(a[i] > b[i]){int t = a[i]; a[i] = b[i]; b[i] = t; D[i].o = 1;}
X.push_back(a[i]);
X.push_back(b[i]);
}
std::sort(X.begin(), X.end());
X.erase(std::unique(X.begin(), X.end()), X.end());
for(base=1; base<=X.size()+1; base*=2);
for(i=1; i<=n; i++)
{
a[i] = std::lower_bound(X.begin(), X.end(), a[i]) - X.begin() + 1;
b[i] = std::lower_bound(X.begin(), X.end(), b[i]) - X.begin() + 1;
}
for(i=1; i<=k; i++)
{
scanf("%d", q+i);
q[i] = std::upper_bound(X.begin(), X.end(), q[i]) - X.begin();
set1(q[i], i);
set2(q[i], 1);
}
for(i=1; i<=n; i++)
{
int l = getmax1(a[i], b[i]-1);
D[i].i = i;
D[i].loc = l;
if(l!=0) D[i].o = 1;
}
std::sort(D+1, D+1+n);
j=1;
long long sum = 0;
for(i=1; i<=n; i++)
{
for(; j<=D[i].loc; j++)set2(q[j], -1);
if((D[i].o + getsum2(b[D[i].i], X.size()))&1) sum += X[b[D[i].i]-1];
else sum += X[a[D[i].i]-1];
// printf("#%d %d %d %lld\n", D[i].i, D[i].loc, D[i].o, sum);
}
printf("%lld", sum);
return 0;
}
int IT1[550000*2];
void set1(int x, int v){for(IT1[x+=base] = v; x/=2;)IT1[x]=max(IT1[2*x], IT1[2*x+1]);}
int getmax1(int f, int r)
{
f+=base; r+=base;
int re = 0;
while(f<r)
{
if(f%2==1) re = max(re, IT1[f++]);
if(r%2==0) re = max(re, IT1[r--]);
f/=2; r/=2;
}
if(f==r) re = max(re, IT1[f]);
return re;
}
int IT2[550000*2];
void set2(int x, int v){for(IT2[x+=base] += v; x/=2;)IT2[x]=IT2[2*x]+IT2[2*x+1];}
int getsum2(int f, int r)
{
f+=base; r+=base;
int re = 0;
while(f<r)
{
if(f%2==1) re += IT2[f++];
if(r%2==0) re += IT2[r--];
f/=2; r/=2;
}
if(f==r) re += IT2[f];
return re;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
14500 KB |
Output is correct |
2 |
Correct |
0 ms |
14500 KB |
Output is correct |
3 |
Correct |
0 ms |
14500 KB |
Output is correct |
4 |
Correct |
0 ms |
14500 KB |
Output is correct |
5 |
Correct |
0 ms |
14500 KB |
Output is correct |
6 |
Correct |
2 ms |
14500 KB |
Output is correct |
7 |
Correct |
0 ms |
14500 KB |
Output is correct |
8 |
Correct |
0 ms |
14500 KB |
Output is correct |
9 |
Correct |
0 ms |
14500 KB |
Output is correct |
10 |
Correct |
2 ms |
14500 KB |
Output is correct |
11 |
Correct |
0 ms |
14500 KB |
Output is correct |
12 |
Correct |
2 ms |
14500 KB |
Output is correct |
13 |
Correct |
2 ms |
14500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
14632 KB |
Output is correct |
2 |
Correct |
30 ms |
14888 KB |
Output is correct |
3 |
Correct |
42 ms |
14888 KB |
Output is correct |
4 |
Correct |
72 ms |
15272 KB |
Output is correct |
5 |
Correct |
64 ms |
15272 KB |
Output is correct |
6 |
Correct |
66 ms |
15272 KB |
Output is correct |
7 |
Correct |
69 ms |
15272 KB |
Output is correct |
8 |
Correct |
65 ms |
15272 KB |
Output is correct |
9 |
Correct |
41 ms |
15272 KB |
Output is correct |
10 |
Correct |
56 ms |
15272 KB |
Output is correct |
11 |
Correct |
40 ms |
15272 KB |
Output is correct |
12 |
Correct |
40 ms |
15272 KB |
Output is correct |
13 |
Correct |
48 ms |
15272 KB |
Output is correct |
14 |
Correct |
58 ms |
15272 KB |
Output is correct |
15 |
Correct |
44 ms |
15272 KB |
Output is correct |
16 |
Correct |
62 ms |
15272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
109 ms |
14632 KB |
Output is correct |
2 |
Correct |
171 ms |
15272 KB |
Output is correct |
3 |
Correct |
248 ms |
16040 KB |
Output is correct |
4 |
Correct |
433 ms |
17576 KB |
Output is correct |
5 |
Correct |
39 ms |
14500 KB |
Output is correct |
6 |
Correct |
414 ms |
17576 KB |
Output is correct |
7 |
Correct |
404 ms |
17576 KB |
Output is correct |
8 |
Correct |
405 ms |
17576 KB |
Output is correct |
9 |
Correct |
399 ms |
17576 KB |
Output is correct |
10 |
Correct |
425 ms |
17576 KB |
Output is correct |
11 |
Correct |
372 ms |
17576 KB |
Output is correct |
12 |
Correct |
394 ms |
17576 KB |
Output is correct |
13 |
Correct |
416 ms |
17576 KB |
Output is correct |
14 |
Correct |
209 ms |
17576 KB |
Output is correct |
15 |
Correct |
234 ms |
17576 KB |
Output is correct |
16 |
Correct |
235 ms |
17576 KB |
Output is correct |
17 |
Correct |
309 ms |
17576 KB |
Output is correct |
18 |
Correct |
246 ms |
17576 KB |
Output is correct |
19 |
Correct |
322 ms |
17576 KB |
Output is correct |
20 |
Correct |
313 ms |
17576 KB |
Output is correct |