#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 2e5 + 10;
const int inf = 1e9 + 10;
int bit[maxn], seg[4*maxn];
void updateBIT( int id ){ for( int i = id; i < maxn; i += i&-i ) bit[i]++; }
int queryBIT( int id ){
int resp = 0;
for( int i = id; i > 0 ; i -= i&-i ) resp += bit[i];
return resp;
}
void updateSEG( int pos, int ini, int fim, int id, int val ){
if( ini > id || id > fim ) return;
if( ini == fim ){ seg[pos] = val; return; }
int mid = ( ini + fim )/2;
int l = 2*pos, r = 2*pos + 1;
updateSEG( l, ini, mid, id, val );
updateSEG( r, mid + 1, fim, id, val );
seg[pos] = max( seg[l], seg[r] );
}
int querySEG( int pos, int ini, int fim, int ki, int kf ){
if( ki > kf ) return 0;
if( fim < ki || kf < ini ) return -inf;
if( ki <= ini && fim <= kf ) return seg[pos];
int mid = ( ini + fim )/2;
int l = 2*pos, r = 2*pos + 1;
int queryl = querySEG( l, ini, mid, ki, kf );
int queryr = querySEG( r, mid + 1, fim, ki, kf );
return max( queryl, queryr );
}
struct card{
int maior, menor, idmaior;
int a, b;
card( int maior = 0, int menor = 0, int a = 0, int b = 0 ) : maior(maior), menor(menor), a(a), b(b) {}
};
vector<card> cartas;
struct evento{
int val, id, tipo;
evento( int v, int i, int t ) : val(v), id(i), tipo(t) {}
bool operator < ( evento e ){
if( val == e.val ) return tipo > e.tipo;
return val > e.val;
}
};
vector<evento> lineSweep;
int main(){
int n, m; cin >> n >> m;
cartas.emplace_back();
for( int i = 1; i <= n; i++ ){
int a, b; cin >> a >> b;
cartas.push_back( card( max( a, b ), min( a, b ), a, b ) );
lineSweep.push_back( evento( max( a, b ), i, 2 ) );
lineSweep.push_back( evento( min( a, b ), i, 1 ) );
}
for( int i = 1; i <= m; i++ ){
int t; cin >> t;
lineSweep.push_back( evento( t, i, 3 ) );
}
sort( lineSweep.begin(), lineSweep.end() );
int cont = 0;
ll resp = 0;
for( evento cur : lineSweep ){
if( cur.tipo == 3 ){
updateBIT( cur.id );
updateSEG( 1, 1, m, ++cont, cur.id );
}
if( cur.tipo == 2 ){
cartas[ cur.id ].idmaior = cont + 1;
}
if( cur.tipo == 1 ){
int last = querySEG( 1, 1, m, cartas[cur.id].idmaior, cont );
int qtd = cont - queryBIT( last );
if( last != 0 ){
if( qtd%2 == 1 ){
resp += (ll)( cartas[cur.id].menor );
}
else{
resp += (ll)( cartas[cur.id].maior );
}
}
else{
if( qtd%2 == 1 ){
resp += (ll)( cartas[cur.id].b );
}
else{
resp += (ll)( cartas[cur.id].a );
}
}
}
}
cout << resp << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
448 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
404 KB |
Output is correct |
13 |
Correct |
2 ms |
452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
448 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
404 KB |
Output is correct |
13 |
Correct |
2 ms |
452 KB |
Output is correct |
14 |
Correct |
18 ms |
1732 KB |
Output is correct |
15 |
Correct |
41 ms |
2524 KB |
Output is correct |
16 |
Correct |
57 ms |
3632 KB |
Output is correct |
17 |
Correct |
74 ms |
4556 KB |
Output is correct |
18 |
Correct |
73 ms |
4588 KB |
Output is correct |
19 |
Correct |
72 ms |
4580 KB |
Output is correct |
20 |
Correct |
72 ms |
4564 KB |
Output is correct |
21 |
Correct |
70 ms |
4528 KB |
Output is correct |
22 |
Correct |
54 ms |
4180 KB |
Output is correct |
23 |
Correct |
56 ms |
4160 KB |
Output is correct |
24 |
Correct |
61 ms |
4056 KB |
Output is correct |
25 |
Correct |
55 ms |
4180 KB |
Output is correct |
26 |
Correct |
61 ms |
4188 KB |
Output is correct |
27 |
Correct |
68 ms |
4688 KB |
Output is correct |
28 |
Correct |
66 ms |
4536 KB |
Output is correct |
29 |
Correct |
69 ms |
4608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
448 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
404 KB |
Output is correct |
13 |
Correct |
2 ms |
452 KB |
Output is correct |
14 |
Correct |
18 ms |
1732 KB |
Output is correct |
15 |
Correct |
41 ms |
2524 KB |
Output is correct |
16 |
Correct |
57 ms |
3632 KB |
Output is correct |
17 |
Correct |
74 ms |
4556 KB |
Output is correct |
18 |
Correct |
73 ms |
4588 KB |
Output is correct |
19 |
Correct |
72 ms |
4580 KB |
Output is correct |
20 |
Correct |
72 ms |
4564 KB |
Output is correct |
21 |
Correct |
70 ms |
4528 KB |
Output is correct |
22 |
Correct |
54 ms |
4180 KB |
Output is correct |
23 |
Correct |
56 ms |
4160 KB |
Output is correct |
24 |
Correct |
61 ms |
4056 KB |
Output is correct |
25 |
Correct |
55 ms |
4180 KB |
Output is correct |
26 |
Correct |
61 ms |
4188 KB |
Output is correct |
27 |
Correct |
68 ms |
4688 KB |
Output is correct |
28 |
Correct |
66 ms |
4536 KB |
Output is correct |
29 |
Correct |
69 ms |
4608 KB |
Output is correct |
30 |
Correct |
133 ms |
8244 KB |
Output is correct |
31 |
Correct |
189 ms |
10760 KB |
Output is correct |
32 |
Correct |
250 ms |
13896 KB |
Output is correct |
33 |
Correct |
381 ms |
22040 KB |
Output is correct |
34 |
Correct |
126 ms |
7536 KB |
Output is correct |
35 |
Correct |
379 ms |
22068 KB |
Output is correct |
36 |
Correct |
393 ms |
22000 KB |
Output is correct |
37 |
Correct |
429 ms |
22036 KB |
Output is correct |
38 |
Correct |
412 ms |
22072 KB |
Output is correct |
39 |
Correct |
446 ms |
22048 KB |
Output is correct |
40 |
Correct |
381 ms |
21960 KB |
Output is correct |
41 |
Correct |
368 ms |
22008 KB |
Output is correct |
42 |
Correct |
369 ms |
22044 KB |
Output is correct |
43 |
Correct |
350 ms |
21436 KB |
Output is correct |
44 |
Correct |
327 ms |
21372 KB |
Output is correct |
45 |
Correct |
324 ms |
21372 KB |
Output is correct |
46 |
Correct |
295 ms |
20324 KB |
Output is correct |
47 |
Correct |
288 ms |
20148 KB |
Output is correct |
48 |
Correct |
352 ms |
22128 KB |
Output is correct |
49 |
Correct |
359 ms |
22228 KB |
Output is correct |