Submission #1091938

# Submission time Handle Problem Language Result Execution time Memory
1091938 2024-09-22T16:17:28 Z anhthi Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
242 ms 25836 KB
#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

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 time Memory Grader output
1 Correct 3 ms 5464 KB Output is correct
2 Correct 3 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 3 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 3 ms 5216 KB Output is correct
9 Correct 3 ms 5208 KB Output is correct
10 Correct 3 ms 5208 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 3 ms 5212 KB Output is correct
13 Correct 3 ms 5208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5464 KB Output is correct
2 Correct 3 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 3 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 3 ms 5216 KB Output is correct
9 Correct 3 ms 5208 KB Output is correct
10 Correct 3 ms 5208 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 3 ms 5212 KB Output is correct
13 Correct 3 ms 5208 KB Output is correct
14 Correct 10 ms 6236 KB Output is correct
15 Correct 20 ms 7136 KB Output is correct
16 Correct 31 ms 8144 KB Output is correct
17 Correct 40 ms 9168 KB Output is correct
18 Correct 42 ms 9168 KB Output is correct
19 Correct 39 ms 9172 KB Output is correct
20 Correct 39 ms 9172 KB Output is correct
21 Correct 39 ms 9176 KB Output is correct
22 Correct 29 ms 8156 KB Output is correct
23 Correct 28 ms 7900 KB Output is correct
24 Correct 27 ms 7724 KB Output is correct
25 Correct 29 ms 8628 KB Output is correct
26 Correct 30 ms 8372 KB Output is correct
27 Correct 33 ms 8668 KB Output is correct
28 Correct 37 ms 8660 KB Output is correct
29 Correct 35 ms 8916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5464 KB Output is correct
2 Correct 3 ms 5212 KB Output is correct
3 Correct 2 ms 5212 KB Output is correct
4 Correct 3 ms 5212 KB Output is correct
5 Correct 2 ms 5212 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 2 ms 5212 KB Output is correct
8 Correct 3 ms 5216 KB Output is correct
9 Correct 3 ms 5208 KB Output is correct
10 Correct 3 ms 5208 KB Output is correct
11 Correct 3 ms 5212 KB Output is correct
12 Correct 3 ms 5212 KB Output is correct
13 Correct 3 ms 5208 KB Output is correct
14 Correct 10 ms 6236 KB Output is correct
15 Correct 20 ms 7136 KB Output is correct
16 Correct 31 ms 8144 KB Output is correct
17 Correct 40 ms 9168 KB Output is correct
18 Correct 42 ms 9168 KB Output is correct
19 Correct 39 ms 9172 KB Output is correct
20 Correct 39 ms 9172 KB Output is correct
21 Correct 39 ms 9176 KB Output is correct
22 Correct 29 ms 8156 KB Output is correct
23 Correct 28 ms 7900 KB Output is correct
24 Correct 27 ms 7724 KB Output is correct
25 Correct 29 ms 8628 KB Output is correct
26 Correct 30 ms 8372 KB Output is correct
27 Correct 33 ms 8668 KB Output is correct
28 Correct 37 ms 8660 KB Output is correct
29 Correct 35 ms 8916 KB Output is correct
30 Correct 74 ms 11696 KB Output is correct
31 Correct 114 ms 14532 KB Output is correct
32 Correct 141 ms 18024 KB Output is correct
33 Correct 234 ms 25532 KB Output is correct
34 Correct 66 ms 10960 KB Output is correct
35 Correct 242 ms 25532 KB Output is correct
36 Correct 227 ms 25280 KB Output is correct
37 Correct 238 ms 25280 KB Output is correct
38 Correct 225 ms 25280 KB Output is correct
39 Correct 230 ms 25284 KB Output is correct
40 Correct 227 ms 25836 KB Output is correct
41 Correct 220 ms 25280 KB Output is correct
42 Correct 221 ms 25160 KB Output is correct
43 Correct 158 ms 25376 KB Output is correct
44 Correct 151 ms 25280 KB Output is correct
45 Correct 159 ms 25124 KB Output is correct
46 Correct 147 ms 19904 KB Output is correct
47 Correct 144 ms 18368 KB Output is correct
48 Correct 174 ms 23144 KB Output is correct
49 Correct 168 ms 23232 KB Output is correct