답안 #908754

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
908754 2024-01-16T19:22:18 Z typ_ik 운세 보기 2 (JOI14_fortune_telling2) C++17
100 / 100
324 ms 60616 KB
#include <bits/stdc++.h>

#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define watch(x) cout << (#x) <<  " : " << x << '\n'
#define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;   

const int N = 2e5 + 128;
int n, k;
int a[N], b[N], c[N];
vector <int> t[4 * N];

void build(int v, int tl, int tr) {
    if (tl == tr) {
        t[v].push_back(c[tl]);
        return;
    }
    int tm = (tl + tr) >> 1;
    build(v << 1, tl, tm);
    build(v << 1 | 1, tm + 1, tr);
    merge(all(t[v << 1]), all(t[v << 1 | 1]), back_inserter(t[v]));
}

int get(int l, int r, int x, int v, int tl, int tr) {
    if (tl > r || tr < l)
        return 0;
    if (l <= tl && tr <= r)
        return (int)t[v].size() - (int)(lower_bound(all(t[v]), x) - t[v].begin());
    int tm = (tl + tr) >> 1;
    return get(l, r, x, v << 1, tl, tm) +
            get(l, r, x, v << 1 | 1, tm + 1, tr);
} 

const int INF = 2e9 + 128;
int mx[4 * N];

void update(int pos, int val, int v, int tl, int tr) {
    if (tl > pos || tr < pos)
        return;
    if (tl == tr) {
        mx[v] = val;
        return;
    }
    int tm = (tl + tr) >> 1;
    update(pos, val, v << 1, tl, tm);
    update(pos, val, v << 1 | 1, tm + 1, tr);
    mx[v] = max(mx[v << 1], mx[v << 1 | 1]);
}

int rightmost(int l, int r, int x, int v, int tl, int tr) {
    if (tl > r || tr < l || mx[v] < x)
        return -1; 
    if (tl == tr)
        return tl;
    int tm = (tl + tr) >> 1;
    if (l <= tl && tr <= r) { 
        if (mx[v << 1 | 1] >= x)
            return rightmost(l, r, x, v << 1 | 1, tm + 1, tr);
        else if (mx[v << 1] >= x)
            return rightmost(l, r, x, v << 1, tl, tm); 
        return -1;
    } 
    return max(rightmost(l, r, x, v << 1, tl, tm), rightmost(l, r, x, v << 1 | 1, tm + 1, tr));
} 

void solve() {  
    cin >> n >> k;
    for (int i = 0; i < n; i++)
        cin >> a[i] >> b[i];
    for (int i = 0; i < k; i++)
        cin >> c[i]; 
    
    build(1, 0, k - 1);

    vector <int> order(n);  
    iota(all(order), 0);
    sort(all(order), [&](const int& x, const int& y) -> bool {
        return max(a[x], b[x]) < max(a[y], b[y]); 
    });

    vector < pair<int, int> > to_add;
    for (int i = 0; i < k; i++)
        to_add.push_back(make_pair(c[i], i));
    sort(all(to_add));
    
    ll ans = 0;

    vector <int> pref(k);
    for (int i = 0; i < k; i++)
        pref[i] = max((i ? pref[i - 1] : 0), c[i]); 
 
    for (int i = 0; i < k; i++)
        update(i, -INF, 1, 0, k - 1); 
    
    int pt = 0;

    for (auto& i : order) {  
        int x = -1;  
        if (a[i] > b[i]) { 
            x = (int)(lower_bound(all(pref), a[i]) - pref.begin());  
            if (x == k) { 
                ans += a[i];
                continue;
            }
            swap(a[i], b[i]);
        }

        assert(a[i] <= b[i]);
        while (pt < k && to_add[pt].first < b[i])
            update(to_add[pt].second, to_add[pt].first, 1, 0, k - 1), pt++; 

        int pos = rightmost(x + 1, k - 1, a[i], 1, 0, k - 1);  

        bool was = (pos < 0);
        if (was)
            pos = x; 

        int lefts = get(pos + 1, k - 1, b[i], 1, 0, k - 1);   
        
        ans += ((lefts % 2 == was) ? b[i] : a[i]); 
    } 

    cout << ans << '\n';
}

main() {
    boost;
    solve();
    return 0;
}

Compilation message

fortune_telling2.cpp:129:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  129 | main() {
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 21080 KB Output is correct
2 Correct 7 ms 21080 KB Output is correct
3 Correct 7 ms 21084 KB Output is correct
4 Correct 7 ms 21084 KB Output is correct
5 Correct 7 ms 21084 KB Output is correct
6 Correct 8 ms 21080 KB Output is correct
7 Correct 7 ms 21084 KB Output is correct
8 Correct 7 ms 20996 KB Output is correct
9 Correct 8 ms 21084 KB Output is correct
10 Correct 9 ms 21084 KB Output is correct
11 Correct 8 ms 21084 KB Output is correct
12 Correct 7 ms 21084 KB Output is correct
13 Correct 7 ms 21080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 21080 KB Output is correct
2 Correct 7 ms 21080 KB Output is correct
3 Correct 7 ms 21084 KB Output is correct
4 Correct 7 ms 21084 KB Output is correct
5 Correct 7 ms 21084 KB Output is correct
6 Correct 8 ms 21080 KB Output is correct
7 Correct 7 ms 21084 KB Output is correct
8 Correct 7 ms 20996 KB Output is correct
9 Correct 8 ms 21084 KB Output is correct
10 Correct 9 ms 21084 KB Output is correct
11 Correct 8 ms 21084 KB Output is correct
12 Correct 7 ms 21084 KB Output is correct
13 Correct 7 ms 21080 KB Output is correct
14 Correct 18 ms 23128 KB Output is correct
15 Correct 30 ms 27064 KB Output is correct
16 Correct 42 ms 28152 KB Output is correct
17 Correct 57 ms 30760 KB Output is correct
18 Correct 59 ms 30596 KB Output is correct
19 Correct 57 ms 30604 KB Output is correct
20 Correct 62 ms 30744 KB Output is correct
21 Correct 56 ms 30524 KB Output is correct
22 Correct 45 ms 30092 KB Output is correct
23 Correct 49 ms 30092 KB Output is correct
24 Correct 58 ms 30088 KB Output is correct
25 Correct 45 ms 30352 KB Output is correct
26 Correct 53 ms 30348 KB Output is correct
27 Correct 57 ms 30564 KB Output is correct
28 Correct 57 ms 30704 KB Output is correct
29 Correct 60 ms 30768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 21080 KB Output is correct
2 Correct 7 ms 21080 KB Output is correct
3 Correct 7 ms 21084 KB Output is correct
4 Correct 7 ms 21084 KB Output is correct
5 Correct 7 ms 21084 KB Output is correct
6 Correct 8 ms 21080 KB Output is correct
7 Correct 7 ms 21084 KB Output is correct
8 Correct 7 ms 20996 KB Output is correct
9 Correct 8 ms 21084 KB Output is correct
10 Correct 9 ms 21084 KB Output is correct
11 Correct 8 ms 21084 KB Output is correct
12 Correct 7 ms 21084 KB Output is correct
13 Correct 7 ms 21080 KB Output is correct
14 Correct 18 ms 23128 KB Output is correct
15 Correct 30 ms 27064 KB Output is correct
16 Correct 42 ms 28152 KB Output is correct
17 Correct 57 ms 30760 KB Output is correct
18 Correct 59 ms 30596 KB Output is correct
19 Correct 57 ms 30604 KB Output is correct
20 Correct 62 ms 30744 KB Output is correct
21 Correct 56 ms 30524 KB Output is correct
22 Correct 45 ms 30092 KB Output is correct
23 Correct 49 ms 30092 KB Output is correct
24 Correct 58 ms 30088 KB Output is correct
25 Correct 45 ms 30352 KB Output is correct
26 Correct 53 ms 30348 KB Output is correct
27 Correct 57 ms 30564 KB Output is correct
28 Correct 57 ms 30704 KB Output is correct
29 Correct 60 ms 30768 KB Output is correct
30 Correct 188 ms 54740 KB Output is correct
31 Correct 215 ms 55748 KB Output is correct
32 Correct 253 ms 57692 KB Output is correct
33 Correct 299 ms 60608 KB Output is correct
34 Correct 176 ms 54468 KB Output is correct
35 Correct 302 ms 60372 KB Output is correct
36 Correct 298 ms 60388 KB Output is correct
37 Correct 295 ms 60608 KB Output is correct
38 Correct 305 ms 60424 KB Output is correct
39 Correct 318 ms 60616 KB Output is correct
40 Correct 281 ms 60204 KB Output is correct
41 Correct 324 ms 60548 KB Output is correct
42 Correct 299 ms 60608 KB Output is correct
43 Correct 226 ms 59836 KB Output is correct
44 Correct 232 ms 59872 KB Output is correct
45 Correct 224 ms 59840 KB Output is correct
46 Correct 236 ms 58696 KB Output is correct
47 Correct 251 ms 58564 KB Output is correct
48 Correct 302 ms 60476 KB Output is correct
49 Correct 280 ms 60544 KB Output is correct