Submission #871409

# Submission time Handle Problem Language Result Execution time Memory
871409 2023-11-10T18:40:31 Z LOLOLO Fortune Telling 2 (JOI14_fortune_telling2) C++17
100 / 100
286 ms 33452 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())
 
const int N = 4e6 + 100;
struct fen{
    vector <ll> f;
    int n;
    fen(int N) {
        f.assign(N + 1, 0);
        n = N;
    }

    void upd(int i, int x) {
        for (; i <= n; i += i & (-i))
            f[i] += x;
    }

    ll get(int i) {
        ll s = 0;
        for (; i; i -= i & (-i))
            s += f[i];

        return s;
    }

    ll range(int l, int r) {
        return get(r) - get(l - 1);
    }
};

struct seg{
    vector <ll> v;
    int n;

    seg(int N) {
        n = N;
        v.assign(n * 4 + 100, 0);
    }

    void upd(int id, int l, int r, int u, int x) {
        if (l > u || r < u)
            return;

        if (l == r) {
            v[id] = max(v[id], (ll)x);
            return;
        }

        int m = (l + r) / 2;
        upd(id * 2, l, m, u, x);
        upd(id * 2 + 1, m + 1, r, u, x);
        v[id] = max(v[id * 2], v[id * 2 + 1]);
    }

    int get(int id, int l, int r, int u, int v) {
        if (l > v || r < u || u > v)
            return 0;

        if (l >= u && r <= v)
            return this -> v[id];

        int m = (l + r) / 2;
        return max(get(id * 2, l, m, u, v), get(id * 2 + 1, m + 1, r, u, v));
    }
};

bool cmp(pair <int, int> &a, pair <int, int> &b) {
    return max(a.f, a.s) > max(b.f, b.s);
}

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

    int n, q;
    cin >> n >> q;

    vector <pair <int, int>> st;
    vector <pair <int, int>> save;
    vector <int> coor, rev;

    for (int i = 0; i < n; i++) {
        int a, b;
        cin >> a >> b;
        st.pb({a, b});
        coor.pb(a);
        coor.pb(b);
    }

    for (int i = 1; i <= q; i++) {
        int x;
        cin >> x;
        save.pb({x, i});
        coor.pb(x);
    }

    coor.pb(0);
    uniquev(coor);
    sort(all(st), cmp);
    sort(all(save), greater <pair <int, int>>());
    int sz = n * 2 + q + 10;
    rev.resize(sz);

    for (auto &x : st) {
        int ind = 0;
        ind = lower_bound(all(coor), x.f) - coor.begin();
        rev[ind] = x.f;
        x.f = ind;

        ind = lower_bound(all(coor), x.s) - coor.begin();
        rev[ind] = x.s;
        x.s = ind;
    }

    
    seg ST(sz);

    for (auto &x : save) {
        int ind = 0;
        ind = lower_bound(all(coor), x.f) - coor.begin();
        rev[ind] = x.f;
        x.f = ind;
        ST.upd(1, 1, sz, x.f, x.s);
    }

    ll ans = 0, j = 0;
    fen F(q + 4);

    for (auto x : st) {
        int mx = max(x.f, x.s), mi = min(x.f, x.s);
        int id = 0;

        if (mx != mi) {
            id = ST.get(1, 1, sz, mi, mx - 1);
        }

        while (j < sz(save) && save[j].f >= mx) {
            F.upd(save[j].s, 1);
            j++;
        }

        if (id == 0) {
            if (F.range(1, q) % 2) {
                ans += rev[x.s];
            } else {
                ans += rev[x.f];
            }
        } else {
            if (F.range(id + 1, q) % 2) {
                ans += rev[mi];
            } else {
                ans += rev[mx];
            }            
        }
    }

    cout << ans << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 508 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 508 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
14 Correct 12 ms 2136 KB Output is correct
15 Correct 28 ms 3576 KB Output is correct
16 Correct 47 ms 5320 KB Output is correct
17 Correct 48 ms 6888 KB Output is correct
18 Correct 48 ms 7004 KB Output is correct
19 Correct 61 ms 7072 KB Output is correct
20 Correct 48 ms 6848 KB Output is correct
21 Correct 56 ms 6764 KB Output is correct
22 Correct 38 ms 6724 KB Output is correct
23 Correct 46 ms 6728 KB Output is correct
24 Correct 47 ms 6744 KB Output is correct
25 Correct 38 ms 6724 KB Output is correct
26 Correct 35 ms 6428 KB Output is correct
27 Correct 52 ms 6904 KB Output is correct
28 Correct 39 ms 6808 KB Output is correct
29 Correct 41 ms 6976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 508 KB Output is correct
3 Correct 2 ms 604 KB Output is correct
4 Correct 2 ms 604 KB Output is correct
5 Correct 2 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 600 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 2 ms 604 KB Output is correct
14 Correct 12 ms 2136 KB Output is correct
15 Correct 28 ms 3576 KB Output is correct
16 Correct 47 ms 5320 KB Output is correct
17 Correct 48 ms 6888 KB Output is correct
18 Correct 48 ms 7004 KB Output is correct
19 Correct 61 ms 7072 KB Output is correct
20 Correct 48 ms 6848 KB Output is correct
21 Correct 56 ms 6764 KB Output is correct
22 Correct 38 ms 6724 KB Output is correct
23 Correct 46 ms 6728 KB Output is correct
24 Correct 47 ms 6744 KB Output is correct
25 Correct 38 ms 6724 KB Output is correct
26 Correct 35 ms 6428 KB Output is correct
27 Correct 52 ms 6904 KB Output is correct
28 Correct 39 ms 6808 KB Output is correct
29 Correct 41 ms 6976 KB Output is correct
30 Correct 93 ms 13636 KB Output is correct
31 Correct 127 ms 17552 KB Output is correct
32 Correct 216 ms 22576 KB Output is correct
33 Correct 278 ms 33304 KB Output is correct
34 Correct 73 ms 12472 KB Output is correct
35 Correct 282 ms 32184 KB Output is correct
36 Correct 277 ms 32384 KB Output is correct
37 Correct 286 ms 32432 KB Output is correct
38 Correct 260 ms 33448 KB Output is correct
39 Correct 265 ms 32944 KB Output is correct
40 Correct 255 ms 32224 KB Output is correct
41 Correct 259 ms 32376 KB Output is correct
42 Correct 256 ms 32836 KB Output is correct
43 Correct 190 ms 32428 KB Output is correct
44 Correct 199 ms 32208 KB Output is correct
45 Correct 187 ms 32428 KB Output is correct
46 Correct 196 ms 32436 KB Output is correct
47 Correct 194 ms 31408 KB Output is correct
48 Correct 218 ms 33452 KB Output is correct
49 Correct 201 ms 32684 KB Output is correct