Submission #1112212

# Submission time Handle Problem Language Result Execution time Memory
1112212 2024-11-13T19:51:08 Z ortsac Fortune Telling 2 (JOI14_fortune_telling2) C++17
100 / 100
325 ms 27476 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define pii pair<long long, long long>
#define fr first
#define se second

const int MAXN = 2e5 + 10;
const int INF = 0x3f3f3f3f3f3f3f3f;

struct Node {
    int mi = INF, mx = -INF;
    int qtdmx = 0;
};

Node merge(const Node& a, const Node& b) {
    Node ans;
    ans.mi = min(a.mi, b.mi);
    ans.mx = max(a.mx, b.mx);
    if (ans.mx == a.mx) ans.qtdmx += a.qtdmx;
    if (ans.mx == b.mx) ans.qtdmx += b.qtdmx;
    return ans;
}

Node seg[4*MAXN];
int v[MAXN];

void build(int node, int l, int r) {
    if (l == r) {
        seg[node].mi = seg[node].mx = v[l];
        seg[node].qtdmx = 1;
        return;
    }
    int m = (l+r)/2;
    build(2*node, l, m);
    build(2*node + 1, m + 1, r);
    seg[node] = merge(seg[2*node], seg[2*node + 1]);
}

// find last value that is smaller than x
int bsearch(int node, int l, int r, int x) {
    //////cout << "hey search here " << l << " " << r << "\n";
    if (l == r) {
        if (seg[node].mi < x)
            return l;
        else
            return 0;
    }
    int m = (l+r)/2;
    if (seg[2*node + 1].mi < x) return bsearch(2*node + 1, m + 1, r, x);
    else if (seg[2*node].mi < x) return bsearch(2*node, l, m, x);
    else return 0;
}

void update(int node, int l, int r, int po) {
    if (l == r) {
        seg[node].mi = seg[node].mx = INF;
        return;
    }
    int m = (l+r)/2;
    if (po <= m) update(2*node, l, m, po);
    else update(2*node + 1, m + 1, r, po);
    seg[node] = merge(seg[2*node], seg[2*node + 1]);
}

Node empt;

Node query(int node, int l, int r, int tl, int tr) {
    ////cout << "query " << l << " " << r << " " << node << "\n";
    if ((tr < l) || (r < tl)) return empt;
    if ((tl <= l) && (r <= tr)) return seg[node];
    int m = (l+r)/2;
    return merge(query(2*node, l, m, tl, tr), query(2*node + 1, m + 1, r, tl, tr));
}

bool comp(pii a, pii b) {
    return (min(a.fr, a.se) < min(b.fr, b.se));
}

int32_t main() {
    int n, k;
    cin >> n >> k;
    vector<pii> c(n);
    for (int i = 0; i < n; i++) {
        cin >> c[i].fr >> c[i].se;
    }
    vector<pii> po;
    for (int i = 1; i <= k; i++) {
        cin >> v[i];
        po.push_back({v[i], i});
    }
    sort(po.begin(), po.end(), greater<pii>());
    sort(c.begin(), c.end(), comp);
    build(1, 1, k);
    int ans = 0;
    for (auto& u : c) {
        if (u.fr == u.se) {
            ans += u.fr;
            //cout << u.fr << " " << u.se << " " << u.fr << "\n";
            continue;
        }
        while ((!po.empty()) && (po.back().fr < min(u.fr, u.se))) {
            //cout << "rem " << po.back().fr << " " << po.back().se << "\n";
            update(1, 1, k, po.back().se);
            ////cout << seg[1].mx << "\n";
            po.pop_back();
        }
        if (po.empty()) {
            ans += u.fr;
            //cout << u.fr << " " << u.se << " " << u.fr << "\n";
            continue;
        }
        if (u.fr < u.se) {
            int p = bsearch(1, 1, k, u.se);
            if (p == k) {
                ans += u.se;
                //cout << u.fr << " " << u.se << " " << u.se << "\n";
                continue;
            }
            int comeco = 0;
            int qtd = (k - p);
            Node q = query(1, 1, k, p + 1, k);
            ////cout << q.mi << " " << q.mx << " " << q.qtdmx << "\n";
            if (q.mx == INF) qtd -= q.qtdmx;
            if (p == 0) {
                comeco = 1;
            }
            qtd += comeco;
            ////cout << qtd << " " << comeco << " " << p << "\n";
            if ((qtd % 2LL) == 1LL) {
                ans += u.fr;
                //cout << u.fr << " " << u.se << " " << u.fr << "\n";
            }
            else {
                ans += u.se;
                //cout << u.fr << " " << u.se << " " << u.se << "\n";
            }
            continue;
        } else {
            swap(u.fr, u.se);
            int p = bsearch(1, 1, k, u.se);
            if (p == k) {
                ans += u.se;
                //cout << u.se << " " << u.fr << " " << u.se << "\n";
                continue;
            }
            int comeco = 0;
            int qtd = (k - p);
            Node q = query(1, 1, k, p + 1, k);
            if (q.mx == INF) qtd -= q.qtdmx;
            //if (p == 0) {
            //    comeco = 1;
            //}
            //cout << qtd << " " << p << "\n";
            qtd += comeco;
            if ((qtd % 2LL) == 1LL) {
                ans += u.fr;
                //cout << u.se << " " << u.fr << " " << u.fr << "\n";
            }
            else {
                ans += u.se;
                //cout << u.se << " " << u.fr << " " << u.se << "\n";
            }
            continue;
        }
    }
    cout << ans << "\n";
}
// 3 2 2
// 4 5 5
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 2 ms 336 KB Output is correct
4 Correct 2 ms 336 KB Output is correct
5 Correct 2 ms 336 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 336 KB Output is correct
8 Correct 2 ms 336 KB Output is correct
9 Correct 2 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 2 ms 336 KB Output is correct
12 Correct 2 ms 504 KB Output is correct
13 Correct 2 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 2 ms 336 KB Output is correct
4 Correct 2 ms 336 KB Output is correct
5 Correct 2 ms 336 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 336 KB Output is correct
8 Correct 2 ms 336 KB Output is correct
9 Correct 2 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 2 ms 336 KB Output is correct
12 Correct 2 ms 504 KB Output is correct
13 Correct 2 ms 336 KB Output is correct
14 Correct 13 ms 1740 KB Output is correct
15 Correct 29 ms 2760 KB Output is correct
16 Correct 38 ms 3304 KB Output is correct
17 Correct 62 ms 5052 KB Output is correct
18 Correct 53 ms 6228 KB Output is correct
19 Correct 55 ms 6332 KB Output is correct
20 Correct 55 ms 6336 KB Output is correct
21 Correct 43 ms 6332 KB Output is correct
22 Correct 35 ms 5828 KB Output is correct
23 Correct 38 ms 5820 KB Output is correct
24 Correct 44 ms 5820 KB Output is correct
25 Correct 33 ms 5820 KB Output is correct
26 Correct 46 ms 6084 KB Output is correct
27 Correct 55 ms 6332 KB Output is correct
28 Correct 52 ms 6168 KB Output is correct
29 Correct 52 ms 6332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 2 ms 336 KB Output is correct
4 Correct 2 ms 336 KB Output is correct
5 Correct 2 ms 336 KB Output is correct
6 Correct 2 ms 504 KB Output is correct
7 Correct 2 ms 336 KB Output is correct
8 Correct 2 ms 336 KB Output is correct
9 Correct 2 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 2 ms 336 KB Output is correct
12 Correct 2 ms 504 KB Output is correct
13 Correct 2 ms 336 KB Output is correct
14 Correct 13 ms 1740 KB Output is correct
15 Correct 29 ms 2760 KB Output is correct
16 Correct 38 ms 3304 KB Output is correct
17 Correct 62 ms 5052 KB Output is correct
18 Correct 53 ms 6228 KB Output is correct
19 Correct 55 ms 6332 KB Output is correct
20 Correct 55 ms 6336 KB Output is correct
21 Correct 43 ms 6332 KB Output is correct
22 Correct 35 ms 5828 KB Output is correct
23 Correct 38 ms 5820 KB Output is correct
24 Correct 44 ms 5820 KB Output is correct
25 Correct 33 ms 5820 KB Output is correct
26 Correct 46 ms 6084 KB Output is correct
27 Correct 55 ms 6332 KB Output is correct
28 Correct 52 ms 6168 KB Output is correct
29 Correct 52 ms 6332 KB Output is correct
30 Correct 139 ms 20820 KB Output is correct
31 Correct 161 ms 22196 KB Output is correct
32 Correct 222 ms 23740 KB Output is correct
33 Correct 289 ms 27312 KB Output is correct
34 Correct 83 ms 20472 KB Output is correct
35 Correct 294 ms 27312 KB Output is correct
36 Correct 299 ms 27316 KB Output is correct
37 Correct 325 ms 27460 KB Output is correct
38 Correct 266 ms 27312 KB Output is correct
39 Correct 285 ms 27348 KB Output is correct
40 Correct 203 ms 27056 KB Output is correct
41 Correct 272 ms 27352 KB Output is correct
42 Correct 294 ms 27344 KB Output is correct
43 Correct 169 ms 26716 KB Output is correct
44 Correct 168 ms 26800 KB Output is correct
45 Correct 170 ms 26544 KB Output is correct
46 Correct 202 ms 25520 KB Output is correct
47 Correct 239 ms 25264 KB Output is correct
48 Correct 289 ms 27312 KB Output is correct
49 Correct 254 ms 27476 KB Output is correct