답안 #971415

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
971415 2024-04-28T13:19:12 Z Whisper 운세 보기 2 (JOI14_fortune_telling2) C++17
100 / 100
434 ms 59264 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';
    }
}


# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 19032 KB Output is correct
2 Correct 4 ms 19036 KB Output is correct
3 Correct 5 ms 19036 KB Output is correct
4 Correct 5 ms 19036 KB Output is correct
5 Correct 6 ms 19036 KB Output is correct
6 Correct 6 ms 19036 KB Output is correct
7 Correct 5 ms 19036 KB Output is correct
8 Correct 5 ms 19036 KB Output is correct
9 Correct 5 ms 19032 KB Output is correct
10 Correct 5 ms 19036 KB Output is correct
11 Correct 5 ms 19124 KB Output is correct
12 Correct 5 ms 19036 KB Output is correct
13 Correct 6 ms 19036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 19032 KB Output is correct
2 Correct 4 ms 19036 KB Output is correct
3 Correct 5 ms 19036 KB Output is correct
4 Correct 5 ms 19036 KB Output is correct
5 Correct 6 ms 19036 KB Output is correct
6 Correct 6 ms 19036 KB Output is correct
7 Correct 5 ms 19036 KB Output is correct
8 Correct 5 ms 19036 KB Output is correct
9 Correct 5 ms 19032 KB Output is correct
10 Correct 5 ms 19036 KB Output is correct
11 Correct 5 ms 19124 KB Output is correct
12 Correct 5 ms 19036 KB Output is correct
13 Correct 6 ms 19036 KB Output is correct
14 Correct 17 ms 19932 KB Output is correct
15 Correct 32 ms 20700 KB Output is correct
16 Correct 49 ms 22216 KB Output is correct
17 Correct 74 ms 22984 KB Output is correct
18 Correct 65 ms 22972 KB Output is correct
19 Correct 68 ms 23008 KB Output is correct
20 Correct 64 ms 23012 KB Output is correct
21 Correct 67 ms 23248 KB Output is correct
22 Correct 49 ms 22504 KB Output is correct
23 Correct 49 ms 21484 KB Output is correct
24 Correct 47 ms 21476 KB Output is correct
25 Correct 49 ms 23000 KB Output is correct
26 Correct 48 ms 24268 KB Output is correct
27 Correct 54 ms 24784 KB Output is correct
28 Correct 54 ms 24528 KB Output is correct
29 Correct 59 ms 24548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 19032 KB Output is correct
2 Correct 4 ms 19036 KB Output is correct
3 Correct 5 ms 19036 KB Output is correct
4 Correct 5 ms 19036 KB Output is correct
5 Correct 6 ms 19036 KB Output is correct
6 Correct 6 ms 19036 KB Output is correct
7 Correct 5 ms 19036 KB Output is correct
8 Correct 5 ms 19036 KB Output is correct
9 Correct 5 ms 19032 KB Output is correct
10 Correct 5 ms 19036 KB Output is correct
11 Correct 5 ms 19124 KB Output is correct
12 Correct 5 ms 19036 KB Output is correct
13 Correct 6 ms 19036 KB Output is correct
14 Correct 17 ms 19932 KB Output is correct
15 Correct 32 ms 20700 KB Output is correct
16 Correct 49 ms 22216 KB Output is correct
17 Correct 74 ms 22984 KB Output is correct
18 Correct 65 ms 22972 KB Output is correct
19 Correct 68 ms 23008 KB Output is correct
20 Correct 64 ms 23012 KB Output is correct
21 Correct 67 ms 23248 KB Output is correct
22 Correct 49 ms 22504 KB Output is correct
23 Correct 49 ms 21484 KB Output is correct
24 Correct 47 ms 21476 KB Output is correct
25 Correct 49 ms 23000 KB Output is correct
26 Correct 48 ms 24268 KB Output is correct
27 Correct 54 ms 24784 KB Output is correct
28 Correct 54 ms 24528 KB Output is correct
29 Correct 59 ms 24548 KB Output is correct
30 Correct 138 ms 26308 KB Output is correct
31 Correct 196 ms 36540 KB Output is correct
32 Correct 285 ms 40568 KB Output is correct
33 Correct 434 ms 58816 KB Output is correct
34 Correct 136 ms 27932 KB Output is correct
35 Correct 425 ms 57908 KB Output is correct
36 Correct 426 ms 58188 KB Output is correct
37 Correct 430 ms 57160 KB Output is correct
38 Correct 434 ms 58044 KB Output is correct
39 Correct 400 ms 58032 KB Output is correct
40 Correct 394 ms 59264 KB Output is correct
41 Correct 423 ms 56764 KB Output is correct
42 Correct 412 ms 57448 KB Output is correct
43 Correct 272 ms 50356 KB Output is correct
44 Correct 261 ms 52528 KB Output is correct
45 Correct 265 ms 51892 KB Output is correct
46 Correct 267 ms 45620 KB Output is correct
47 Correct 242 ms 40628 KB Output is correct
48 Correct 306 ms 49152 KB Output is correct
49 Correct 291 ms 49340 KB Output is correct