답안 #411247

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

using namespace std;

const int N = 2e5 + 5;

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

priority_queue<pair<int, int>> pq;

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

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

Card cards[N];

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

    void build(int v, int l, int r){
        if(l == r) tree[v] = tindx[l];
        else{
            int 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]);
        } 
    }

    int query(int v, int tl, int tr, int l, int r){
        if(l > r) return 0;
        else if(tl == l && tr == r) return tree[v];
        else{
            int 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{
    int tree[N * 2];

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

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

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

Fenwick fw;

void compress(){
    vector<int> nums;

    for(int i = 1; i <= n; i++) nums.push_back(cards[i].a), nums.push_back(cards[i].b);
    for(int 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(int 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(int 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(int 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(int i = 1; i <= k; i++) cin >> t[i];

    compress();

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

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

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

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

    for(int 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 - 1, cards[i].b, cards[i].a - 1);
        cnt = fw.query(indx + 1, N * 2 - 1);

        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 Incorrect 5 ms 4428 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 4428 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 5 ms 4428 KB Output isn't correct
2 Halted 0 ms 0 KB -