Submission #1073402

#TimeUsernameProblemLanguageResultExecution timeMemory
1073402steveonalexFortune Telling 2 (JOI14_fortune_telling2)C++17
100 / 100
295 ms25124 KiB
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
 
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
 
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
 
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
 
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
    double cur = rngesus(0, MASK(60) - 1);
    cur /= MASK(60) - 1;
    return l + cur * (r - l);
}
 
template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }
 
template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }
 
template <class T>
    void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }
 
template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

const int N = 2e5 + 69;

struct FenwickTree{
    int n;
    vector<int> a;

    FenwickTree(int _n){
        n = _n;
        a.resize(n+1, 0);
    }

    void update(int i, int v){
        while(i <= n){
            a[i] += v;
            i += LASTBIT(i);
        }
    }

    int get(int i){
        int ans = 0;
        while(i > 0){
            ans += a[i];
            i -= LASTBIT(i);
        }
        return ans;
    }
};

struct SegmentTree{
    int n;
    vector<int> a;

    SegmentTree(int _n){
        n = _n;
        a.resize(n * 2 + 2, 0);
    }

    void update(int i, int v){
        i += n; if (!maximize(a[i], v)) return;
        while(i > 1){
            i >>= 1;
            a[i] = max(a[i * 2], a[i * 2 + 1]);
        }
    }

    int get(int l, int r){
        l += n; r += n + 1;
        int ans = 0;
        while(l < r){
            if (l & 1) maximize(ans, a[l++]);
            if (r & 1) maximize(ans, a[--r]);
            l >>= 1; r >>= 1;
        }
        return ans;
    }
};

int main(void){
    ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
    clock_t start = clock();

    int n, k; cin >> n >> k;
    vector<pair<int, int>> a(n);
    for(int i = 0; i<n; ++i) cin >> a[i].first >> a[i].second;

    vector<int> query(k+1);
    for(int i = 1; i<=k; ++i) cin >> query[i];

    vector<int> X = query;
    for(pair<int, int> i: a) {
        X.push_back(i.first);
        X.push_back(i.second);
    }

    remove_dup(X);

    int m = X.size() - 1;
    SegmentTree st(m);
    for(int i = 1; i<=k; ++i){
        int idx = lower_bound(ALL(X), query[i]) - X.begin();
        st.update(idx, i);
    }

    vector<int> val(n);
    vector<vector<int>> createQuery(k+1);
    for(int i= 0; i<n; ++i){
        int l = a[i].first, r = a[i].second;
        if (l > r) swap(l, r);
        l = lower_bound(ALL(X), l) - X.begin();
        r = lower_bound(ALL(X), r) - X.begin() - 1;

        val[i] = st.get(l, r);
        createQuery[val[i]].push_back(i);
    }

    FenwickTree bit(m);
    for(int i = k; i>=0; --i){
        for(int j: createQuery[i]){
            int l = lower_bound(ALL(X), max(a[j].first, a[j].second)) - X.begin();
            int cnt = bit.get(m) - bit.get(l-1);
            if (i > 0 && a[j].first < a[j].second){
                swap(a[j].first, a[j].second);
            }
            if (cnt % 2) swap(a[j].first, a[j].second);
        }
        if (i == 0) break;
        int idx = lower_bound(ALL(X), query[i]) - X.begin();
        bit.update(idx, 1);
    }

    ll sum = 0;
    for(pair<int, int> i: a) sum += i.first;

    cout << sum << "\n";


 
    cerr << "Time elapsed: " << clock() - start << " ms\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...