Submission #971416

# Submission time Handle Problem Language Result Execution time Memory
971416 2024-04-28T13:19:42 Z vjudge1 Fortune Telling 2 (JOI14_fortune_telling2) C++17
100 / 100
445 ms 53252 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define int long long
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = b; i >= a; i --)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPD(i, n) for (int i = n - 1; i >= 0; --i)

#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)

constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void setupIO(){
    #define name "Whisper"
    //Phu Trong from Nguyen Tat Thanh High School for gifted student
    srand(time(NULL));
    cin.tie(nullptr)->sync_with_stdio(false); cout.tie(nullptr);
    //freopen(name".inp", "r", stdin);
    //freopen(name".out", "w", stdout);
    cout << fixed << setprecision(10);
}

template <class X, class Y>
    bool minimize(X &x, const Y &y){
        X eps = 1e-9;
        if (x > y + eps) {x = y; return 1;}
        return 0;
    }

template <class X, class Y>
    bool maximize(X &x, const Y &y){
        X eps = 1e-9;
        if (x + eps < y) {x = y; return 1;}
        return 0;
    }
#define All(a...) a.begin(), a.end()

const int MAX = 6e5 + 5;
int nArr, numQuery;
pair<int, int> A[MAX];
int qs[MAX];
vector<int> comp;

int find(int x){
    return lower_bound(All(comp), x) - comp.begin() + 1;
}
int st[MAX << 2], F[MAX];

void Modify(int nd, int l, int r, int pos, int val){
    if (pos < l || pos > r) return;
    if(l == r){
        st[nd] = max(st[nd], val);
        return;
    }
    int mid = (l + r) >> 1;
    Modify(nd << 1, l, mid, pos, val);
    Modify(nd << 1 | 1, mid + 1, r, pos, val);
    st[nd] = max(st[nd << 1], st[nd << 1 | 1]);
}
int Query(int nd, int l, int r, int u, int  v){
    if (u > r || v < l) return 0;
    if (u <= l && v >= r) return st[nd];
    int mid = (l + r) >> 1;
    int ql = Query(nd << 1, l, mid, u, v);
    int qr = Query(nd << 1 | 1, mid + 1, r, u, v);
    return max(ql, qr);
}

void Modify(int pos, int val){ Modify(1, 1, comp.size(), pos, val); }
int Query(int l, int r){ return Query(1, 1, comp.size(), l, r); }

void upd(int p, int val){
    for (; p <= (int)comp.size(); p += p & (-p)) F[p] += val;
}
int get(int p){
    int res = 0;
    for (; p > 0; p -= p & (-p)) res += F[p];
    return res;
}

vector<int> event[MAX];

void Whisper(){
    cin >> nArr >> numQuery;
    for (int i = 1; i <= nArr; ++i){
        cin >> A[i].first >> A[i].second;
        comp.push_back(A[i].first);
        comp.push_back(A[i].second);
    }

    for (int i = 1; i <= numQuery; ++i){
        cin >> qs[i];
        comp.push_back(qs[i]);
    }
    sort(All(comp));
    comp.resize(unique(All(comp)) - comp.begin());
    for (int i = 1; i <= numQuery; ++i){
        Modify(find(qs[i]), i);
    }
    int ans = 0;
    for (int i = 1; i <= nArr; ++i){
        int l = find(min(A[i].first, A[i].second));
        int r = find(max(A[i].first, A[i].second));
        if (l == r){
            ans += A[i].first; continue;
        }
        int mx_id = Query(l, r - 1);
        event[mx_id].emplace_back(i);
    }

    for (int i = numQuery; i >= 1; --i){
        for (int &j : event[i]){
            int v = find(max(A[j].first, A[j].second));
            int turn = get((int)comp.size()) - get(v - 1);
            if(turn & 1) {
                ans += min(A[j].first, A[j].second);
            }
            else{
                ans += max(A[j].first, A[j].second);
            }
        }
        upd(find(qs[i]), 1);
    }

    for (int &j : event[0]){
        int v = find(max(A[j].first, A[j].second));
        int turn = get((int)comp.size()) - get(v - 1);
        if (turn & 1) ans += A[j].second;
        else ans += A[j].first;
    }

    cout << ans;
    /*
        Case 1: if A(i).first <= qs(j) < A(i).second
        -> the answer always = A(i).second
        Case 2: if qs(j) > A(i).second
        -> the cards is always turn down
        Case 3: if qs(j) < A(i).first
        -> The cards isn't affected
    */
}


signed main(){
    setupIO();
    int Test = 1;
//    cin >> Test;
    for ( int i = 1 ; i <= Test ; i++ ){
        Whisper();
        if (i < Test) cout << '\n';
    }
}


# Verdict Execution time Memory Grader output
1 Correct 4 ms 19036 KB Output is correct
2 Correct 4 ms 19036 KB Output is correct
3 Correct 5 ms 19032 KB Output is correct
4 Correct 5 ms 19036 KB Output is correct
5 Correct 5 ms 19036 KB Output is correct
6 Correct 6 ms 19292 KB Output is correct
7 Correct 5 ms 19036 KB Output is correct
8 Correct 5 ms 19036 KB Output is correct
9 Correct 4 ms 19032 KB Output is correct
10 Correct 4 ms 19036 KB Output is correct
11 Correct 5 ms 19036 KB Output is correct
12 Correct 4 ms 19036 KB Output is correct
13 Correct 6 ms 19036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 19036 KB Output is correct
2 Correct 4 ms 19036 KB Output is correct
3 Correct 5 ms 19032 KB Output is correct
4 Correct 5 ms 19036 KB Output is correct
5 Correct 5 ms 19036 KB Output is correct
6 Correct 6 ms 19292 KB Output is correct
7 Correct 5 ms 19036 KB Output is correct
8 Correct 5 ms 19036 KB Output is correct
9 Correct 4 ms 19032 KB Output is correct
10 Correct 4 ms 19036 KB Output is correct
11 Correct 5 ms 19036 KB Output is correct
12 Correct 4 ms 19036 KB Output is correct
13 Correct 6 ms 19036 KB Output is correct
14 Correct 18 ms 19936 KB Output is correct
15 Correct 34 ms 20752 KB Output is correct
16 Correct 51 ms 22252 KB Output is correct
17 Correct 64 ms 22988 KB Output is correct
18 Correct 64 ms 22988 KB Output is correct
19 Correct 64 ms 22984 KB Output is correct
20 Correct 62 ms 23024 KB Output is correct
21 Correct 66 ms 23244 KB Output is correct
22 Correct 50 ms 22732 KB Output is correct
23 Correct 48 ms 21584 KB Output is correct
24 Correct 47 ms 21484 KB Output is correct
25 Correct 56 ms 22984 KB Output is correct
26 Correct 48 ms 24268 KB Output is correct
27 Correct 55 ms 24780 KB Output is correct
28 Correct 52 ms 24436 KB Output is correct
29 Correct 59 ms 24524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 19036 KB Output is correct
2 Correct 4 ms 19036 KB Output is correct
3 Correct 5 ms 19032 KB Output is correct
4 Correct 5 ms 19036 KB Output is correct
5 Correct 5 ms 19036 KB Output is correct
6 Correct 6 ms 19292 KB Output is correct
7 Correct 5 ms 19036 KB Output is correct
8 Correct 5 ms 19036 KB Output is correct
9 Correct 4 ms 19032 KB Output is correct
10 Correct 4 ms 19036 KB Output is correct
11 Correct 5 ms 19036 KB Output is correct
12 Correct 4 ms 19036 KB Output is correct
13 Correct 6 ms 19036 KB Output is correct
14 Correct 18 ms 19936 KB Output is correct
15 Correct 34 ms 20752 KB Output is correct
16 Correct 51 ms 22252 KB Output is correct
17 Correct 64 ms 22988 KB Output is correct
18 Correct 64 ms 22988 KB Output is correct
19 Correct 64 ms 22984 KB Output is correct
20 Correct 62 ms 23024 KB Output is correct
21 Correct 66 ms 23244 KB Output is correct
22 Correct 50 ms 22732 KB Output is correct
23 Correct 48 ms 21584 KB Output is correct
24 Correct 47 ms 21484 KB Output is correct
25 Correct 56 ms 22984 KB Output is correct
26 Correct 48 ms 24268 KB Output is correct
27 Correct 55 ms 24780 KB Output is correct
28 Correct 52 ms 24436 KB Output is correct
29 Correct 59 ms 24524 KB Output is correct
30 Correct 138 ms 26480 KB Output is correct
31 Correct 193 ms 33468 KB Output is correct
32 Correct 250 ms 36288 KB Output is correct
33 Correct 418 ms 52384 KB Output is correct
34 Correct 129 ms 25796 KB Output is correct
35 Correct 427 ms 51956 KB Output is correct
36 Correct 427 ms 51024 KB Output is correct
37 Correct 419 ms 51424 KB Output is correct
38 Correct 411 ms 51776 KB Output is correct
39 Correct 404 ms 52716 KB Output is correct
40 Correct 395 ms 53252 KB Output is correct
41 Correct 445 ms 51384 KB Output is correct
42 Correct 427 ms 50728 KB Output is correct
43 Correct 270 ms 44444 KB Output is correct
44 Correct 258 ms 45496 KB Output is correct
45 Correct 267 ms 46520 KB Output is correct
46 Correct 278 ms 42084 KB Output is correct
47 Correct 276 ms 35256 KB Output is correct
48 Correct 305 ms 43576 KB Output is correct
49 Correct 301 ms 43004 KB Output is correct