답안 #317607

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
317607 2020-10-30T03:56:15 Z two_sides 운세 보기 2 (JOI14_fortune_telling2) C++17
100 / 100
399 ms 8136 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second

const int N = 200005;

int bit[N], oper[N], n, q, st[N * 4];
pair <int, int> card[N], event[N];
vector <int> vals;

void update_st(int i, int l, int r, int p, int v) {
    if (l > p || r < p) return;
    st[i] = max(st[i], v);
    if (l == r) return;
    int m = (l + r) / 2;
    update_st(i * 2, l, m, p, v);
    update_st(i * 2 + 1, m + 1, r, p, v);
}

int query_st(int i, int l, int r, int lo, int hi) {
    if (l > hi || r < lo) return 0;
    if (l >= lo && r <= hi) return st[i];
    int m = (l + r) / 2;
    return max(query_st(i * 2, l, m, lo, hi),
    query_st(i * 2 + 1, m + 1, r, lo, hi));
}

int get_id(int x) {
    return lower_bound(vals.begin(),
    vals.end(), x) - vals.begin() + 1;
}

void update_bit(int i) {
    for (; i > 0; i -= i & -i) bit[i]++;
}

int query_bit(int i) {
    int res = 0;
    for (; i < N; i += i & -i) res += bit[i];
    return res;
}

int main() {
/*#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
#endif*/
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> q; long long res = 0;
    for (int i = 1; i <= n; i++)
        cin >> card[i].fi >> card[i].se;
    for (int i = 1; i <= q; i++) {
        cin >> oper[i];
        vals.push_back(oper[i]);
    }
    sort(vals.begin(), vals.end());
    for (int i = 1; i <= q; i++) {
        oper[i] = get_id(oper[i]);
        update_st(1, 1, q, oper[i], i);
    }
    for (int i = 1, l, r; i <= n; i++) {
        tie(l, r) = minmax(card[i].fi, card[i].se);
        l = get_id(l); r = get_id(r) - 1;
        event[i] = {query_st(1, 1, q, l, r), i};
    }
    sort(event + 1, event + n + 1);
    for (int i = 1, j = 1; i <= n; i++) {
        while (j <= event[i].fi) update_bit(oper[j++]);
        int last = query_bit(get_id(max(
        card[event[i].se].se, card[event[i].se].se)));
        int flip = q - get_id((max(card[event[i].se].se,
        card[event[i].se].se))) + 1 - last;
        if (event[i].fi)
            flip ^= card[event[i].se].fi < card[event[i].se].se;
        if (flip & 1) res += card[event[i].se].se;
        else res += card[event[i].se].fi;
    }
    cout << res << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 14 ms 896 KB Output is correct
15 Correct 31 ms 1280 KB Output is correct
16 Correct 45 ms 1528 KB Output is correct
17 Correct 63 ms 2060 KB Output is correct
18 Correct 62 ms 2040 KB Output is correct
19 Correct 63 ms 2040 KB Output is correct
20 Correct 63 ms 2040 KB Output is correct
21 Correct 59 ms 2040 KB Output is correct
22 Correct 46 ms 2040 KB Output is correct
23 Correct 46 ms 2040 KB Output is correct
24 Correct 46 ms 2040 KB Output is correct
25 Correct 44 ms 2044 KB Output is correct
26 Correct 49 ms 2040 KB Output is correct
27 Correct 55 ms 2164 KB Output is correct
28 Correct 51 ms 2040 KB Output is correct
29 Correct 56 ms 2044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 14 ms 896 KB Output is correct
15 Correct 31 ms 1280 KB Output is correct
16 Correct 45 ms 1528 KB Output is correct
17 Correct 63 ms 2060 KB Output is correct
18 Correct 62 ms 2040 KB Output is correct
19 Correct 63 ms 2040 KB Output is correct
20 Correct 63 ms 2040 KB Output is correct
21 Correct 59 ms 2040 KB Output is correct
22 Correct 46 ms 2040 KB Output is correct
23 Correct 46 ms 2040 KB Output is correct
24 Correct 46 ms 2040 KB Output is correct
25 Correct 44 ms 2044 KB Output is correct
26 Correct 49 ms 2040 KB Output is correct
27 Correct 55 ms 2164 KB Output is correct
28 Correct 51 ms 2040 KB Output is correct
29 Correct 56 ms 2044 KB Output is correct
30 Correct 138 ms 5104 KB Output is correct
31 Correct 187 ms 5616 KB Output is correct
32 Correct 251 ms 6476 KB Output is correct
33 Correct 385 ms 8048 KB Output is correct
34 Correct 115 ms 4844 KB Output is correct
35 Correct 384 ms 8136 KB Output is correct
36 Correct 399 ms 8024 KB Output is correct
37 Correct 384 ms 7940 KB Output is correct
38 Correct 379 ms 8088 KB Output is correct
39 Correct 379 ms 8048 KB Output is correct
40 Correct 332 ms 8044 KB Output is correct
41 Correct 383 ms 8044 KB Output is correct
42 Correct 378 ms 8044 KB Output is correct
43 Correct 238 ms 8052 KB Output is correct
44 Correct 237 ms 8044 KB Output is correct
45 Correct 237 ms 8128 KB Output is correct
46 Correct 272 ms 8044 KB Output is correct
47 Correct 251 ms 8048 KB Output is correct
48 Correct 306 ms 7792 KB Output is correct
49 Correct 288 ms 7572 KB Output is correct