#include <bits/stdc++.h>
using namespace std;
const int Nmax = 2e5 + 5;
typedef long long ll;
int n, m, P;
int A[Nmax], B[Nmax], Op[Nmax];
vector<int> all;
vector<pair<int,int>> ask[Nmax];
#define left_son (node<<1)
#define right_son ((node<<1)|1)
#define mid ((st+dr)>>1)
class SegTree
{
int mx[Nmax<<2], cnt[Nmax<<2];
void update(int node, int st, int dr, int pos, int val = 0)
{
if(!val) ++cnt[node];
if(st == dr)
{
if(val)
mx[node] = max(mx[node], val);
return;
}
if(pos <= mid) update(left_son, st, mid, pos, val);
else update(right_son, mid+1, dr, pos, val);
if(val) mx[node] = max(mx[left_son], mx[right_son]);
}
int query_max(int node, int st, int dr, int L, int R)
{
if(L <= st && dr <= R) return mx[node];
int res = 0;
if(L <= mid) res = max(res, query_max(left_son, st, mid, L, R));
if(mid < R) res = max(res, query_max(right_son, mid+1, dr, L, R));
return res;
}
int query_cnt(int node, int st, int dr, int L, int R)
{
if(L <= st && dr <= R) return cnt[node];
int res = 0;
if(L <= mid) res += query_cnt(left_son, st, mid, L, R);
if(mid < R) res += query_cnt(right_son, mid+1, dr, L, R);
return res;
}
public:
void update(int x, int val = 0)
{
x = lower_bound( all.begin(), all.end(), x ) - all.begin();
update(1, 0, P-1, x, val);
}
int query_max(int x, int y)
{
x = lower_bound( all.begin(), all.end(), x ) - all.begin();
y = upper_bound( all.begin(), all.end(), y ) - all.begin() - 1;
if(x > y) return 0;
return query_max(1, 0, P-1, x, y);
}
int query_cnt(int x)
{
x = upper_bound( all.begin(), all.end(), x ) - all.begin() - 1;
if(x < 0) return 0;
return query_cnt(1, 0, P-1, 0, x);
}
} aint;
void solve(int pos)
{
int mn, mx;
mn = min(A[pos], B[pos]);
mx = max(A[pos], B[pos]);
int last = aint.query_max( mn, mx-1 );
ask[last].push_back( { pos, mn } );
}
void prepare()
{
int i;
for(i=1; i<=m; ++i) all.push_back(Op[i]);
sort(all.begin(), all.end());
all.erase( unique(all.begin(), all.end()), all.end() );
P = all.size();
for(i=1; i<=m; ++i) aint.update( Op[i], i );
}
int main()
{
// freopen("input", "r", stdin);
cin.sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
int i;
for(i=1; i<=n; ++i) cin >> A[i] >> B[i];
for(i=1; i<=m; ++i) cin >> Op[i];
prepare();
ll ans = 0;
for(i=1; i<=n; ++i) solve(i);
for(i=m; i>=0; --i)
{
for(auto it : ask[i])
{
int cnt;
cnt = m - i - aint.query_cnt(it.second - 1);
int curr;
if(i)
{
if(cnt % 2 == 1) curr = min(A[it.first], B[it.first]);
else curr = max(A[it.first], B[it.first]);
}
else
{
if(cnt % 2 == 0) curr = A[it.first];
else curr = B[it.first];
}
ans += curr;
}
if(!i) break;
aint.update( Op[i] );
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
5112 KB |
Output is correct |
5 |
Correct |
7 ms |
5116 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
10 ms |
5112 KB |
Output is correct |
8 |
Correct |
8 ms |
5112 KB |
Output is correct |
9 |
Correct |
7 ms |
5112 KB |
Output is correct |
10 |
Correct |
7 ms |
5112 KB |
Output is correct |
11 |
Correct |
9 ms |
5112 KB |
Output is correct |
12 |
Correct |
8 ms |
5112 KB |
Output is correct |
13 |
Correct |
3 ms |
5112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
5112 KB |
Output is correct |
5 |
Correct |
7 ms |
5116 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
10 ms |
5112 KB |
Output is correct |
8 |
Correct |
8 ms |
5112 KB |
Output is correct |
9 |
Correct |
7 ms |
5112 KB |
Output is correct |
10 |
Correct |
7 ms |
5112 KB |
Output is correct |
11 |
Correct |
9 ms |
5112 KB |
Output is correct |
12 |
Correct |
8 ms |
5112 KB |
Output is correct |
13 |
Correct |
3 ms |
5112 KB |
Output is correct |
14 |
Correct |
29 ms |
5880 KB |
Output is correct |
15 |
Correct |
41 ms |
6776 KB |
Output is correct |
16 |
Correct |
61 ms |
7288 KB |
Output is correct |
17 |
Correct |
84 ms |
8300 KB |
Output is correct |
18 |
Correct |
82 ms |
8180 KB |
Output is correct |
19 |
Correct |
87 ms |
8312 KB |
Output is correct |
20 |
Correct |
81 ms |
8440 KB |
Output is correct |
21 |
Correct |
79 ms |
8208 KB |
Output is correct |
22 |
Correct |
59 ms |
7412 KB |
Output is correct |
23 |
Correct |
60 ms |
7284 KB |
Output is correct |
24 |
Correct |
60 ms |
7344 KB |
Output is correct |
25 |
Correct |
60 ms |
7920 KB |
Output is correct |
26 |
Correct |
68 ms |
8184 KB |
Output is correct |
27 |
Correct |
74 ms |
8308 KB |
Output is correct |
28 |
Correct |
63 ms |
8312 KB |
Output is correct |
29 |
Correct |
75 ms |
8308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
5112 KB |
Output is correct |
5 |
Correct |
7 ms |
5116 KB |
Output is correct |
6 |
Correct |
8 ms |
5112 KB |
Output is correct |
7 |
Correct |
10 ms |
5112 KB |
Output is correct |
8 |
Correct |
8 ms |
5112 KB |
Output is correct |
9 |
Correct |
7 ms |
5112 KB |
Output is correct |
10 |
Correct |
7 ms |
5112 KB |
Output is correct |
11 |
Correct |
9 ms |
5112 KB |
Output is correct |
12 |
Correct |
8 ms |
5112 KB |
Output is correct |
13 |
Correct |
3 ms |
5112 KB |
Output is correct |
14 |
Correct |
29 ms |
5880 KB |
Output is correct |
15 |
Correct |
41 ms |
6776 KB |
Output is correct |
16 |
Correct |
61 ms |
7288 KB |
Output is correct |
17 |
Correct |
84 ms |
8300 KB |
Output is correct |
18 |
Correct |
82 ms |
8180 KB |
Output is correct |
19 |
Correct |
87 ms |
8312 KB |
Output is correct |
20 |
Correct |
81 ms |
8440 KB |
Output is correct |
21 |
Correct |
79 ms |
8208 KB |
Output is correct |
22 |
Correct |
59 ms |
7412 KB |
Output is correct |
23 |
Correct |
60 ms |
7284 KB |
Output is correct |
24 |
Correct |
60 ms |
7344 KB |
Output is correct |
25 |
Correct |
60 ms |
7920 KB |
Output is correct |
26 |
Correct |
68 ms |
8184 KB |
Output is correct |
27 |
Correct |
74 ms |
8308 KB |
Output is correct |
28 |
Correct |
63 ms |
8312 KB |
Output is correct |
29 |
Correct |
75 ms |
8308 KB |
Output is correct |
30 |
Correct |
215 ms |
11976 KB |
Output is correct |
31 |
Correct |
268 ms |
12908 KB |
Output is correct |
32 |
Correct |
332 ms |
13680 KB |
Output is correct |
33 |
Correct |
472 ms |
15856 KB |
Output is correct |
34 |
Correct |
194 ms |
11860 KB |
Output is correct |
35 |
Correct |
469 ms |
15912 KB |
Output is correct |
36 |
Correct |
478 ms |
15748 KB |
Output is correct |
37 |
Correct |
475 ms |
15504 KB |
Output is correct |
38 |
Correct |
459 ms |
15728 KB |
Output is correct |
39 |
Correct |
457 ms |
15476 KB |
Output is correct |
40 |
Correct |
450 ms |
14956 KB |
Output is correct |
41 |
Correct |
469 ms |
15468 KB |
Output is correct |
42 |
Correct |
452 ms |
15344 KB |
Output is correct |
43 |
Correct |
321 ms |
14956 KB |
Output is correct |
44 |
Correct |
321 ms |
14956 KB |
Output is correct |
45 |
Correct |
320 ms |
14956 KB |
Output is correct |
46 |
Correct |
336 ms |
15356 KB |
Output is correct |
47 |
Correct |
316 ms |
13420 KB |
Output is correct |
48 |
Correct |
351 ms |
15344 KB |
Output is correct |
49 |
Correct |
337 ms |
15724 KB |
Output is correct |