#include <bits/stdc++.h>
#define nl "\n"
#define no "NO"
#define yes "YES"
#define fi first
#define se second
#define vec vector
#define task "main"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())
using namespace std;
template<typename U, typename V> bool maxi(U &a, V b) {
if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool mini(U &a, V b) {
if (a > b) { a = b; return 1; } return 0;
}
const int N = (int)2e5 + 9;
const int mod = (int)1e9 + 7;
void prepare(); void main_code();
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
const bool MULTITEST = 0; prepare();
int num_test = 1; if (MULTITEST) cin >> num_test;
while (num_test--) { main_code(); }
}
void prepare() {};
template<typename T>
struct fenwick {
int n; vector<T> bit; bool up = 0;
fenwick() {};
fenwick(int _n, bool _up) { n = _n; bit.resize(n + 7); up = _up; }
void upd(int id, T val) {
if (up) for(; id <= n; id += id & -id) bit[id] += val;
else for(; id > 0; id -= id & -id) bit[id] += val;
}
T get(int id) {
T ans = 0;
if (up) for(; id > 0; id -= id & -id) ans += bit[id];
else for(; id <= n; id += id & -id) ans += bit[id];
return ans;
}
};
const int LG = 17;
int sp[N][LG + 1];
int a[N], b[N], t[N];
void main_code() {
int n, k; cin >> n >> k;
for(int i = 1; i <= n; ++i) {
cin >> a[i] >> b[i];
}
for(int i = 1; i <= k; ++i) {
cin >> t[i];
}
vector<pair<int, int>> o;
FOR(i, 1, k) o.push_back(_mp(t[i], i));
sort(all(o));
FOR(i, 1, k) {
sp[i][0] = o[i - 1].second;
}
vec<int> lg(k + 1, 0), luu(n + 1, 0);
FOR(i, 2, k) lg[i] = lg[i / 2] + 1;
FOR(j, 1, LG) if ( (1 << j) <= k ) {
for(int i = 1; i + (1 << j) - 1 <= k; ++i) {
sp[i][j] = max(sp[i][j - 1], sp[i + (1 << (j - 1))][j - 1]);
}
}
auto get = [&](int l, int r){
int j = lg[r - l + 1];
return max(sp[l][j], sp[r - (1 << j) + 1][j]);
};
fenwick<int> bit(k, 0);
FOR(i, 1, n) if (a[i] != b[i]) {
int l = min(a[i], b[i]);
int r = max(a[i], b[i]) - 1;
if (o.back().fi < l) continue ;
if (o[0].fi > r) continue ;
int ll = lower_bound(all(o), _mp(l, 0)) - o.begin() + 1;
int rr = (--upper_bound(all(o), _mp(r, INT_MAX))) - o.begin() + 1;
if (ll <= rr) {
luu[i] = get(ll, rr);
}
}
// FOR(i, 1, n) cout << luu[i] << ' ' ; cout << nl;
long long ans = 0;
vec<ii> ev;
FOR(i, 1, n) {
if (a[i] == b[i]) {
ans += a[i];
continue ;
}
ev.push_back(_mp(max(b[i],a[i]),i));
}
FOR(i, 1, k) {
ev.push_back(_mp(t[i], -i));
}
sort(ev.begin(), ev.end(), greater<ii>());
int m = sz(ev);
for(int i=0,j=0;i<m;++i){
while(j+1<m and ev[j+1].fi==ev[i].fi)++j;
FOR(k,i,j)if(ev[k].se<0){
int p=-ev[k].se;
bit.upd(p,1);
}
FOR(k,i,j)if(ev[k].se>0){
//tra loi
int p=ev[k].se;
if (luu[p]==0){
int t=bit.get(1);
if(t % 2 == 0) ans += a[p];
else ans += b[p];
}else{
int t=bit.get(luu[p]+1);
if(t % 2 == 0) ans += max(a[p], b[p]);
else ans += min(a[p], b[p]);
}
}
i = j;
}
cout << ans;
}
/* Let the river flows naturally */
Compilation message
fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4432 KB |
Output is correct |
2 |
Correct |
2 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
2 ms |
4600 KB |
Output is correct |
6 |
Correct |
2 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
2 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
2 ms |
4432 KB |
Output is correct |
11 |
Correct |
2 ms |
4600 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
2 ms |
4432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4432 KB |
Output is correct |
2 |
Correct |
2 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
2 ms |
4600 KB |
Output is correct |
6 |
Correct |
2 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
2 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
2 ms |
4432 KB |
Output is correct |
11 |
Correct |
2 ms |
4600 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
2 ms |
4432 KB |
Output is correct |
14 |
Correct |
8 ms |
5200 KB |
Output is correct |
15 |
Correct |
15 ms |
5456 KB |
Output is correct |
16 |
Correct |
21 ms |
7760 KB |
Output is correct |
17 |
Correct |
28 ms |
8556 KB |
Output is correct |
18 |
Correct |
28 ms |
8604 KB |
Output is correct |
19 |
Correct |
28 ms |
8532 KB |
Output is correct |
20 |
Correct |
28 ms |
8532 KB |
Output is correct |
21 |
Correct |
27 ms |
8532 KB |
Output is correct |
22 |
Correct |
21 ms |
8532 KB |
Output is correct |
23 |
Correct |
21 ms |
8532 KB |
Output is correct |
24 |
Correct |
21 ms |
8532 KB |
Output is correct |
25 |
Correct |
20 ms |
8536 KB |
Output is correct |
26 |
Correct |
22 ms |
8528 KB |
Output is correct |
27 |
Correct |
25 ms |
8532 KB |
Output is correct |
28 |
Correct |
24 ms |
8560 KB |
Output is correct |
29 |
Correct |
31 ms |
8544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4432 KB |
Output is correct |
2 |
Correct |
2 ms |
4432 KB |
Output is correct |
3 |
Correct |
2 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
2 ms |
4600 KB |
Output is correct |
6 |
Correct |
2 ms |
4432 KB |
Output is correct |
7 |
Correct |
1 ms |
4432 KB |
Output is correct |
8 |
Correct |
2 ms |
4432 KB |
Output is correct |
9 |
Correct |
1 ms |
4432 KB |
Output is correct |
10 |
Correct |
2 ms |
4432 KB |
Output is correct |
11 |
Correct |
2 ms |
4600 KB |
Output is correct |
12 |
Correct |
1 ms |
4432 KB |
Output is correct |
13 |
Correct |
2 ms |
4432 KB |
Output is correct |
14 |
Correct |
8 ms |
5200 KB |
Output is correct |
15 |
Correct |
15 ms |
5456 KB |
Output is correct |
16 |
Correct |
21 ms |
7760 KB |
Output is correct |
17 |
Correct |
28 ms |
8556 KB |
Output is correct |
18 |
Correct |
28 ms |
8604 KB |
Output is correct |
19 |
Correct |
28 ms |
8532 KB |
Output is correct |
20 |
Correct |
28 ms |
8532 KB |
Output is correct |
21 |
Correct |
27 ms |
8532 KB |
Output is correct |
22 |
Correct |
21 ms |
8532 KB |
Output is correct |
23 |
Correct |
21 ms |
8532 KB |
Output is correct |
24 |
Correct |
21 ms |
8532 KB |
Output is correct |
25 |
Correct |
20 ms |
8536 KB |
Output is correct |
26 |
Correct |
22 ms |
8528 KB |
Output is correct |
27 |
Correct |
25 ms |
8532 KB |
Output is correct |
28 |
Correct |
24 ms |
8560 KB |
Output is correct |
29 |
Correct |
31 ms |
8544 KB |
Output is correct |
30 |
Correct |
64 ms |
24512 KB |
Output is correct |
31 |
Correct |
82 ms |
24768 KB |
Output is correct |
32 |
Correct |
104 ms |
27000 KB |
Output is correct |
33 |
Correct |
149 ms |
27524 KB |
Output is correct |
34 |
Correct |
58 ms |
24512 KB |
Output is correct |
35 |
Correct |
150 ms |
27460 KB |
Output is correct |
36 |
Correct |
151 ms |
27460 KB |
Output is correct |
37 |
Correct |
150 ms |
27460 KB |
Output is correct |
38 |
Correct |
152 ms |
27340 KB |
Output is correct |
39 |
Correct |
155 ms |
27460 KB |
Output is correct |
40 |
Correct |
144 ms |
27460 KB |
Output is correct |
41 |
Correct |
211 ms |
27460 KB |
Output is correct |
42 |
Correct |
172 ms |
27340 KB |
Output is correct |
43 |
Correct |
111 ms |
27460 KB |
Output is correct |
44 |
Correct |
110 ms |
27508 KB |
Output is correct |
45 |
Correct |
113 ms |
27460 KB |
Output is correct |
46 |
Correct |
122 ms |
27460 KB |
Output is correct |
47 |
Correct |
120 ms |
27336 KB |
Output is correct |
48 |
Correct |
154 ms |
27460 KB |
Output is correct |
49 |
Correct |
139 ms |
27460 KB |
Output is correct |