#include<bits/stdc++.h>
#define pb emplace_back
using namespace std;
const int N = int(2e5) + 1;
const int M = int(6e5) + 1;
vector<int> v, c[N];
int n, m, a[N], b[N], t[N], p[N];
int l[M << 2], h[M << 2], st[M << 2];
void Build(int x, int low, int high) {
l[x] = low, h[x] = high;
if(l[x] == h[x]) {
st[x] = p[l[x]];
return;
}
int mid = (low + high) >> 1;
Build(x << 1, low, mid);
Build(x << 1 | 1, mid + 1, high);
st[x] = max(st[x << 1], st[x << 1 | 1]);
}
int Query(int x, int low, int high) {
if(high < low) return -1;
if(l[x] > high || h[x] < low) return -1;
if(l[x] >= low && h[x] <= high) return st[x];
return max(Query(x << 1, low, high), Query(x << 1 | 1, low, high));
}
int Pos(int x) {return upper_bound(v.begin(), v.end(), x) - v.begin();}
void Update(int x) {
for(; x > 0; x -= (x & -x)) ++l[x];
}
int Get(int x) {
int res = 0;
for(; x <= int(v.size()); x += (x & -x)) res += l[x];
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
if(fopen("test.inp", "r")) {
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
cin >> n >> m;
for(int i = 0; i < n; ++i) {
cin >> a[i] >> b[i];
v.pb(a[i]), v.pb(b[i]);
}
for(int i = 0; i < m; ++i) cin >> t[i], v.pb(t[i]);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
memset(&p, -1, sizeof p);
for(int i = 0; i < m; ++i) p[t[i] = Pos(t[i])] = i;
Build(1, 1, int(v.size()));
for(int i = 0; i < n; ++i) {
a[i] = Pos(a[i]), b[i] = Pos(b[i]);
c[Query(1, min(a[i], b[i]), max(a[i], b[i]) - 1) + 1].pb(i);
}
fill(l, l + int(v.size()) + 1, 0);
long long res = 0;
for(int i = m; i >= 0; --i) {
if(i - m) Update(t[i]);
for(int id: c[i]) {
h[id] = Get(max(a[id], b[id]));
if(i == 0 && a[id] < b[id]) ++h[id];
if(h[id] & 1) res += v[min(a[id], b[id]) - 1];
else res += v[max(a[id], b[id]) - 1];
}
}
cout << res;
}
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:46:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("test.inp", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:47:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("test.out", "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5880 KB |
Output is correct |
2 |
Correct |
7 ms |
5880 KB |
Output is correct |
3 |
Correct |
8 ms |
6008 KB |
Output is correct |
4 |
Correct |
8 ms |
6008 KB |
Output is correct |
5 |
Correct |
8 ms |
6008 KB |
Output is correct |
6 |
Correct |
8 ms |
6008 KB |
Output is correct |
7 |
Correct |
8 ms |
6008 KB |
Output is correct |
8 |
Correct |
8 ms |
6008 KB |
Output is correct |
9 |
Correct |
8 ms |
6008 KB |
Output is correct |
10 |
Correct |
8 ms |
5880 KB |
Output is correct |
11 |
Correct |
8 ms |
5916 KB |
Output is correct |
12 |
Correct |
8 ms |
6008 KB |
Output is correct |
13 |
Correct |
9 ms |
6008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5880 KB |
Output is correct |
2 |
Correct |
7 ms |
5880 KB |
Output is correct |
3 |
Correct |
8 ms |
6008 KB |
Output is correct |
4 |
Correct |
8 ms |
6008 KB |
Output is correct |
5 |
Correct |
8 ms |
6008 KB |
Output is correct |
6 |
Correct |
8 ms |
6008 KB |
Output is correct |
7 |
Correct |
8 ms |
6008 KB |
Output is correct |
8 |
Correct |
8 ms |
6008 KB |
Output is correct |
9 |
Correct |
8 ms |
6008 KB |
Output is correct |
10 |
Correct |
8 ms |
5880 KB |
Output is correct |
11 |
Correct |
8 ms |
5916 KB |
Output is correct |
12 |
Correct |
8 ms |
6008 KB |
Output is correct |
13 |
Correct |
9 ms |
6008 KB |
Output is correct |
14 |
Correct |
24 ms |
7504 KB |
Output is correct |
15 |
Correct |
42 ms |
8568 KB |
Output is correct |
16 |
Correct |
64 ms |
10656 KB |
Output is correct |
17 |
Correct |
84 ms |
11328 KB |
Output is correct |
18 |
Correct |
82 ms |
11268 KB |
Output is correct |
19 |
Correct |
85 ms |
11216 KB |
Output is correct |
20 |
Correct |
92 ms |
11252 KB |
Output is correct |
21 |
Correct |
89 ms |
11376 KB |
Output is correct |
22 |
Correct |
63 ms |
10868 KB |
Output is correct |
23 |
Correct |
54 ms |
9332 KB |
Output is correct |
24 |
Correct |
53 ms |
9204 KB |
Output is correct |
25 |
Correct |
59 ms |
11124 KB |
Output is correct |
26 |
Correct |
60 ms |
10996 KB |
Output is correct |
27 |
Correct |
76 ms |
11224 KB |
Output is correct |
28 |
Correct |
63 ms |
11252 KB |
Output is correct |
29 |
Correct |
76 ms |
11496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5880 KB |
Output is correct |
2 |
Correct |
7 ms |
5880 KB |
Output is correct |
3 |
Correct |
8 ms |
6008 KB |
Output is correct |
4 |
Correct |
8 ms |
6008 KB |
Output is correct |
5 |
Correct |
8 ms |
6008 KB |
Output is correct |
6 |
Correct |
8 ms |
6008 KB |
Output is correct |
7 |
Correct |
8 ms |
6008 KB |
Output is correct |
8 |
Correct |
8 ms |
6008 KB |
Output is correct |
9 |
Correct |
8 ms |
6008 KB |
Output is correct |
10 |
Correct |
8 ms |
5880 KB |
Output is correct |
11 |
Correct |
8 ms |
5916 KB |
Output is correct |
12 |
Correct |
8 ms |
6008 KB |
Output is correct |
13 |
Correct |
9 ms |
6008 KB |
Output is correct |
14 |
Correct |
24 ms |
7504 KB |
Output is correct |
15 |
Correct |
42 ms |
8568 KB |
Output is correct |
16 |
Correct |
64 ms |
10656 KB |
Output is correct |
17 |
Correct |
84 ms |
11328 KB |
Output is correct |
18 |
Correct |
82 ms |
11268 KB |
Output is correct |
19 |
Correct |
85 ms |
11216 KB |
Output is correct |
20 |
Correct |
92 ms |
11252 KB |
Output is correct |
21 |
Correct |
89 ms |
11376 KB |
Output is correct |
22 |
Correct |
63 ms |
10868 KB |
Output is correct |
23 |
Correct |
54 ms |
9332 KB |
Output is correct |
24 |
Correct |
53 ms |
9204 KB |
Output is correct |
25 |
Correct |
59 ms |
11124 KB |
Output is correct |
26 |
Correct |
60 ms |
10996 KB |
Output is correct |
27 |
Correct |
76 ms |
11224 KB |
Output is correct |
28 |
Correct |
63 ms |
11252 KB |
Output is correct |
29 |
Correct |
76 ms |
11496 KB |
Output is correct |
30 |
Runtime error |
126 ms |
30084 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
31 |
Halted |
0 ms |
0 KB |
- |