Submission #824701

# Submission time Handle Problem Language Result Execution time Memory
824701 2023-08-14T08:58:59 Z Koyote Fortune Telling 2 (JOI14_fortune_telling2) C++14
35 / 100
183 ms 109316 KB
#include <bits/stdc++.h>
using namespace std;

#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()

#define int int64_t

template<class T> struct merge_sort_tree {
    int _l, _r, _m;
    vector<T> v;
    merge_sort_tree *left, *right;
    merge_sort_tree(int l, int r, vector<T> &e) {
        v.resize(r - l + 1);
        _l = l, _r = r, _m = (l + r) >> 1, v[0] = e[l];
        if (l == r) left = right = nullptr;
        else {
            left = new merge_sort_tree(_l, _m, e);
            right = new merge_sort_tree(_m + 1, _r, e);
            vector<T> v1 = left->v, v2 = right->v;
            v.clear();
            int s1 = sz(v1), s2 = sz(v2);
            v.reserve(s1 + s2);
            int i = 0, j = 0;
            while (i < s1 && j < s2) {
                if (v1[i] <= v2[j]) v.push_back(v1[i]), i++;
                else v.push_back(v2[j]), j++;
            }
            while (i < s1) v.push_back(v1[i]), i++;
            while (j < s2) v.push_back(v2[j]), j++;
        }
    }
    int count(int l, int r, T a, T b) {
        if (a > b) return 0;
        if (l > _r || r < _l) return 0;
        if (_l >= l && _r <= r) return upper_bound(all(v), b) - lower_bound(all(v), a);
        return left->count(l, r, a, b) + right->count(l, r, a, b);
    }
};

const int N = 2e5 + 2, LG = 21;
int n, k, a[N], b[N], t[N], ti[N];
int spt_max_ti[N][LG];

int lg2(int x) { return x ? 31 - __builtin_clz(x) : -1; }

int32_t main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
    for (int i = 0; i < k; i++) cin >> t[i];

    vector<int> compr; compr.reserve(2 * n + k);
    for (int i = 0; i < n; i++) compr.push_back(a[i]), compr.push_back(b[i]);
    for (int i = 0; i < k; i++) compr.push_back(t[i]);
    sort(compr.begin(), compr.end());
    compr.erase(unique(compr.begin(), compr.end()), compr.end());
    for (int i = 0; i < n; i++) {
        a[i] = lower_bound(compr.begin(), compr.end(), a[i]) - compr.begin();
        b[i] = lower_bound(compr.begin(), compr.end(), b[i]) - compr.begin();
    }
    for (int i = 0; i < k; i++)
        t[i] = lower_bound(compr.begin(), compr.end(), t[i]) - compr.begin();

    int max_ti = *max_element(t, t + k);
    for (int i = 0; i <= max_ti; i++) ti[i] = -1;
    for (int i = 0; i < k; i++) ti[t[i]] = i;
    for (int i = 0; i <= max_ti; i++)
        spt_max_ti[i][0] = ti[i];
    for (int j = 1; j < LG; j++)
        for (int i = 0; i + (1 << j) - 1 <= max_ti; i++)
            spt_max_ti[i][j] = max(spt_max_ti[i][j - 1], spt_max_ti[i + (1 << (j - 1))][j - 1]);
    
    auto query_max_ti = [&](int l, int r) {
        int len = lg2(r - l + 1);
        return max(spt_max_ti[l][len], spt_max_ti[r - (1 << len) + 1][len]);
    };

    vector<int> t_vector(k);
    for (int i = 0; i < k; i++) t_vector[i] = t[i];
    auto cnt_in_range_ab = merge_sort_tree<int>(0, k - 1, t_vector);

    long long ans = 0;
    for (int i = 0; i < n; i++) {
        if (a[i] == b[i]) { ans += compr[a[i]]; continue; }
        bool swapped = (a[i] > b[i]);
        if (a[i] > b[i]) swap(a[i], b[i]);
        auto last_pos = query_max_ti(a[i], b[i] - 1);
        int cnt_flipped = cnt_in_range_ab.count(last_pos + 1, k - 1, b[i], int(1e9));

        if (last_pos != -1 || swapped) swap(a[i], b[i]);
        if (cnt_flipped % 2 == 0) ans += compr[a[i]];
        else ans += compr[b[i]];
    }
    
    cout << ans << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 852 KB Output is correct
2 Correct 1 ms 980 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 2 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 2 ms 1108 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 1 ms 980 KB Output is correct
10 Correct 1 ms 852 KB Output is correct
11 Correct 1 ms 1052 KB Output is correct
12 Correct 1 ms 980 KB Output is correct
13 Correct 1 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 852 KB Output is correct
2 Correct 1 ms 980 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 2 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 2 ms 1108 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 1 ms 980 KB Output is correct
10 Correct 1 ms 852 KB Output is correct
11 Correct 1 ms 1052 KB Output is correct
12 Correct 1 ms 980 KB Output is correct
13 Correct 1 ms 980 KB Output is correct
14 Correct 15 ms 9148 KB Output is correct
15 Correct 30 ms 18192 KB Output is correct
16 Correct 48 ms 27252 KB Output is correct
17 Correct 78 ms 36464 KB Output is correct
18 Correct 71 ms 36364 KB Output is correct
19 Correct 69 ms 36344 KB Output is correct
20 Correct 73 ms 36400 KB Output is correct
21 Correct 66 ms 36348 KB Output is correct
22 Correct 47 ms 29560 KB Output is correct
23 Correct 53 ms 26036 KB Output is correct
24 Correct 49 ms 23544 KB Output is correct
25 Correct 48 ms 31444 KB Output is correct
26 Correct 57 ms 28168 KB Output is correct
27 Correct 65 ms 29488 KB Output is correct
28 Correct 55 ms 29588 KB Output is correct
29 Correct 79 ms 32980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 852 KB Output is correct
2 Correct 1 ms 980 KB Output is correct
3 Correct 1 ms 1108 KB Output is correct
4 Correct 2 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 1 ms 1108 KB Output is correct
7 Correct 2 ms 1108 KB Output is correct
8 Correct 1 ms 1108 KB Output is correct
9 Correct 1 ms 980 KB Output is correct
10 Correct 1 ms 852 KB Output is correct
11 Correct 1 ms 1052 KB Output is correct
12 Correct 1 ms 980 KB Output is correct
13 Correct 1 ms 980 KB Output is correct
14 Correct 15 ms 9148 KB Output is correct
15 Correct 30 ms 18192 KB Output is correct
16 Correct 48 ms 27252 KB Output is correct
17 Correct 78 ms 36464 KB Output is correct
18 Correct 71 ms 36364 KB Output is correct
19 Correct 69 ms 36344 KB Output is correct
20 Correct 73 ms 36400 KB Output is correct
21 Correct 66 ms 36348 KB Output is correct
22 Correct 47 ms 29560 KB Output is correct
23 Correct 53 ms 26036 KB Output is correct
24 Correct 49 ms 23544 KB Output is correct
25 Correct 48 ms 31444 KB Output is correct
26 Correct 57 ms 28168 KB Output is correct
27 Correct 65 ms 29488 KB Output is correct
28 Correct 55 ms 29588 KB Output is correct
29 Correct 79 ms 32980 KB Output is correct
30 Incorrect 183 ms 109316 KB Output isn't correct
31 Halted 0 ms 0 KB -