#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int MaxN = 2e5 + 5;
const int MaxLog = 19;
int n, q;
pair<long long, long long> arr[MaxN];
pair<long long, int> query[MaxN];
void Inp()
{
cin >> n >> q;
for (int x = 1; x <= n; x++)
{
cin >> arr[x].first >> arr[x].second;
}
for (int x = 1; x <= q; x++)
{
cin >> query[x].first;
query[x].second = x;
}
sort(query + 1, query + q + 1);
}
int Sparse[MaxLog][MaxN];
void Build()
{
for (int x = 1; x <= q; x++)
{
Sparse[0][x] = query[x].second;
}
for (int x = 1; x < MaxLog; x++)
{
for (int y = 1; y + (1 << x) - 1 <= q; y++)
{
Sparse[x][y] = max(Sparse[x - 1][y], Sparse[x - 1][y + (1 << (x - 1))]);
}
}
}
int Get(int l, int r)
{
if (r < l)
{
return 0;
}
int p = __lg(r - l + 1);
return max(Sparse[p][l], Sparse[p][r - (1 << p) + 1]);
}
int Fenwick[MaxN];
void Update(int u)
{
int idx = u;
while (idx <= q)
{
Fenwick[idx]++;
idx += (idx & (-idx));
}
}
int Query(int u)
{
int idx = u, res = 0;
while (idx > 0)
{
res += Fenwick[idx];
idx -= (idx & (-idx));
}
return res;
}
vector<int> add[MaxN];
void Exc()
{
Build();
for (int x = 1; x <= n; x++)
{
int l = 1, r = q, mid, res = q + 1;
while (l <= r)
{
mid = (l + r) / 2;
if (query[mid].first >= max(arr[x].first, arr[x].second))
{
res = mid;
r = mid - 1;
}
else
{
l = mid + 1;
}
}
add[res].push_back(x);
}
long long ans = 0;
for (int w = q + 1; w > 0; w--)
{
if (w <= q)
{
Update(query[w].second);
}
for (int x : add[w])
{
int l = 1, r = q, mid, res = q + 1;
while (l <= r)
{
mid = (l + r) / 2;
if (min(arr[x].first, arr[x].second) <= query[mid].first)
{
res = mid;
r = mid - 1;
}
else
{
l = mid + 1;
}
}
int k = Get(res, w - 1), p = Query(q) - Query(k);
if (k != 0)
{
if (p & 1)
{
ans += min(arr[x].first, arr[x].second);
}
else
{
ans += max(arr[x].first, arr[x].second);
}
}
else
{
if (p & 1)
{
ans += arr[x].second;
}
else
{
ans += arr[x].first;
}
}
//cout << x << " " << w << " " << res << " " << k << " " << p << " " << ans << "\n";
}
}
cout << ans;
}
int main()
{
//freopen("A.INP", "r", stdin);
//freopen("A.OUT", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int test = 1;
//cin >> test;
for (int w = 1; w <= test; w++)
{
Inp();
Exc();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
19024 KB |
Output is correct |
2 |
Correct |
3 ms |
19024 KB |
Output is correct |
3 |
Correct |
3 ms |
19024 KB |
Output is correct |
4 |
Correct |
4 ms |
19196 KB |
Output is correct |
5 |
Correct |
3 ms |
19024 KB |
Output is correct |
6 |
Correct |
4 ms |
19024 KB |
Output is correct |
7 |
Correct |
4 ms |
19108 KB |
Output is correct |
8 |
Correct |
4 ms |
19024 KB |
Output is correct |
9 |
Correct |
3 ms |
19024 KB |
Output is correct |
10 |
Correct |
4 ms |
19024 KB |
Output is correct |
11 |
Correct |
3 ms |
19024 KB |
Output is correct |
12 |
Correct |
3 ms |
19024 KB |
Output is correct |
13 |
Correct |
4 ms |
19096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
19024 KB |
Output is correct |
2 |
Correct |
3 ms |
19024 KB |
Output is correct |
3 |
Correct |
3 ms |
19024 KB |
Output is correct |
4 |
Correct |
4 ms |
19196 KB |
Output is correct |
5 |
Correct |
3 ms |
19024 KB |
Output is correct |
6 |
Correct |
4 ms |
19024 KB |
Output is correct |
7 |
Correct |
4 ms |
19108 KB |
Output is correct |
8 |
Correct |
4 ms |
19024 KB |
Output is correct |
9 |
Correct |
3 ms |
19024 KB |
Output is correct |
10 |
Correct |
4 ms |
19024 KB |
Output is correct |
11 |
Correct |
3 ms |
19024 KB |
Output is correct |
12 |
Correct |
3 ms |
19024 KB |
Output is correct |
13 |
Correct |
4 ms |
19096 KB |
Output is correct |
14 |
Correct |
8 ms |
21072 KB |
Output is correct |
15 |
Correct |
13 ms |
21328 KB |
Output is correct |
16 |
Correct |
19 ms |
23632 KB |
Output is correct |
17 |
Correct |
24 ms |
25680 KB |
Output is correct |
18 |
Correct |
24 ms |
25696 KB |
Output is correct |
19 |
Correct |
25 ms |
25748 KB |
Output is correct |
20 |
Correct |
24 ms |
25680 KB |
Output is correct |
21 |
Correct |
24 ms |
25424 KB |
Output is correct |
22 |
Correct |
20 ms |
25680 KB |
Output is correct |
23 |
Correct |
20 ms |
25680 KB |
Output is correct |
24 |
Correct |
19 ms |
26016 KB |
Output is correct |
25 |
Correct |
17 ms |
25636 KB |
Output is correct |
26 |
Correct |
19 ms |
25692 KB |
Output is correct |
27 |
Correct |
22 ms |
25680 KB |
Output is correct |
28 |
Correct |
21 ms |
25680 KB |
Output is correct |
29 |
Correct |
23 ms |
25680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
19024 KB |
Output is correct |
2 |
Correct |
3 ms |
19024 KB |
Output is correct |
3 |
Correct |
3 ms |
19024 KB |
Output is correct |
4 |
Correct |
4 ms |
19196 KB |
Output is correct |
5 |
Correct |
3 ms |
19024 KB |
Output is correct |
6 |
Correct |
4 ms |
19024 KB |
Output is correct |
7 |
Correct |
4 ms |
19108 KB |
Output is correct |
8 |
Correct |
4 ms |
19024 KB |
Output is correct |
9 |
Correct |
3 ms |
19024 KB |
Output is correct |
10 |
Correct |
4 ms |
19024 KB |
Output is correct |
11 |
Correct |
3 ms |
19024 KB |
Output is correct |
12 |
Correct |
3 ms |
19024 KB |
Output is correct |
13 |
Correct |
4 ms |
19096 KB |
Output is correct |
14 |
Correct |
8 ms |
21072 KB |
Output is correct |
15 |
Correct |
13 ms |
21328 KB |
Output is correct |
16 |
Correct |
19 ms |
23632 KB |
Output is correct |
17 |
Correct |
24 ms |
25680 KB |
Output is correct |
18 |
Correct |
24 ms |
25696 KB |
Output is correct |
19 |
Correct |
25 ms |
25748 KB |
Output is correct |
20 |
Correct |
24 ms |
25680 KB |
Output is correct |
21 |
Correct |
24 ms |
25424 KB |
Output is correct |
22 |
Correct |
20 ms |
25680 KB |
Output is correct |
23 |
Correct |
20 ms |
25680 KB |
Output is correct |
24 |
Correct |
19 ms |
26016 KB |
Output is correct |
25 |
Correct |
17 ms |
25636 KB |
Output is correct |
26 |
Correct |
19 ms |
25692 KB |
Output is correct |
27 |
Correct |
22 ms |
25680 KB |
Output is correct |
28 |
Correct |
21 ms |
25680 KB |
Output is correct |
29 |
Correct |
23 ms |
25680 KB |
Output is correct |
30 |
Correct |
42 ms |
25424 KB |
Output is correct |
31 |
Correct |
62 ms |
26188 KB |
Output is correct |
32 |
Correct |
97 ms |
27208 KB |
Output is correct |
33 |
Correct |
167 ms |
30020 KB |
Output is correct |
34 |
Correct |
37 ms |
25236 KB |
Output is correct |
35 |
Correct |
137 ms |
30020 KB |
Output is correct |
36 |
Correct |
135 ms |
30024 KB |
Output is correct |
37 |
Correct |
149 ms |
30024 KB |
Output is correct |
38 |
Correct |
129 ms |
29512 KB |
Output is correct |
39 |
Correct |
141 ms |
30024 KB |
Output is correct |
40 |
Correct |
106 ms |
28492 KB |
Output is correct |
41 |
Correct |
115 ms |
30080 KB |
Output is correct |
42 |
Correct |
128 ms |
30024 KB |
Output is correct |
43 |
Correct |
73 ms |
27976 KB |
Output is correct |
44 |
Correct |
68 ms |
27980 KB |
Output is correct |
45 |
Correct |
70 ms |
27976 KB |
Output is correct |
46 |
Correct |
83 ms |
30228 KB |
Output is correct |
47 |
Correct |
86 ms |
31048 KB |
Output is correct |
48 |
Correct |
127 ms |
29260 KB |
Output is correct |
49 |
Correct |
104 ms |
29256 KB |
Output is correct |