답안 #1045164

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1045164 2024-08-05T17:53:20 Z ArashJafariyan 운세 보기 2 (JOI14_fortune_telling2) C++17
100 / 100
389 ms 88132 KB
#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx,avx2,sse,sse2,fma")

#define pb push_back
#define ll long long
#define i2 array<int, 2>
#define SZ(x) (int) (x).size()
#define int ll
#define pii pair<int, int>
#define F first
#define S second

int power(int a, int b, int mod) {
    int ret = 1;
    while (b) {
        if (b & 1) {
            ret = ret * a % mod;   
        }
        b >>= 1;
        a = a * a % mod;
    }
    return ret;
}

const int N = 2e5 + 8;

int n, k, node[(N * 3) << 2], fen[N];
pii A[N], T[N];
vector<int> all;
unordered_map<int, int> f, g;

void set_val(int i, int v, int x = 1, int l = 0, int r = N * 3) {
    if (r - l == 1) {
        node[x] = max(node[x], v);
        return;
    }
    int m = (l + r) >> 1;
    i < m ? set_val(i, v, x << 1, l, m) : set_val(i, v, x << 1 | 1, m, r);
    node[x] = max(node[x << 1], node[x << 1 | 1]);
}

int get(int l, int r, int x = 1, int lx = 0, int rx = N * 3) {
    if (l >= r) {
        return -1;
    }
    if (l == lx && r == rx) {
        return node[x];
    }
    int m = (lx + rx) >> 1;
    return max(get(l, min(r, m), x << 1, lx, m), get(max(l, m), r, x << 1 | 1, m, rx));
}

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

int get_fen(int i) {
    int ret = 0;
    for (i++; i; i -= i & -i) {
        ret += fen[i];
    }
    return ret;
}

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

    for (int i = 0; i < ((N * 3) << 2); i++) {
        node[i] = -1;
    }

    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> A[i].F >> A[i].S;     
        all.pb(A[i].F);
        all.pb(A[i].S);
    }
    for (int i = 0; i < k; i++) {
        cin >> T[i].F;
        T[i].S = i;
        all.pb(T[i].F);
    }

    sort(all.begin(), all.end());
    int give = 0;
    for (int v : all) {
        if (f.find(v) == f.end()) {
            f[v] = give;
            g[give] = v;
            give++;
        }
    }

    for (int i = 0; i < n; i++) {
        A[i].F = f[A[i].F];
        A[i].S = f[A[i].S];
    }
    for (int i = 0; i < k; i++) {
        T[i].F = f[T[i].F];
    }

    sort(A, A + n, [&](pii x, pii y) {
        return max(x.F, x.S) > max(y.F, y.S);        
    });
    sort(T, T + k, [&](pii x, pii y) {
        return x.F > y.F;
    });

    for (int i = 0; i < k; i++) {
        set_val(T[i].F, T[i].S);
    }

    /* cout << "deBUG: ---------\n";
    for (int i = 0; i < n; i++) {
        cout << A[i].F << ' ' << A[i].S << '\n';
    }
    cout << ".\n";
    for (int i = 0; i < k; i++) {
        cout << T[i].F << ' ' << T[i].S << '\n';
    }
    cout << ".\n";
    cout << get(0, 2);
    cout << "-------------\n"; */

    int pt = 0, ans = 0;
    for (int i = 0; i < n; i++) {
        auto [a, b] = A[i];
        while (pt < k && T[pt].F >= max(a, b)) {
            update(T[pt].S);
            pt++;
        }

        int last = get(min(a, b), max(a, b));
        // cout << "debug: " << last << '\n';

        if (last == -1) {
            int t = get_fen(N - 2);
            ans += (t % 2 ? g[b] : g[a]);
        }
        else {
            int t = get_fen(N - 2) - get_fen(last);
            if (a > b) {
                swap(a, b);
            }
            ans += (t % 2 ? g[a] : g[b]);
        }
    }
    cout << ans;

    return 0;
}


# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 24924 KB Output is correct
2 Correct 3 ms 24820 KB Output is correct
3 Correct 4 ms 23008 KB Output is correct
4 Correct 3 ms 23132 KB Output is correct
5 Correct 3 ms 23012 KB Output is correct
6 Correct 3 ms 23132 KB Output is correct
7 Correct 4 ms 23132 KB Output is correct
8 Correct 4 ms 24924 KB Output is correct
9 Correct 4 ms 22876 KB Output is correct
10 Correct 4 ms 24808 KB Output is correct
11 Correct 4 ms 24924 KB Output is correct
12 Correct 4 ms 25072 KB Output is correct
13 Correct 4 ms 24924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 24924 KB Output is correct
2 Correct 3 ms 24820 KB Output is correct
3 Correct 4 ms 23008 KB Output is correct
4 Correct 3 ms 23132 KB Output is correct
5 Correct 3 ms 23012 KB Output is correct
6 Correct 3 ms 23132 KB Output is correct
7 Correct 4 ms 23132 KB Output is correct
8 Correct 4 ms 24924 KB Output is correct
9 Correct 4 ms 22876 KB Output is correct
10 Correct 4 ms 24808 KB Output is correct
11 Correct 4 ms 24924 KB Output is correct
12 Correct 4 ms 25072 KB Output is correct
13 Correct 4 ms 24924 KB Output is correct
14 Correct 15 ms 28052 KB Output is correct
15 Correct 30 ms 33048 KB Output is correct
16 Correct 43 ms 37004 KB Output is correct
17 Correct 60 ms 39400 KB Output is correct
18 Correct 52 ms 39212 KB Output is correct
19 Correct 57 ms 39320 KB Output is correct
20 Correct 51 ms 39240 KB Output is correct
21 Correct 50 ms 39212 KB Output is correct
22 Correct 33 ms 35020 KB Output is correct
23 Correct 33 ms 33744 KB Output is correct
24 Correct 29 ms 33076 KB Output is correct
25 Correct 34 ms 37016 KB Output is correct
26 Correct 36 ms 34776 KB Output is correct
27 Correct 41 ms 35288 KB Output is correct
28 Correct 41 ms 35532 KB Output is correct
29 Correct 47 ms 36160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 24924 KB Output is correct
2 Correct 3 ms 24820 KB Output is correct
3 Correct 4 ms 23008 KB Output is correct
4 Correct 3 ms 23132 KB Output is correct
5 Correct 3 ms 23012 KB Output is correct
6 Correct 3 ms 23132 KB Output is correct
7 Correct 4 ms 23132 KB Output is correct
8 Correct 4 ms 24924 KB Output is correct
9 Correct 4 ms 22876 KB Output is correct
10 Correct 4 ms 24808 KB Output is correct
11 Correct 4 ms 24924 KB Output is correct
12 Correct 4 ms 25072 KB Output is correct
13 Correct 4 ms 24924 KB Output is correct
14 Correct 15 ms 28052 KB Output is correct
15 Correct 30 ms 33048 KB Output is correct
16 Correct 43 ms 37004 KB Output is correct
17 Correct 60 ms 39400 KB Output is correct
18 Correct 52 ms 39212 KB Output is correct
19 Correct 57 ms 39320 KB Output is correct
20 Correct 51 ms 39240 KB Output is correct
21 Correct 50 ms 39212 KB Output is correct
22 Correct 33 ms 35020 KB Output is correct
23 Correct 33 ms 33744 KB Output is correct
24 Correct 29 ms 33076 KB Output is correct
25 Correct 34 ms 37016 KB Output is correct
26 Correct 36 ms 34776 KB Output is correct
27 Correct 41 ms 35288 KB Output is correct
28 Correct 41 ms 35532 KB Output is correct
29 Correct 47 ms 36160 KB Output is correct
30 Correct 104 ms 50200 KB Output is correct
31 Correct 148 ms 57024 KB Output is correct
32 Correct 263 ms 70124 KB Output is correct
33 Correct 375 ms 88132 KB Output is correct
34 Correct 97 ms 48436 KB Output is correct
35 Correct 389 ms 87008 KB Output is correct
36 Correct 385 ms 87984 KB Output is correct
37 Correct 378 ms 87220 KB Output is correct
38 Correct 347 ms 86708 KB Output is correct
39 Correct 361 ms 87452 KB Output is correct
40 Correct 342 ms 87216 KB Output is correct
41 Correct 344 ms 86532 KB Output is correct
42 Correct 363 ms 86452 KB Output is correct
43 Correct 218 ms 86200 KB Output is correct
44 Correct 212 ms 87164 KB Output is correct
45 Correct 219 ms 86780 KB Output is correct
46 Correct 156 ms 60768 KB Output is correct
47 Correct 143 ms 54656 KB Output is correct
48 Correct 246 ms 76728 KB Output is correct
49 Correct 247 ms 76980 KB Output is correct