답안 #885387

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
885387 2023-12-09T14:59:26 Z Koyote 운세 보기 2 (JOI14_fortune_telling2) C++11
100 / 100
327 ms 66092 KB
#include <bits/stdc++.h>
using namespace std;

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

// Segment Tree (iterative)
template<class S, S (*op)(S, S), S (*e)()> struct segtree {
    int _n, _size, _log;
    vector<S> d;
    void updateS(int k) { d[k] = op(d[k << 1], d[k << 1 | 1]); }
    segtree(const vector<S> &v) : _n((int)v.size()) {
        _log = 31 - __builtin_clz(_n) + ((_n & -_n) != _n), _size = 1 << _log;
        d = vector<S>(_size << 1, e());
        for (int i = 0; i < _n; ++i) d[_size + i] = v[i];
        for (int i = _size - 1; i > 0; --i) updateS(i);
    }
    S query(int l, int r) { // [l, r)
        S sml = e(), smr = e();
        for (l += _size, r += _size; l < r; l >>= 1, r >>= 1) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
        }
        return op(sml, smr);
    }
};

int max_op(int x, int y) { return max(x, y); }
int max_e() { return 0; }
// End of Segment Tree (iterative)

// Merge Sort Tree
template<class T> struct merge_sort_tree {
    int _l, _r, _m;
    vector<T> v;
    merge_sort_tree *lt, *rt;
    merge_sort_tree(int l, int r, const vector<T> &e) : _l(l), _r(r), _m((l + r) >> 1), v(r - l + 1) {
        if (l == r) v[0] = e[l], lt = rt = nullptr;
        else {
            lt = new merge_sort_tree(_l, _m, e);
            rt = new merge_sort_tree(_m + 1, _r, e);
            vector<T> v1 = lt->v, v2 = rt->v;
            merge(all(v1), all(v2), v.begin());
        }
    }
    int count(int qs, int qe, T x, T y) {
        if (x > y || _r < qs || qe < _l) return 0;
        if (qs <= _l && _r <= qe) return upper_bound(all(v), y) - lower_bound(all(v), x);
        // return lt->count(qs, qe, x, y) + rt->count(qs, qe, x, y);
        return (qs <= lt->_r ? lt->count(qs, qe, x, y) : 0) + (qe >= rt->_l ? rt->count(qs, qe, x, y) : 0);
    }
};
// End of Merge Sort Tree
 
const int N = 2e5 + 2;
int a[N], b[N], t[N], t_idx[N * 3];
 
int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    if (fopen(TASK ".inp", "r")) {
        freopen(TASK ".inp", "r", stdin);
        freopen(TASK ".out", "w", stdout);
    }
    int n, k; cin >> n >> k;
    for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
    for (int i = 1; i <= k; i++) cin >> t[i];

    // Compressing values
    basic_string<int> cmpr; cmpr.reserve(2 * n + k + 1), cmpr += 0;
    for (int i = 1; i <= n; i++) cmpr += a[i], cmpr += b[i];
    for (int i = 1; i <= k; i++) cmpr += t[i];
    sort(all(cmpr)), cmpr.erase(unique(all(cmpr)), cmpr.end());

    auto search = [&](const int v) -> int { return lower_bound(all(cmpr), v) - cmpr.begin(); };
    for (int i = 1; i <= n; i++) a[i] = search(a[i]), b[i] = search(b[i]);
    for (int i = 1; i <= k; i++) t[i] = search(t[i]), t_idx[t[i]] = i;

    // Data Structures
    segtree<int, max_op, max_e> stree(vector<int>(t_idx, t_idx + sz(cmpr) + 1));
    auto query_max_t_idx = [&](int l, int r) -> int { return stree.query(l, r + 1); };

    merge_sort_tree<int> mst(0, k + 1, vector<int>(t, t + k + 1));
    // Number of i that (l <= i <= r) and (x <= a[i] <= y)
    auto cnt_on_range = [&](int l, int r, int x, int y) -> int {
        return mst.count(l, r, x, y);
    };

    // Solving
    basic_string<int> ans; ans.reserve(n);
    for (int i = 1; i <= n; i++) {
        if (a[i] == b[i]) { ans += cmpr[a[i]]; continue; }

        // Assuming a[i] < b[i], find the last query that flips into b[i]
        bool swapped = (a[i] > b[i] ? (swap(a[i], b[i]), true) : false);
        int last_query = query_max_t_idx(a[i], b[i] - 1);

        // Count number of queries that guanrantee to flip
        int cnt_flipped = cnt_on_range(last_query + 1, k, b[i], sz(cmpr));
 
        if (last_query != 0 || swapped) swap(a[i], b[i]);
        ans += cmpr[~cnt_flipped & 1 ? a[i] : b[i]];
    }

    // for (int i : ans) cout << i << ' ';
    cout << accumulate(ans.begin(), ans.end(), 0LL) << '\n';
}

Compilation message

fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:63:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |         freopen(TASK ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:64:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |         freopen(TASK ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2648 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 2 ms 2816 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2648 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 2 ms 2816 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 11 ms 5212 KB Output is correct
15 Correct 24 ms 10124 KB Output is correct
16 Correct 36 ms 13388 KB Output is correct
17 Correct 50 ms 15740 KB Output is correct
18 Correct 49 ms 15740 KB Output is correct
19 Correct 48 ms 15740 KB Output is correct
20 Correct 52 ms 15812 KB Output is correct
21 Correct 47 ms 15796 KB Output is correct
22 Correct 37 ms 15896 KB Output is correct
23 Correct 38 ms 15348 KB Output is correct
24 Correct 40 ms 15264 KB Output is correct
25 Correct 37 ms 15860 KB Output is correct
26 Correct 53 ms 15676 KB Output is correct
27 Correct 75 ms 15884 KB Output is correct
28 Correct 52 ms 15892 KB Output is correct
29 Correct 107 ms 15936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2648 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 1 ms 2652 KB Output is correct
7 Correct 2 ms 2652 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 2 ms 2816 KB Output is correct
12 Correct 1 ms 2652 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 11 ms 5212 KB Output is correct
15 Correct 24 ms 10124 KB Output is correct
16 Correct 36 ms 13388 KB Output is correct
17 Correct 50 ms 15740 KB Output is correct
18 Correct 49 ms 15740 KB Output is correct
19 Correct 48 ms 15740 KB Output is correct
20 Correct 52 ms 15812 KB Output is correct
21 Correct 47 ms 15796 KB Output is correct
22 Correct 37 ms 15896 KB Output is correct
23 Correct 38 ms 15348 KB Output is correct
24 Correct 40 ms 15264 KB Output is correct
25 Correct 37 ms 15860 KB Output is correct
26 Correct 53 ms 15676 KB Output is correct
27 Correct 75 ms 15884 KB Output is correct
28 Correct 52 ms 15892 KB Output is correct
29 Correct 107 ms 15936 KB Output is correct
30 Correct 119 ms 57856 KB Output is correct
31 Correct 150 ms 60348 KB Output is correct
32 Correct 198 ms 60732 KB Output is correct
33 Correct 276 ms 65864 KB Output is correct
34 Correct 110 ms 57920 KB Output is correct
35 Correct 280 ms 65832 KB Output is correct
36 Correct 276 ms 66072 KB Output is correct
37 Correct 281 ms 65832 KB Output is correct
38 Correct 278 ms 65992 KB Output is correct
39 Correct 287 ms 65864 KB Output is correct
40 Correct 272 ms 66092 KB Output is correct
41 Correct 276 ms 65904 KB Output is correct
42 Correct 292 ms 65952 KB Output is correct
43 Correct 201 ms 65864 KB Output is correct
44 Correct 200 ms 65784 KB Output is correct
45 Correct 204 ms 66092 KB Output is correct
46 Correct 202 ms 61884 KB Output is correct
47 Correct 237 ms 59712 KB Output is correct
48 Correct 327 ms 61764 KB Output is correct
49 Correct 289 ms 61748 KB Output is correct