제출 #1337693

#제출 시각아이디문제언어결과실행 시간메모리
1337693niehFortune Telling 2 (JOI14_fortune_telling2)C++20
100 / 100
235 ms9908 KiB
#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define FOD(i, a, b) for(int i = (a); i >= (b); i--)
#define ALL(x) (x).begin(), (x).end()
#define file(name) if(fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define fi first
#define se second
#define el cout << '\n'
#define maxn int(2e5 + 5)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

struct SegmentTree {
    int n;

    vector<int> t;

    SegmentTree(int __n = 0) {
        n = __n;
        t.assign(n << 2 | 3, 0);
    }

    void update(int pos, int val, int id, int l, int r) {
        if(pos < l || r < pos) return;
        if(l == r) {
            t[id] = val;
            return;
        }
        int mid = (l + r) >> 1;
        update(pos, val, id << 1, l, mid);
        update(pos, val, id << 1 | 1, mid + 1, r);
        t[id] = max(t[id << 1], t[id << 1 | 1]);
    }

    int get(int u, int v, int id, int l, int r) {
        if(v < l || r < u || v < u) return 0;
        if(u <= l && r <= v) return t[id];
        int mid = (l + r) >> 1;
        return max(get(u, v, id << 1, l, mid), get(u, v, id << 1 | 1, mid + 1, r));
    }

    void update(int pos, int val) {
        update(pos, val, 1, 1, n);
    }

    int get(int u, int v) {
        return get(u, v, 1, 1, n);
    }
};

struct FenwickTree {
    int n;

    vector<int> t;

    FenwickTree(int __n = 0) {
        n = __n;
        t.assign(n + 3, 0);
    }

    void update(int x, int v) {
        for(; x <= n; x += x & -x) t[x] += v;
    }

    int get(int x) {
        int res = 0;
        for(; x; x -= x & -x) res += t[x];
        return res;
    }
};

int n, m, a[maxn], b[maxn], t[maxn];

int cnt[maxn];

ll res;

vector<pii> query;

vector<int> vals;

void compress() {
    FOR(i, 1, m) vals.push_back(t[i]);
    sort(ALL(vals));
    vals.resize(unique(ALL(vals)) - vals.begin());
    FOR(i, 1, m) t[i] = lower_bound(ALL(vals), t[i]) - vals.begin() + 1;
}

bool cmp(pii &A, pii &B) {
    return A.fi > B.fi;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0 ); cout.tie(0);
    cin >> n >> m;
    FOR(i, 1, n)
        cin >> a[i] >> b[i];
    FOR(i, 1, m) cin >> t[i];
    compress();
    SegmentTree st(m);
    FenwickTree ft(m);
    FOR(i, 1, m) st.update(t[i], i);
    FOR(i, 1, n) {
        int l = lower_bound(ALL(vals), min(a[i], b[i])) - vals.begin() + 1;
        int r = lower_bound(ALL(vals), max(a[i], b[i])) - vals.begin();
        int p = st.get(l, r);
        if(p + 1 <= m) query.push_back({ p + 1, i });
        if(p > 0 && a[i] <= b[i]) swap(a[i], b[i]);
    }
    sort(ALL(query), cmp);
    int j = m + 1;
    for(pii x : query) {
        while(j - 1 >= x.fi) ft.update(t[--j], 1);
        int p = lower_bound(ALL(vals), max(a[x.se], b[x.se])) - vals.begin() + 1;
        cnt[x.se] = ft.get(m) - ft.get(p - 1);
    }
    FOR(i, 1, n) if(cnt[i] & 1) swap(a[i], b[i]);
    FOR(i, 1, n) res += a[i];
    cout << res;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...