Submission #1091938

#TimeUsernameProblemLanguageResultExecution timeMemory
1091938anhthiFortune Telling 2 (JOI14_fortune_telling2)C++14
100 / 100
242 ms25836 KiB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define ll long long
#define mp(x, y) make_pair(x, y)
#define sz(v) ((int) (v).size())
#define all(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define pb push_back
#define max_rng *max_element
#define min_rng *min_element
#define rep(i, n) for(int i = 1, _n = (n); i <= _n; ++i)
#define forn(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define ford(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)

template <class X, class Y>
    inline bool maximize(X &x, Y y) {
        return (x < y ? x = y, true : false);
    }

template <class X, class Y>
    inline bool minimize(X &x, Y y) {
        return (x > y ? x = y, true : false);
    }

template <class X>
    inline void compress(vector<X> &a) {
        sort(all(a));
        a.resize(unique(all(a)) - a.begin());
    }

int ctz(ll x) { return __builtin_ctzll(x); }
int lg(ll x) { return 63 - __builtin_clzll(x); }
int popcount(ll x) { return __builtin_popcount(x); }

mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) {
    return l + abs((ll) rng()) % (r - l + 1);
}

const ll oo = (ll) 1e17;
const int inf = (ll) 2e9 + 7, mod = (ll) 1e9 + 7;

const int mxn = 2e5 + 5;

void add(int &x, int y) { x += y; if (x >= mod) x -= mod; }
void sub(int &x, int y) { x -= y; if (x < 0) x += mod; }

int n, k;
int p[mxn];
pair<int, int> a[mxn];

int lst[mxn], side[mxn];

struct Seg {
    int n;
    vector<int> st;

    Seg(int n) : n(n) {
        st.resize(2 * n + 5, 0);
    }
    void upd(int t, int val) {
        t += n;
        maximize(st[t], val);
        while (t > 1) {
            t >>= 1;
            st[t] = max(st[2 * t], st[2 * t + 1]);
        }
    }
    int get(int l, int r) {
        if (l > r) return 0;
        int ans = 0;
        for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
            if (l & 1) maximize(ans, st[l++]);
            if (r & 1) maximize(ans, st[--r]);
        }
        return ans;
    }
};

struct fen {
    int n;
    vector<int> bit;

    fen(int n) : n(n) {
        bit.resize(n + 1);
    }
    void upd(int p, int d) {
        for (; p <= n; p += p & -p)
            bit[p] += d;
        return;
    }
    int get(int p) {
        int ans = 0;
        for(; p > 0; p -= p & -p) {
            ans += bit[p];
        }
        return ans;
    }

    int rng(int l, int r) {
        if (l > r) return 0;
        return get(r) - get(l - 1);
    }
};

vector<int> events[mxn];

vector<int> cps;
void zipping() {
    rep(i, n) {
        cps.pb(a[i].fi);
        cps.pb(a[i].se);
    }
    rep(i, k) cps.pb(p[i]);

    compress(cps);
    rep(i, n) {
        a[i].fi = lower_bound(all(cps), a[i].fi) - cps.begin() + 1;
        a[i].se = lower_bound(all(cps), a[i].se) - cps.begin() + 1;
    }
    rep(i, k) p[i] = lower_bound(all(cps), p[i]) - cps.begin() + 1;
}

int main(void) {

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #define TASK "cquery"
    if (fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }
    cin >> n >> k;
    rep(i, n) cin >> a[i].fi >> a[i].se;
    rep(i, k) cin >> p[i];

    zipping();
    int m = sz(cps);


    rep(i, n) {
        if (a[i].fi > a[i].se) {
            side[i] = 1;
            swap(a[i].fi, a[i].se);
        }
    }

    Seg s(m);
    rep(i, k) s.upd(p[i], i);
    rep(i, n) {
        lst[i] = s.get(a[i].fi, a[i].se - 1);
        if (lst[i]) side[i] = 1;
        events[lst[i]].pb(i);
    }

    fen f(m);
    ford(i, k, 0) {
        for (int j : events[i]) {
            int cnt = f.rng(a[j].se, m);
            if (cnt & 1) side[j] ^= 1;
        }
        if (i) f.upd(p[i], 1);
    }
    ll sum = 0;
    rep(i, n) if (side[i])
        sum += cps[a[i].se - 1];
    else sum += cps[a[i].fi - 1];

    cout << sum;

    return 0;
}

Compilation message (stderr)

fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:136:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  136 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:137:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  137 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...