답안 #411253

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
411253 2021-05-24T20:34:14 Z AlperenT 운세 보기 2 (JOI14_fortune_telling2) C++17
35 / 100
466 ms 262148 KB
#include <bits/stdc++.h>

using namespace std;

const long long N = 2e5 + 5;

long long n, k, t[N], ans, tindx[N * 2], indx, cnt;

priority_queue<pair<long long, long long>> pq;

struct Card{
    long long a, b, aa, bb;
    bool swapped;

    bool operator < (Card &scnd) const{
        return a > scnd.a;
    }
};

Card cards[N];

struct SegTree{
    long long tree[(N * 2) * 4];

    void build(long long v, long long l, long long r){
        if(l == r) tree[v] = tindx[l];
        else{
            long long m = l + (r - l) / 2;
            build(v * 2, l, m);
            build(v * 2 + 1, m + 1, r);
            tree[v] = max(tree[v * 2], tree[v * 2 + 1]);
        } 
    }

    long long query(long long v, long long tl, long long tr, long long l, long long r){
        if(l > r) return 0;
        else if(tl == l && tr == r) return tree[v];
        else{
            long long tm = tl + (tr - tl) / 2;
            return max(query(v * 2, tl, tm, l, min(tm, r)), query(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
        }
    }
};

SegTree segtree;

struct Fenwick{
    long long tree[N * 2];

    long long getsum(long long r){
        long long sum = 0;
        for(long long i = r; i >= 1; i -= i & (-i)){
            sum += tree[i];
        }
        return sum;
    }

    long long query(long long l, long long r){
        return getsum(r) - getsum(l - 1);
    }

    void update(long long pos, long long val){
        for(long long i = pos; i < N * 2; i += i & (-i)){
            tree[i] += val;
        }
    }
};

Fenwick fw;

void compress(){
    vector<long long> nums;

    for(long long i = 1; i <= n; i++) nums.push_back(cards[i].a), nums.push_back(cards[i].b);
    for(long long i = 1; i <= k; i++) nums.push_back(t[i]);

    sort(nums.begin(), nums.end());

    nums.erase(unique(nums.begin(), nums.end()), nums.end());

    for(long long i = 1; i <= n; i++){
        cards[i].aa = cards[i].a;
        cards[i].a = lower_bound(nums.begin(), nums.end(), cards[i].a) - nums.begin() + 1;
        cards[i].bb = cards[i].b;
        cards[i].b = lower_bound(nums.begin(), nums.end(), cards[i].b) - nums.begin() + 1;
    }

    for(long long i = 1; i <= k; i++) t[i] = lower_bound(nums.begin(), nums.end(), t[i]) - nums.begin() + 1;
}

int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    
    cin >> n >> k;

    for(long long i = 1; i <= n; i++){
        cin >> cards[i].a >> cards[i].b;

        if(cards[i].b > cards[i].a) swap(cards[i].a, cards[i].b), cards[i].swapped = true;
    }

    for(long long i = 1; i <= k; i++) cin >> t[i];

    compress();

    sort(cards + 1, cards + n + 1);

    for(long long i = 1; i <= k; i++){
        pq.push({t[i], i});

        tindx[t[i]] = max(tindx[t[i]], i);
    }

    segtree.build(1, 1, N * 2);

    for(long long i = 1; i <= n; i++){
        while(!pq.empty() && pq.top().first >= cards[i].a){
            fw.update(pq.top().second, 1);
            pq.pop();
        }

        indx = segtree.query(1, 1, N * 2, cards[i].b, cards[i].a - 1);
        cnt = fw.query(indx + 1, N * 2);

        if(indx == 0){
            if(cards[i].swapped) ans += (cnt % 2 == 0 ? cards[i].bb : cards[i].aa);
            else ans += (cnt % 2 == 0 ? cards[i].aa : cards[i].bb);
        }
        else{
            ans += (cnt % 2 == 0 ? cards[i].aa : cards[i].bb);
        }
    }

    cout << ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8652 KB Output is correct
2 Correct 7 ms 8652 KB Output is correct
3 Correct 7 ms 8652 KB Output is correct
4 Correct 7 ms 8652 KB Output is correct
5 Correct 7 ms 8652 KB Output is correct
6 Correct 7 ms 8732 KB Output is correct
7 Correct 8 ms 8652 KB Output is correct
8 Correct 9 ms 8652 KB Output is correct
9 Correct 8 ms 8696 KB Output is correct
10 Correct 7 ms 8652 KB Output is correct
11 Correct 7 ms 8652 KB Output is correct
12 Correct 9 ms 8652 KB Output is correct
13 Correct 7 ms 8652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8652 KB Output is correct
2 Correct 7 ms 8652 KB Output is correct
3 Correct 7 ms 8652 KB Output is correct
4 Correct 7 ms 8652 KB Output is correct
5 Correct 7 ms 8652 KB Output is correct
6 Correct 7 ms 8732 KB Output is correct
7 Correct 8 ms 8652 KB Output is correct
8 Correct 9 ms 8652 KB Output is correct
9 Correct 8 ms 8696 KB Output is correct
10 Correct 7 ms 8652 KB Output is correct
11 Correct 7 ms 8652 KB Output is correct
12 Correct 9 ms 8652 KB Output is correct
13 Correct 7 ms 8652 KB Output is correct
14 Correct 24 ms 9796 KB Output is correct
15 Correct 44 ms 11588 KB Output is correct
16 Correct 59 ms 12788 KB Output is correct
17 Correct 79 ms 14592 KB Output is correct
18 Correct 83 ms 14568 KB Output is correct
19 Correct 83 ms 14524 KB Output is correct
20 Correct 82 ms 14456 KB Output is correct
21 Correct 72 ms 14496 KB Output is correct
22 Correct 56 ms 13668 KB Output is correct
23 Correct 59 ms 13556 KB Output is correct
24 Correct 59 ms 13456 KB Output is correct
25 Correct 56 ms 13828 KB Output is correct
26 Correct 67 ms 13688 KB Output is correct
27 Correct 67 ms 14240 KB Output is correct
28 Correct 68 ms 14176 KB Output is correct
29 Correct 82 ms 14264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 8652 KB Output is correct
2 Correct 7 ms 8652 KB Output is correct
3 Correct 7 ms 8652 KB Output is correct
4 Correct 7 ms 8652 KB Output is correct
5 Correct 7 ms 8652 KB Output is correct
6 Correct 7 ms 8732 KB Output is correct
7 Correct 8 ms 8652 KB Output is correct
8 Correct 9 ms 8652 KB Output is correct
9 Correct 8 ms 8696 KB Output is correct
10 Correct 7 ms 8652 KB Output is correct
11 Correct 7 ms 8652 KB Output is correct
12 Correct 9 ms 8652 KB Output is correct
13 Correct 7 ms 8652 KB Output is correct
14 Correct 24 ms 9796 KB Output is correct
15 Correct 44 ms 11588 KB Output is correct
16 Correct 59 ms 12788 KB Output is correct
17 Correct 79 ms 14592 KB Output is correct
18 Correct 83 ms 14568 KB Output is correct
19 Correct 83 ms 14524 KB Output is correct
20 Correct 82 ms 14456 KB Output is correct
21 Correct 72 ms 14496 KB Output is correct
22 Correct 56 ms 13668 KB Output is correct
23 Correct 59 ms 13556 KB Output is correct
24 Correct 59 ms 13456 KB Output is correct
25 Correct 56 ms 13828 KB Output is correct
26 Correct 67 ms 13688 KB Output is correct
27 Correct 67 ms 14240 KB Output is correct
28 Correct 68 ms 14176 KB Output is correct
29 Correct 82 ms 14264 KB Output is correct
30 Correct 174 ms 19228 KB Output is correct
31 Correct 205 ms 26080 KB Output is correct
32 Correct 305 ms 29776 KB Output is correct
33 Runtime error 466 ms 262148 KB Execution killed with signal 9
34 Halted 0 ms 0 KB -