#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAX 200001
int a[MAX], b[MAX], q[MAX];
struct Seg
{
int n;
vector<int> ST;
Seg(int _n) : n(_n)
{
ST.assign(1 << (__lg(_n) + 2), -1);
}
void update(int pos, int val)
{
int mid, id = 1, l = 1, r = n;
while (l < r)
{
mid = (l + r) >> 1;
if (pos > mid) id = id << 1 | 1, l = mid + 1;
else id <<= 1, r = mid;
}
ST[id] = val;
while (id > 1)
{
id >>= 1;
ST[id] = max(ST[id << 1], ST[id << 1 | 1]);
}
}
int getpos(int id, int l, int r, int u, int v)
{
if (r < u || v < l) return -1;
if (u <= l && r <= v) return ST[id];
int mid = (l + r) >> 1;
return max(getpos(id << 1, l, mid, u, v), getpos(id << 1 | 1, mid + 1, r, u, v));
}
int getpos(int u, int v)
{
return getpos(1, 1, n, u, v);
}
};
struct waifutree
{
int n;
vector<int> BIT;
waifutree(int _n) : n(_n)
{
BIT.assign(_n + 1, 0);
}
void update(int i, int val)
{
for (; i <= n; i += -i & i) BIT[i] += val;
}
int get(int i)
{
int res = 0;
for (; i > 0; i -= -i & i) res += BIT[i];
return res;
}
};
vector<int> v, res[2 * MAX + MAX], nopar;
bool check[2 * MAX + MAX];
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("ilight.inp", "r", stdin); freopen("ilight.out", "w", stdout);
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i] >> b[i], v.push_back(a[i]), v.push_back(b[i]);
for (int i = 1; i <= k; i++) cin >> q[i], v.push_back(q[i]);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
Seg sg(n * 2 + k);
waifutree fw(n * 2 + k);
for (int i = 1; i <= k; i++)
{
q[i] = lower_bound(v.begin(), v.end(), q[i]) - v.begin() + 1;
sg.update(q[i], i);
}
for (int i = 1; i <= n; i++)
{
a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1;
b[i] = lower_bound(v.begin(), v.end(), b[i]) - v.begin() + 1;
int j = sg.getpos(min(a[i], b[i]), max(a[i], b[i]) - 1);
if (j != -1) res[q[j]].push_back(i);
else nopar.push_back(i);
}
int ans = 0;
for (int i = k; i > 0; i--)
{
if (!check[q[i]])
{
check[q[i]] = true;
for (int j : res[q[i]])
{
int t = fw.get(n * 2 + k) - fw.get(max(a[j], b[j]) - 1);
if (a[j] <= b[j])
{
if (t & 1) ans += v[a[j] - 1];
else ans += v[b[j] - 1];
}
else
{
if (t & 1) ans += v[b[j] - 1];
else ans += v[a[j] - 1];
}
}
}
fw.update(q[i], 1);
}
for (int i : nopar)
{
int j = fw.get(n * 2 + k) - fw.get(max(a[i], b[i]) - 1);
if (j == 0)
{
ans += v[a[i] - 1];
continue;
}
if (j & 1) ans += v[b[i] - 1];
else ans += v[a[i] - 1];
}
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14424 KB |
Output is correct |
2 |
Correct |
6 ms |
14684 KB |
Output is correct |
3 |
Correct |
7 ms |
14580 KB |
Output is correct |
4 |
Correct |
8 ms |
14620 KB |
Output is correct |
5 |
Correct |
7 ms |
14684 KB |
Output is correct |
6 |
Correct |
7 ms |
14684 KB |
Output is correct |
7 |
Correct |
7 ms |
14680 KB |
Output is correct |
8 |
Correct |
7 ms |
14684 KB |
Output is correct |
9 |
Correct |
6 ms |
14684 KB |
Output is correct |
10 |
Correct |
7 ms |
14624 KB |
Output is correct |
11 |
Correct |
9 ms |
14684 KB |
Output is correct |
12 |
Correct |
9 ms |
14680 KB |
Output is correct |
13 |
Correct |
7 ms |
14684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14424 KB |
Output is correct |
2 |
Correct |
6 ms |
14684 KB |
Output is correct |
3 |
Correct |
7 ms |
14580 KB |
Output is correct |
4 |
Correct |
8 ms |
14620 KB |
Output is correct |
5 |
Correct |
7 ms |
14684 KB |
Output is correct |
6 |
Correct |
7 ms |
14684 KB |
Output is correct |
7 |
Correct |
7 ms |
14680 KB |
Output is correct |
8 |
Correct |
7 ms |
14684 KB |
Output is correct |
9 |
Correct |
6 ms |
14684 KB |
Output is correct |
10 |
Correct |
7 ms |
14624 KB |
Output is correct |
11 |
Correct |
9 ms |
14684 KB |
Output is correct |
12 |
Correct |
9 ms |
14680 KB |
Output is correct |
13 |
Correct |
7 ms |
14684 KB |
Output is correct |
14 |
Correct |
16 ms |
16232 KB |
Output is correct |
15 |
Correct |
28 ms |
17724 KB |
Output is correct |
16 |
Correct |
41 ms |
19920 KB |
Output is correct |
17 |
Correct |
53 ms |
20940 KB |
Output is correct |
18 |
Correct |
55 ms |
21192 KB |
Output is correct |
19 |
Correct |
58 ms |
21004 KB |
Output is correct |
20 |
Correct |
54 ms |
21200 KB |
Output is correct |
21 |
Correct |
52 ms |
21456 KB |
Output is correct |
22 |
Correct |
38 ms |
20432 KB |
Output is correct |
23 |
Correct |
38 ms |
20452 KB |
Output is correct |
24 |
Correct |
39 ms |
20520 KB |
Output is correct |
25 |
Correct |
40 ms |
20868 KB |
Output is correct |
26 |
Correct |
39 ms |
20432 KB |
Output is correct |
27 |
Correct |
47 ms |
20944 KB |
Output is correct |
28 |
Correct |
43 ms |
20944 KB |
Output is correct |
29 |
Correct |
50 ms |
21192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14424 KB |
Output is correct |
2 |
Correct |
6 ms |
14684 KB |
Output is correct |
3 |
Correct |
7 ms |
14580 KB |
Output is correct |
4 |
Correct |
8 ms |
14620 KB |
Output is correct |
5 |
Correct |
7 ms |
14684 KB |
Output is correct |
6 |
Correct |
7 ms |
14684 KB |
Output is correct |
7 |
Correct |
7 ms |
14680 KB |
Output is correct |
8 |
Correct |
7 ms |
14684 KB |
Output is correct |
9 |
Correct |
6 ms |
14684 KB |
Output is correct |
10 |
Correct |
7 ms |
14624 KB |
Output is correct |
11 |
Correct |
9 ms |
14684 KB |
Output is correct |
12 |
Correct |
9 ms |
14680 KB |
Output is correct |
13 |
Correct |
7 ms |
14684 KB |
Output is correct |
14 |
Correct |
16 ms |
16232 KB |
Output is correct |
15 |
Correct |
28 ms |
17724 KB |
Output is correct |
16 |
Correct |
41 ms |
19920 KB |
Output is correct |
17 |
Correct |
53 ms |
20940 KB |
Output is correct |
18 |
Correct |
55 ms |
21192 KB |
Output is correct |
19 |
Correct |
58 ms |
21004 KB |
Output is correct |
20 |
Correct |
54 ms |
21200 KB |
Output is correct |
21 |
Correct |
52 ms |
21456 KB |
Output is correct |
22 |
Correct |
38 ms |
20432 KB |
Output is correct |
23 |
Correct |
38 ms |
20452 KB |
Output is correct |
24 |
Correct |
39 ms |
20520 KB |
Output is correct |
25 |
Correct |
40 ms |
20868 KB |
Output is correct |
26 |
Correct |
39 ms |
20432 KB |
Output is correct |
27 |
Correct |
47 ms |
20944 KB |
Output is correct |
28 |
Correct |
43 ms |
20944 KB |
Output is correct |
29 |
Correct |
50 ms |
21192 KB |
Output is correct |
30 |
Correct |
107 ms |
26312 KB |
Output is correct |
31 |
Correct |
145 ms |
33472 KB |
Output is correct |
32 |
Correct |
199 ms |
37508 KB |
Output is correct |
33 |
Correct |
336 ms |
53680 KB |
Output is correct |
34 |
Correct |
95 ms |
25440 KB |
Output is correct |
35 |
Correct |
331 ms |
53936 KB |
Output is correct |
36 |
Correct |
326 ms |
53680 KB |
Output is correct |
37 |
Correct |
303 ms |
53428 KB |
Output is correct |
38 |
Correct |
302 ms |
53684 KB |
Output is correct |
39 |
Correct |
313 ms |
53428 KB |
Output is correct |
40 |
Correct |
302 ms |
54708 KB |
Output is correct |
41 |
Correct |
291 ms |
53420 KB |
Output is correct |
42 |
Correct |
310 ms |
53420 KB |
Output is correct |
43 |
Correct |
202 ms |
54192 KB |
Output is correct |
44 |
Correct |
197 ms |
54196 KB |
Output is correct |
45 |
Correct |
196 ms |
54192 KB |
Output is correct |
46 |
Correct |
197 ms |
51636 KB |
Output is correct |
47 |
Correct |
180 ms |
51024 KB |
Output is correct |
48 |
Correct |
241 ms |
53436 KB |
Output is correct |
49 |
Correct |
221 ms |
53684 KB |
Output is correct |