#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 2e6 + 15;
const int INF = 0x3f3f3f3f;
class SparseSegmentTree
{
public:
void createNode(){
e.push_back( 0 );
d.push_back( 0 );
mx.push_back( 0 );
sum.push_back( 0 );
}
int getLeft(int node){
if(e[node] == 0){
e[node] = ++curNode;
createNode();
}
return e[node];
}
int getRight(int node){
if(d[node] == 0){
d[node] = ++curNode;
createNode();
}
return d[node];
}
void update(int node, int l, int r, int i, int v){
if(l == r){
mx[ node ] = v;
sum[ node ]++;
return;
}
int m = (l + r)/2;
if(i <= m) update(getLeft(node) , l , m , i , v);
else update(getRight(node) , m + 1 , r , i , v);
sum[ node ] = sum[ e[node] ] + sum[ d[node] ];
mx[ node ] = max(mx[ e[node] ] , mx[ d[node] ]);
}
int queryMax(int node, int l, int r, int i, int j){
if(i > j) return 0;
if(node == 0 || j < l || r < i) return 0;
if(i <= l && r <= j) return mx[ node ];
int m = (l + r)/2;
int maxLeft = queryMax(e[node] , l , m , i , j);
int maxRight = queryMax(d[node] , m + 1 , r , i , j);
return max(maxLeft , maxRight);
}
int querySum(int node, int l, int r, int i, int j){
if(node == 0 || j < l || r < i) return 0;
if(i <= l && r <= j) return sum[ node ];
int m = (l + r) >> 1;
int sumLeft = querySum(e[node] , l , m , i , j);
int sumRight = querySum(d[node] , m + 1 , r , i , j);
return sumLeft + sumRight;
}
SparseSegmentTree() : curNode( 1 ) { createNode(); createNode(); }
protected:
int curNode;
vector<int> e, d;
vector<int> mx, sum;
};
struct Event{
ll x, type, id;
Event(ll a = INF, ll b = 0, ll c = 0) : x(a), type(b), id(c) { }
bool operator < (Event other){
if(x != other.x) return x > other.x;
return type < other.type;
}
};
ll n, q, x[MAX], y[MAX], T[MAX];
bool changeOrder[MAX];
vector<Event> sweep;
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> x[i] >> y[i];
if(x[i] < y[i]){
swap(x[i], y[i]);
changeOrder[i] = true;
}
}
SparseSegmentTree aux;
for(int i = 1; i <= q; i++){
cin >> T[i];
aux.update(1, 0, INF, T[i], i);
sweep.emplace_back(i, 1, i);
}
for(int i = 1; i <= n; i++){
ll bonga = aux.queryMax(1, 0, INF, y[i], x[i] - 1);
sweep.emplace_back(bonga, 2, i);
}
sort(sweep.begin(), sweep.end());
SparseSegmentTree seg;
ll ans = 0;
for(Event cur : sweep){
if(cur.type == 2){
ll changes = seg.querySum(1, 0, INF, x[cur.id], INF);
if(cur.x == 0){
if((changes + changeOrder[cur.id]) % 2 == 0) ans += x[cur.id];
else ans += y[cur.id];
}else{
if(changes % 2 == 0) ans += x[cur.id];
else ans += y[cur.id];
}
}else seg.update(1, 0, INF, T[cur.id], 0);
}
cout << ans << '\n';
exit(0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1492 KB |
Output is correct |
2 |
Correct |
3 ms |
1360 KB |
Output is correct |
3 |
Correct |
3 ms |
1356 KB |
Output is correct |
4 |
Correct |
3 ms |
1368 KB |
Output is correct |
5 |
Correct |
3 ms |
1364 KB |
Output is correct |
6 |
Correct |
3 ms |
1364 KB |
Output is correct |
7 |
Correct |
3 ms |
1364 KB |
Output is correct |
8 |
Correct |
3 ms |
1364 KB |
Output is correct |
9 |
Correct |
3 ms |
1360 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
1364 KB |
Output is correct |
12 |
Correct |
3 ms |
1364 KB |
Output is correct |
13 |
Correct |
3 ms |
1368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1492 KB |
Output is correct |
2 |
Correct |
3 ms |
1360 KB |
Output is correct |
3 |
Correct |
3 ms |
1356 KB |
Output is correct |
4 |
Correct |
3 ms |
1368 KB |
Output is correct |
5 |
Correct |
3 ms |
1364 KB |
Output is correct |
6 |
Correct |
3 ms |
1364 KB |
Output is correct |
7 |
Correct |
3 ms |
1364 KB |
Output is correct |
8 |
Correct |
3 ms |
1364 KB |
Output is correct |
9 |
Correct |
3 ms |
1360 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
1364 KB |
Output is correct |
12 |
Correct |
3 ms |
1364 KB |
Output is correct |
13 |
Correct |
3 ms |
1368 KB |
Output is correct |
14 |
Correct |
27 ms |
8700 KB |
Output is correct |
15 |
Correct |
57 ms |
16472 KB |
Output is correct |
16 |
Correct |
87 ms |
20148 KB |
Output is correct |
17 |
Correct |
134 ms |
31808 KB |
Output is correct |
18 |
Correct |
113 ms |
31804 KB |
Output is correct |
19 |
Correct |
114 ms |
31824 KB |
Output is correct |
20 |
Correct |
121 ms |
31840 KB |
Output is correct |
21 |
Correct |
113 ms |
31700 KB |
Output is correct |
22 |
Correct |
65 ms |
7484 KB |
Output is correct |
23 |
Correct |
65 ms |
7212 KB |
Output is correct |
24 |
Correct |
64 ms |
6600 KB |
Output is correct |
25 |
Correct |
68 ms |
8080 KB |
Output is correct |
26 |
Correct |
100 ms |
31408 KB |
Output is correct |
27 |
Correct |
106 ms |
31704 KB |
Output is correct |
28 |
Correct |
102 ms |
31412 KB |
Output is correct |
29 |
Correct |
112 ms |
31796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1492 KB |
Output is correct |
2 |
Correct |
3 ms |
1360 KB |
Output is correct |
3 |
Correct |
3 ms |
1356 KB |
Output is correct |
4 |
Correct |
3 ms |
1368 KB |
Output is correct |
5 |
Correct |
3 ms |
1364 KB |
Output is correct |
6 |
Correct |
3 ms |
1364 KB |
Output is correct |
7 |
Correct |
3 ms |
1364 KB |
Output is correct |
8 |
Correct |
3 ms |
1364 KB |
Output is correct |
9 |
Correct |
3 ms |
1360 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
1364 KB |
Output is correct |
12 |
Correct |
3 ms |
1364 KB |
Output is correct |
13 |
Correct |
3 ms |
1368 KB |
Output is correct |
14 |
Correct |
27 ms |
8700 KB |
Output is correct |
15 |
Correct |
57 ms |
16472 KB |
Output is correct |
16 |
Correct |
87 ms |
20148 KB |
Output is correct |
17 |
Correct |
134 ms |
31808 KB |
Output is correct |
18 |
Correct |
113 ms |
31804 KB |
Output is correct |
19 |
Correct |
114 ms |
31824 KB |
Output is correct |
20 |
Correct |
121 ms |
31840 KB |
Output is correct |
21 |
Correct |
113 ms |
31700 KB |
Output is correct |
22 |
Correct |
65 ms |
7484 KB |
Output is correct |
23 |
Correct |
65 ms |
7212 KB |
Output is correct |
24 |
Correct |
64 ms |
6600 KB |
Output is correct |
25 |
Correct |
68 ms |
8080 KB |
Output is correct |
26 |
Correct |
100 ms |
31408 KB |
Output is correct |
27 |
Correct |
106 ms |
31704 KB |
Output is correct |
28 |
Correct |
102 ms |
31412 KB |
Output is correct |
29 |
Correct |
112 ms |
31796 KB |
Output is correct |
30 |
Correct |
510 ms |
123068 KB |
Output is correct |
31 |
Correct |
601 ms |
124628 KB |
Output is correct |
32 |
Correct |
646 ms |
126648 KB |
Output is correct |
33 |
Correct |
764 ms |
130640 KB |
Output is correct |
34 |
Correct |
500 ms |
122776 KB |
Output is correct |
35 |
Correct |
814 ms |
130860 KB |
Output is correct |
36 |
Correct |
794 ms |
130580 KB |
Output is correct |
37 |
Correct |
775 ms |
130596 KB |
Output is correct |
38 |
Correct |
784 ms |
130712 KB |
Output is correct |
39 |
Correct |
775 ms |
130660 KB |
Output is correct |
40 |
Correct |
717 ms |
130612 KB |
Output is correct |
41 |
Correct |
754 ms |
130548 KB |
Output is correct |
42 |
Correct |
783 ms |
130616 KB |
Output is correct |
43 |
Correct |
582 ms |
129672 KB |
Output is correct |
44 |
Correct |
587 ms |
127544 KB |
Output is correct |
45 |
Correct |
577 ms |
123412 KB |
Output is correct |
46 |
Correct |
388 ms |
31872 KB |
Output is correct |
47 |
Correct |
376 ms |
29280 KB |
Output is correct |
48 |
Correct |
592 ms |
130012 KB |
Output is correct |
49 |
Correct |
609 ms |
128680 KB |
Output is correct |