Submission #1071571

# Submission time Handle Problem Language Result Execution time Memory
1071571 2024-08-23T08:54:05 Z TrinhKhanhDung Fortune Telling 2 (JOI14_fortune_telling2) C++14
100 / 100
136 ms 13396 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 21071997)

using namespace std;

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 T1, class T2>
   void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}

template<class T1, class T2>
   void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}

template<class T>
    void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}

struct seg{
    vector<int> val;
    int n;

    seg(int _n = 0){
        n = _n;
        val.resize(n * 4 + 3, 0);   
    }

    void upd(int p, int c){
        int i = 1, l = 1, r = n;
        while(l < r){
            int m = (l + r) >> 1;
            if(p <= m){
                i = i * 2;
                r = m;
            }
            else{
                i = i * 2 + 1;
                l = m + 1;
            }
        }
        val[i] = c;
        while(i > 1){
            i >>= 1;
            val[i] = max(val[i * 2], val[i * 2 + 1]);
        }
    }

    int get(int c){
        if(val[1] < c) return -1;
        int i = 1, l = 1, r = n;
        while(l < r){
            int m = (l + r) >> 1;
            if(val[i * 2 + 1] >= c){
                i = i * 2 + 1;
                l = m + 1;
            }
            else{
                i = i * 2;
                r = m;
            }
        }
        return l;
    }
};

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

    fen(int _n = 0){
        n = _n;
        tree.resize(n + 3, 0);
    }

    void upd(int p, int c){
        while(p <= n){
            tree[p] += c;
            p += p & -p;
        }
    }

    int get(int p){
        int ans = 0;
        while(p > 0){
            ans += tree[p];
            p -= p & -p;
        }
        return ans;
    }
};

const int MAX = 2e5 + 10;

int N, Q;
pair<int, int> a[MAX], q[MAX];

void solve(){
    cin >> N >> Q;
    for(int i=1; i<=N; i++){
        cin >> a[i].fi >> a[i].se;
    }
    for(int i=1; i<=Q; i++){
        cin >> q[i].fi;
        q[i].se = i;
    }
    sort(a + 1, a + N + 1, [&](const pair<int, int> &a, const pair<int, int> &b){return max(a.fi, a.se) > max(b.fi, b.se);});
    sort(q + 1, q + Q + 1, greater<pair<int, int>>());
    fen bit(Q);
    seg it(Q);
    for(int i=1; i<=Q; i++){
        it.upd(q[i].se, q[i].fi);
    }
    int j = 1;
    ll ans = 0;
    for(int i=1; i<=N; i++){
        while(j <= Q && q[j].fi >= max(a[i].fi, a[i].se)){
            bit.upd(q[j].se, 1);
            it.upd(q[j].se, 0);
            j++;
        }
        int p = it.get(min(a[i].fi, a[i].se));
        if(p == -1){
            int c = bit.get(Q);
            if((c + (a[i].fi < a[i].se)) % 2 == 0){
                ans += max(a[i].fi, a[i].se);
            }
            else{
                ans += min(a[i].fi, a[i].se);
            }
        }
        else{
            int c = bit.get(Q) - bit.get(p);
            if(c % 2 == 0){
                ans += max(a[i].fi, a[i].se);
            }
            else{
                ans += min(a[i].fi, a[i].se);
            }
        }
        // cout << ans << ' ';
    }
    cout << ans << '\n';
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
//    freopen("mmgraph.inp", "r", stdin);
//    freopen("mmgraph.out", "w", stdout);

    int t = 1;
   // cin >> t;
    while(t--){
        solve();
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 2 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 2 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 7 ms 2904 KB Output is correct
15 Correct 11 ms 3656 KB Output is correct
16 Correct 17 ms 3932 KB Output is correct
17 Correct 23 ms 4620 KB Output is correct
18 Correct 25 ms 4436 KB Output is correct
19 Correct 26 ms 4440 KB Output is correct
20 Correct 26 ms 4440 KB Output is correct
21 Correct 20 ms 4444 KB Output is correct
22 Correct 18 ms 3932 KB Output is correct
23 Correct 21 ms 3928 KB Output is correct
24 Correct 20 ms 3932 KB Output is correct
25 Correct 17 ms 4172 KB Output is correct
26 Correct 23 ms 4440 KB Output is correct
27 Correct 23 ms 4444 KB Output is correct
28 Correct 22 ms 4436 KB Output is correct
29 Correct 25 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 2 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 7 ms 2904 KB Output is correct
15 Correct 11 ms 3656 KB Output is correct
16 Correct 17 ms 3932 KB Output is correct
17 Correct 23 ms 4620 KB Output is correct
18 Correct 25 ms 4436 KB Output is correct
19 Correct 26 ms 4440 KB Output is correct
20 Correct 26 ms 4440 KB Output is correct
21 Correct 20 ms 4444 KB Output is correct
22 Correct 18 ms 3932 KB Output is correct
23 Correct 21 ms 3928 KB Output is correct
24 Correct 20 ms 3932 KB Output is correct
25 Correct 17 ms 4172 KB Output is correct
26 Correct 23 ms 4440 KB Output is correct
27 Correct 23 ms 4444 KB Output is correct
28 Correct 22 ms 4436 KB Output is correct
29 Correct 25 ms 4444 KB Output is correct
30 Correct 94 ms 8640 KB Output is correct
31 Correct 90 ms 9508 KB Output is correct
32 Correct 107 ms 10576 KB Output is correct
33 Correct 126 ms 13136 KB Output is correct
34 Correct 56 ms 8564 KB Output is correct
35 Correct 130 ms 13064 KB Output is correct
36 Correct 127 ms 13140 KB Output is correct
37 Correct 136 ms 13140 KB Output is correct
38 Correct 131 ms 13224 KB Output is correct
39 Correct 128 ms 13396 KB Output is correct
40 Correct 102 ms 12884 KB Output is correct
41 Correct 127 ms 13140 KB Output is correct
42 Correct 127 ms 13136 KB Output is correct
43 Correct 88 ms 12528 KB Output is correct
44 Correct 89 ms 12524 KB Output is correct
45 Correct 89 ms 12368 KB Output is correct
46 Correct 104 ms 11320 KB Output is correct
47 Correct 112 ms 11092 KB Output is correct
48 Correct 131 ms 13132 KB Output is correct
49 Correct 125 ms 13236 KB Output is correct