#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
class segment_tree
{
#define lc id << 1
#define rc id << 1 | 1
vector<int> ST;
public:
segment_tree(int n)
{
ST.assign(4 * n + 5, 0);
}
void update(int id, int l, int r, int pos, int val)
{
if (l > pos || r < pos) return;
if (l == r){
ST[id] = val;
return;
}
int mid = (l + r) / 2;
update(lc, l, mid, pos, val); update(rc, mid + 1, r, pos, val);
ST[id] = max(ST[lc], ST[rc]);
}
int query(int id, int l, int r, int L, int R)
{
if (l > R || L > r) return 0;
if (L <= l && r <= R) return ST[id];
int mid = (l + r) / 2;
return max(query(lc, l, mid, L, R), query(rc, mid + 1, r, L, R));
}
};
class fenwick_tree
{
vector<int> cnt; int n;
public:
fenwick_tree(int _n)
{
n = _n;
cnt.assign(n + 5, 0);
}
void update(int i, int v)
{
for (; i; i -= i & -i){
cnt[i] += v;
}
}
int sum(int i)
{
int res = 0;
for (; i <= n; i += i & -i){
res += cnt[i];
}
return res;
}
};
int N, M;
int a[maxn], b[maxn], c[maxn];
vector<int> val;
vector<int> query[maxn];
int compress(int x)
{
return lower_bound(val.begin(), val.end(), x) - val.begin() + 1;
}
signed main(void)
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen("A.INP", "r")){
freopen("A.INP", "r", stdin);
freopen("A.OUT", "w", stdout);
}
cin >> N >> M;
for (int i = 1; i <= N; ++i){
cin >> a[i] >> b[i];
val.pb(a[i]); val.pb(b[i]);
}
for (int i = 1; i <= M; ++i){
cin >> c[i];
val.pb(c[i]);
}
sort(val.begin(), val.end()); val.erase(unique(val.begin(), val.end()), val.end());
segment_tree ST(val.size() + 5);
fenwick_tree FT(val.size() + 5);
for (int i = 1; i <= M; ++i){
c[i] = compress(c[i]);
ST.update(1, 1, val.size(), c[i], i);
}
ll res = 0;
for (int i = 1; i <= N; ++i){
if (a[i] == b[i]){
res += a[i];
continue;
}
a[i] = compress(a[i]);
b[i] = compress(b[i]);
int t = ST.query(1, 1, val.size(), min(a[i], b[i]), max(a[i], b[i]) - 1);
if (t && a[i] < b[i]) swap(a[i], b[i]);
query[t].pb(i);
}
for (int i = M; i >= 0; --i){
for (auto id : query[i]){
int v = FT.sum(max(a[id], b[id]));
if (v & 1) swap(a[id], b[id]);
res += val[a[id] - 1];
}
if (i) FT.update(c[i], 1);
}
cout << res << '\n';
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:82:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("A.INP", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:83:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("A.OUT", "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5016 KB |
Output is correct |
2 |
Correct |
10 ms |
5088 KB |
Output is correct |
3 |
Correct |
10 ms |
5160 KB |
Output is correct |
4 |
Correct |
11 ms |
5116 KB |
Output is correct |
5 |
Correct |
49 ms |
5160 KB |
Output is correct |
6 |
Correct |
10 ms |
5112 KB |
Output is correct |
7 |
Correct |
9 ms |
5112 KB |
Output is correct |
8 |
Correct |
10 ms |
5112 KB |
Output is correct |
9 |
Correct |
9 ms |
5112 KB |
Output is correct |
10 |
Correct |
9 ms |
5112 KB |
Output is correct |
11 |
Correct |
9 ms |
5112 KB |
Output is correct |
12 |
Correct |
11 ms |
5112 KB |
Output is correct |
13 |
Correct |
14 ms |
5112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5016 KB |
Output is correct |
2 |
Correct |
10 ms |
5088 KB |
Output is correct |
3 |
Correct |
10 ms |
5160 KB |
Output is correct |
4 |
Correct |
11 ms |
5116 KB |
Output is correct |
5 |
Correct |
49 ms |
5160 KB |
Output is correct |
6 |
Correct |
10 ms |
5112 KB |
Output is correct |
7 |
Correct |
9 ms |
5112 KB |
Output is correct |
8 |
Correct |
10 ms |
5112 KB |
Output is correct |
9 |
Correct |
9 ms |
5112 KB |
Output is correct |
10 |
Correct |
9 ms |
5112 KB |
Output is correct |
11 |
Correct |
9 ms |
5112 KB |
Output is correct |
12 |
Correct |
11 ms |
5112 KB |
Output is correct |
13 |
Correct |
14 ms |
5112 KB |
Output is correct |
14 |
Correct |
24 ms |
6008 KB |
Output is correct |
15 |
Correct |
44 ms |
6792 KB |
Output is correct |
16 |
Correct |
71 ms |
7700 KB |
Output is correct |
17 |
Correct |
87 ms |
8560 KB |
Output is correct |
18 |
Correct |
91 ms |
8692 KB |
Output is correct |
19 |
Correct |
83 ms |
8564 KB |
Output is correct |
20 |
Correct |
82 ms |
8564 KB |
Output is correct |
21 |
Correct |
84 ms |
8820 KB |
Output is correct |
22 |
Correct |
66 ms |
7796 KB |
Output is correct |
23 |
Correct |
61 ms |
7412 KB |
Output is correct |
24 |
Correct |
62 ms |
7128 KB |
Output is correct |
25 |
Correct |
64 ms |
8180 KB |
Output is correct |
26 |
Correct |
68 ms |
7540 KB |
Output is correct |
27 |
Correct |
73 ms |
7796 KB |
Output is correct |
28 |
Correct |
69 ms |
7796 KB |
Output is correct |
29 |
Correct |
76 ms |
8308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5016 KB |
Output is correct |
2 |
Correct |
10 ms |
5088 KB |
Output is correct |
3 |
Correct |
10 ms |
5160 KB |
Output is correct |
4 |
Correct |
11 ms |
5116 KB |
Output is correct |
5 |
Correct |
49 ms |
5160 KB |
Output is correct |
6 |
Correct |
10 ms |
5112 KB |
Output is correct |
7 |
Correct |
9 ms |
5112 KB |
Output is correct |
8 |
Correct |
10 ms |
5112 KB |
Output is correct |
9 |
Correct |
9 ms |
5112 KB |
Output is correct |
10 |
Correct |
9 ms |
5112 KB |
Output is correct |
11 |
Correct |
9 ms |
5112 KB |
Output is correct |
12 |
Correct |
11 ms |
5112 KB |
Output is correct |
13 |
Correct |
14 ms |
5112 KB |
Output is correct |
14 |
Correct |
24 ms |
6008 KB |
Output is correct |
15 |
Correct |
44 ms |
6792 KB |
Output is correct |
16 |
Correct |
71 ms |
7700 KB |
Output is correct |
17 |
Correct |
87 ms |
8560 KB |
Output is correct |
18 |
Correct |
91 ms |
8692 KB |
Output is correct |
19 |
Correct |
83 ms |
8564 KB |
Output is correct |
20 |
Correct |
82 ms |
8564 KB |
Output is correct |
21 |
Correct |
84 ms |
8820 KB |
Output is correct |
22 |
Correct |
66 ms |
7796 KB |
Output is correct |
23 |
Correct |
61 ms |
7412 KB |
Output is correct |
24 |
Correct |
62 ms |
7128 KB |
Output is correct |
25 |
Correct |
64 ms |
8180 KB |
Output is correct |
26 |
Correct |
68 ms |
7540 KB |
Output is correct |
27 |
Correct |
73 ms |
7796 KB |
Output is correct |
28 |
Correct |
69 ms |
7796 KB |
Output is correct |
29 |
Correct |
76 ms |
8308 KB |
Output is correct |
30 |
Correct |
159 ms |
11244 KB |
Output is correct |
31 |
Correct |
230 ms |
16512 KB |
Output is correct |
32 |
Correct |
318 ms |
20696 KB |
Output is correct |
33 |
Correct |
519 ms |
28764 KB |
Output is correct |
34 |
Correct |
149 ms |
12656 KB |
Output is correct |
35 |
Correct |
514 ms |
28640 KB |
Output is correct |
36 |
Correct |
502 ms |
28508 KB |
Output is correct |
37 |
Correct |
520 ms |
28492 KB |
Output is correct |
38 |
Correct |
522 ms |
28640 KB |
Output is correct |
39 |
Correct |
501 ms |
28420 KB |
Output is correct |
40 |
Correct |
460 ms |
28896 KB |
Output is correct |
41 |
Correct |
516 ms |
28384 KB |
Output is correct |
42 |
Correct |
512 ms |
28256 KB |
Output is correct |
43 |
Correct |
348 ms |
28384 KB |
Output is correct |
44 |
Correct |
336 ms |
28640 KB |
Output is correct |
45 |
Correct |
333 ms |
28384 KB |
Output is correct |
46 |
Correct |
317 ms |
20704 KB |
Output is correct |
47 |
Correct |
292 ms |
18528 KB |
Output is correct |
48 |
Correct |
380 ms |
24544 KB |
Output is correct |
49 |
Correct |
364 ms |
24816 KB |
Output is correct |