#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define TASKNAME "NAME"
void init()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
///freopen(TASKNAME".INP","r",stdin); freopen(TASKNAME".OUT","w",stdout);
}
const int SZ = 2e5+5;
const ll INF = INT_MAX / 2, MOD = 1e9+7, INFLL = 2e18;
const double epsilon = 1e-3;
int n,k, b[SZ], st[19][SZ];
pii a[SZ], c[SZ];
void rmqC()
{
for(int i = 1; i <= k; i++)
{
st[0][i] = c[i].se;
}
for(int j = 1; j <= 18; j++)
{
for(int i = 1; i + (1 << j) - 1 <= k; i++)
{
st[j][i] = max(st[j-1][i], st[j-1][i + (1 << (j-1)) ]);
}
}
}
int getMax(int lo, int hi)
{
if(lo > hi) return 0;
int k = __lg(hi - lo + 1);
return max(st[k][lo], st[k][hi - (1 << k) + 1 ]);
}
struct Card
{
int x,y, pos;
Card(int _x = 0, int _y = 0, int _pos = 0) : x(_x), y(_y), pos(_pos) {}
bool operator < (const Card& other) const
{
return pos > other.pos;
}
} cards[SZ];
int lwb(int x)
{
int lo = 1, hi = k, mid;
while(lo <= hi)
{
mid = (lo + hi) / 2;
if(c[mid].fi >= x)
{
hi = mid-1;
} else
{
lo = mid+1;
}
}
return lo;
}
vector<int> cprs;
ll res = 0;
struct FenwickTree
{
int nodes[SZ];
void update(int pos)
{
while(pos > 0)
{
nodes[pos]++;
pos -= pos & (-pos);
}
}
int query(int pos)
{
int sum = 0;
while(pos <= k)
{
sum += nodes[pos];
pos += pos & (-pos);
}
return sum;
}
} ft;
int main()
{
init();
cin >> n >> k;
for(int i = 1; i <= n; i++)
{
cin >> a[i].fi >> a[i].se;
}
for(int i = 1; i <= k; i++)
{
cin >> b[i];
cprs.push_back(b[i]);
c[i] = {b[i],i};
}
sort(c + 1, c + k + 1);
rmqC();
for(int i = 1; i <= n; i++)
{
int pos1 = lwb(min(a[i].fi,a[i].se)) , pos2 = lwb(max(a[i].fi,a[i].se)) - 1;
cards[i] = {a[i].fi, a[i].se, getMax(pos1, pos2) };
//cout << pos1 << " " << pos2 << " " <<getMax(pos1, pos2) << '\n';
}
sort(cards + 1, cards + n + 1);
sort(all(cprs));
for(int i = 1; i <= k; i++)
{
b[i] = lower_bound(all(cprs), b[i]) - cprs.begin() + 1;
}
b[0] = 0;
int j = k;
for(int i = 1; i <= n; i++)
{
int x = cards[i].x, y = cards[i].y, pos = cards[i].pos;
if(pos != 0 && x < y) swap(x,y);
//cout << x << " " << y << " " << pos << '\n';
while(j > pos)
{
ft.update(b[j]);
//cout << "updated " << j << '\n';
j--;
}
//cout << '\n';
if(pos != 0)
{
int cur = ft.query(b[pos]+1);
if(cur % 2 == 1) res += 1LL*y;
else res += 1LL*x;
}
else
{
int mx = max(x,y);
int p = lower_bound(all(cprs), mx) - cprs.begin() + 1;
int cur = ft.query(p);
if(cur % 2 == 1) res += 1LL*y;
else res += 1LL*x;
}
}
cout << res;
}
/*
5 3
4 6
9 1
8 8
4 2
3 7
8
2
9
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14684 KB |
Output is correct |
2 |
Correct |
3 ms |
14684 KB |
Output is correct |
3 |
Correct |
3 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14820 KB |
Output is correct |
5 |
Correct |
3 ms |
14816 KB |
Output is correct |
6 |
Correct |
3 ms |
14680 KB |
Output is correct |
7 |
Correct |
3 ms |
14680 KB |
Output is correct |
8 |
Correct |
3 ms |
14684 KB |
Output is correct |
9 |
Correct |
2 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
14680 KB |
Output is correct |
11 |
Correct |
3 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
14680 KB |
Output is correct |
13 |
Correct |
2 ms |
14684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14684 KB |
Output is correct |
2 |
Correct |
3 ms |
14684 KB |
Output is correct |
3 |
Correct |
3 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14820 KB |
Output is correct |
5 |
Correct |
3 ms |
14816 KB |
Output is correct |
6 |
Correct |
3 ms |
14680 KB |
Output is correct |
7 |
Correct |
3 ms |
14680 KB |
Output is correct |
8 |
Correct |
3 ms |
14684 KB |
Output is correct |
9 |
Correct |
2 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
14680 KB |
Output is correct |
11 |
Correct |
3 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
14680 KB |
Output is correct |
13 |
Correct |
2 ms |
14684 KB |
Output is correct |
14 |
Correct |
10 ms |
17244 KB |
Output is correct |
15 |
Correct |
15 ms |
19788 KB |
Output is correct |
16 |
Correct |
22 ms |
20048 KB |
Output is correct |
17 |
Correct |
30 ms |
20684 KB |
Output is correct |
18 |
Correct |
30 ms |
20648 KB |
Output is correct |
19 |
Correct |
29 ms |
20444 KB |
Output is correct |
20 |
Correct |
30 ms |
20448 KB |
Output is correct |
21 |
Correct |
28 ms |
20444 KB |
Output is correct |
22 |
Correct |
22 ms |
19936 KB |
Output is correct |
23 |
Correct |
22 ms |
19932 KB |
Output is correct |
24 |
Correct |
22 ms |
20192 KB |
Output is correct |
25 |
Correct |
22 ms |
19920 KB |
Output is correct |
26 |
Correct |
25 ms |
20440 KB |
Output is correct |
27 |
Correct |
29 ms |
20532 KB |
Output is correct |
28 |
Correct |
32 ms |
20572 KB |
Output is correct |
29 |
Correct |
32 ms |
20532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
14684 KB |
Output is correct |
2 |
Correct |
3 ms |
14684 KB |
Output is correct |
3 |
Correct |
3 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14820 KB |
Output is correct |
5 |
Correct |
3 ms |
14816 KB |
Output is correct |
6 |
Correct |
3 ms |
14680 KB |
Output is correct |
7 |
Correct |
3 ms |
14680 KB |
Output is correct |
8 |
Correct |
3 ms |
14684 KB |
Output is correct |
9 |
Correct |
2 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
14680 KB |
Output is correct |
11 |
Correct |
3 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
14680 KB |
Output is correct |
13 |
Correct |
2 ms |
14684 KB |
Output is correct |
14 |
Correct |
10 ms |
17244 KB |
Output is correct |
15 |
Correct |
15 ms |
19788 KB |
Output is correct |
16 |
Correct |
22 ms |
20048 KB |
Output is correct |
17 |
Correct |
30 ms |
20684 KB |
Output is correct |
18 |
Correct |
30 ms |
20648 KB |
Output is correct |
19 |
Correct |
29 ms |
20444 KB |
Output is correct |
20 |
Correct |
30 ms |
20448 KB |
Output is correct |
21 |
Correct |
28 ms |
20444 KB |
Output is correct |
22 |
Correct |
22 ms |
19936 KB |
Output is correct |
23 |
Correct |
22 ms |
19932 KB |
Output is correct |
24 |
Correct |
22 ms |
20192 KB |
Output is correct |
25 |
Correct |
22 ms |
19920 KB |
Output is correct |
26 |
Correct |
25 ms |
20440 KB |
Output is correct |
27 |
Correct |
29 ms |
20532 KB |
Output is correct |
28 |
Correct |
32 ms |
20572 KB |
Output is correct |
29 |
Correct |
32 ms |
20532 KB |
Output is correct |
30 |
Correct |
76 ms |
24780 KB |
Output is correct |
31 |
Correct |
95 ms |
25576 KB |
Output is correct |
32 |
Correct |
116 ms |
26620 KB |
Output is correct |
33 |
Correct |
161 ms |
28620 KB |
Output is correct |
34 |
Correct |
70 ms |
24584 KB |
Output is correct |
35 |
Correct |
166 ms |
28448 KB |
Output is correct |
36 |
Correct |
175 ms |
28476 KB |
Output is correct |
37 |
Correct |
161 ms |
28620 KB |
Output is correct |
38 |
Correct |
180 ms |
28468 KB |
Output is correct |
39 |
Correct |
165 ms |
28480 KB |
Output is correct |
40 |
Correct |
143 ms |
28116 KB |
Output is correct |
41 |
Correct |
160 ms |
28488 KB |
Output is correct |
42 |
Correct |
172 ms |
28488 KB |
Output is correct |
43 |
Correct |
107 ms |
27600 KB |
Output is correct |
44 |
Correct |
106 ms |
27596 KB |
Output is correct |
45 |
Correct |
108 ms |
27572 KB |
Output is correct |
46 |
Correct |
115 ms |
26816 KB |
Output is correct |
47 |
Correct |
123 ms |
26572 KB |
Output is correct |
48 |
Correct |
152 ms |
28624 KB |
Output is correct |
49 |
Correct |
138 ms |
28596 KB |
Output is correct |