답안 #940469

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
940469 2024-03-07T09:43:14 Z a_l_i_r_e_z_a 운세 보기 2 (JOI14_fortune_telling2) C++17
100 / 100
174 ms 22292 KB
// In the name of God
// Hope is last to die
 
#include <bits/stdc++.h>
using namespace std;
 
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2")
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
#define pb push_back
// #define int long long
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
 
const int maxn = 2e5 + 5, lg = 19;
const int inf = 1e9 + 7;
int n, q, rmq[maxn][lg], fen[maxn];
pii a[maxn], b[maxn];

bool cmp(pii x, pii y){
    return max(x.F, x.S) > max(y.F, y.S);
}

void upd(int i){
    for(i++; i <= q; i += i & -i) fen[i]++;
}

int get_(int i){
    int res = 0;
    for(i++; i; i &= i - 1) res += fen[i];
    return res;
}

int get(int i){
    return get_(q - 1) - get_(i);
}

int get_rmq(int l, int r){
    int bit = 31 - __builtin_clz(r - l);
    return max(rmq[l][bit], rmq[r - (1ll << bit)][bit]);
}

int32_t main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n >> q;
    for(int i = 0; i < n ; i++) cin >> a[i].F >> a[i].S;
    sort(a, a + n, cmp);
    for(int i = 0; i < q; i++){
        cin >> b[i].F;
        b[i].S = i;
    }
    sort(b, b + q);
    for(int i = q - 1; i >= 0; i--){
        rmq[i][0] = b[i].S;
        for(int bit = 1; bit < lg; bit++){
            rmq[i][bit] = rmq[i][bit - 1];
            if(i + (1ll << (bit - 1)) < q){
                smax(rmq[i][bit], rmq[i + (1ll << (bit - 1))][bit - 1]);
            }
        }
    }
    int cur = q - 1;
    ll ans = 0;
    for(int i = 0; i < n; i++){
        int mn = min(a[i].F, a[i].S), mx = a[i].F + a[i].S - mn;
        while(cur >= 0 && b[cur].F >= mx) upd(b[cur--].S);
        int j = lower_bound(b, b + q, mp(mn, 0)) - b;
        int k = lower_bound(b, b + q, mp(mx, 0)) - b;
        if(j == q || b[j].F >= mx){
            int x = get(-1);
            if(x % 2) ans += a[i].S;
            else ans += a[i].F;
        }
        else{
            int g = get_rmq(j, k);
            int x = get(g);
            if(x % 2) ans += mn;
            else ans += mx;
        }
    }
    cout << ans << '\n';

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 1 ms 4596 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 1 ms 4600 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 2 ms 4444 KB Output is correct
12 Correct 2 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 1 ms 4596 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 1 ms 4600 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 2 ms 4444 KB Output is correct
12 Correct 2 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 9 ms 6980 KB Output is correct
15 Correct 17 ms 7256 KB Output is correct
16 Correct 19 ms 7560 KB Output is correct
17 Correct 28 ms 9556 KB Output is correct
18 Correct 29 ms 9572 KB Output is correct
19 Correct 24 ms 9592 KB Output is correct
20 Correct 27 ms 9688 KB Output is correct
21 Correct 25 ms 9596 KB Output is correct
22 Correct 18 ms 9308 KB Output is correct
23 Correct 19 ms 9188 KB Output is correct
24 Correct 25 ms 9560 KB Output is correct
25 Correct 20 ms 9152 KB Output is correct
26 Correct 26 ms 9588 KB Output is correct
27 Correct 26 ms 9576 KB Output is correct
28 Correct 24 ms 9744 KB Output is correct
29 Correct 23 ms 9592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4696 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 1 ms 4596 KB Output is correct
7 Correct 2 ms 4444 KB Output is correct
8 Correct 1 ms 4600 KB Output is correct
9 Correct 1 ms 4440 KB Output is correct
10 Correct 1 ms 4440 KB Output is correct
11 Correct 2 ms 4444 KB Output is correct
12 Correct 2 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 9 ms 6980 KB Output is correct
15 Correct 17 ms 7256 KB Output is correct
16 Correct 19 ms 7560 KB Output is correct
17 Correct 28 ms 9556 KB Output is correct
18 Correct 29 ms 9572 KB Output is correct
19 Correct 24 ms 9592 KB Output is correct
20 Correct 27 ms 9688 KB Output is correct
21 Correct 25 ms 9596 KB Output is correct
22 Correct 18 ms 9308 KB Output is correct
23 Correct 19 ms 9188 KB Output is correct
24 Correct 25 ms 9560 KB Output is correct
25 Correct 20 ms 9152 KB Output is correct
26 Correct 26 ms 9588 KB Output is correct
27 Correct 26 ms 9576 KB Output is correct
28 Correct 24 ms 9744 KB Output is correct
29 Correct 23 ms 9592 KB Output is correct
30 Correct 55 ms 20484 KB Output is correct
31 Correct 83 ms 21016 KB Output is correct
32 Correct 97 ms 21328 KB Output is correct
33 Correct 163 ms 22128 KB Output is correct
34 Correct 39 ms 20308 KB Output is correct
35 Correct 171 ms 22104 KB Output is correct
36 Correct 163 ms 22284 KB Output is correct
37 Correct 160 ms 22288 KB Output is correct
38 Correct 137 ms 22292 KB Output is correct
39 Correct 174 ms 22132 KB Output is correct
40 Correct 133 ms 22280 KB Output is correct
41 Correct 118 ms 22148 KB Output is correct
42 Correct 132 ms 22284 KB Output is correct
43 Correct 97 ms 21964 KB Output is correct
44 Correct 87 ms 22004 KB Output is correct
45 Correct 84 ms 21812 KB Output is correct
46 Correct 96 ms 21360 KB Output is correct
47 Correct 110 ms 21252 KB Output is correct
48 Correct 115 ms 22128 KB Output is correct
49 Correct 114 ms 22292 KB Output is correct