답안 #791365

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
791365 2023-07-24T03:58:29 Z Cookie 운세 보기 2 (JOI14_fortune_telling2) C++14
100 / 100
402 ms 39364 KB
#include<bits/stdc++.h>
#include<fstream>
#pragma GCC optimize("Ofast,O3,unroll-loops")
#pragma GCC target("avx2")
using namespace std;
//ifstream fin("FEEDING.INP");
//ofstream fout("FEEDING.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const int mxn = 6e5, base = 74;
const ll p = 31, mod = 1e9 + 7;
int n, k;
int st[4 * mxn + 1], bit[mxn + 1];
void upd(int nd, int l, int r, int id, int v){
    if(id < l || id > r)return;
    if(l == r){
        st[nd] = max(st[nd], v);
        return;
    }
    int mid = (l + r) >> 1;
    upd(nd << 1, l, mid, id, v); upd(nd << 1 | 1, mid + 1, r, id, v);
    st[nd] = max(st[nd << 1], st[nd << 1 | 1]);
}
int get(int nd, int l, int r, int ql, int qr){
    if(ql > r || qr < l)return(0);
    if(ql <= l && qr >= r)return(st[nd]);
    int mid = (l + r) >> 1;
    return(max(get(nd << 1, l, mid, ql, qr), get(nd << 1 | 1, mid + 1, r, ql, qr)));
}
vt<int>comp;
void updbit(int p, int v){
    while(p <= sz(comp)){
        bit[p] += v; p += p & (-p);
    }
}
int getbit(int p){
    int ans = 0;
    while(p){
        ans += bit[p]; p -= p & (-p);
    }
    return(ans);
}
int find(int x){
    return(lower_bound(comp.begin(), comp.end(), x) - comp.begin() + 1);
}
vt<int>q[mxn + 1];
ll a[mxn + 1], b[mxn + 1], c[mxn + 1];
signed main()
{
     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> k;
    for(int i = 1; i <= n; i++){
        cin >> a[i] >> b[i]; comp.pb(a[i]); comp.pb(b[i]);
    }
    for(int i = 1; i <= k; i++){
        cin >> c[i]; comp.pb(c[i]);
    }
    sort(comp.begin(), comp.end());
    for(int i = 1; i <= k; i++){
        upd(1, 1, sz(comp), find(c[i]), i);
    }
    ll ans = 0;
    for(int i = 1; i <= n; i++){
        int mn = find(min(a[i], b[i])), mx = find(max(a[i], b[i]));
        if(mn == mx){
            ans += comp[mn - 1];
            continue;
        }
        int mxid = get(1, 1, sz(comp), mn, mx - 1);
        //cout << i << " " << mxid << "\n";
        q[mxid].pb(i);
    }
    for(int i = k; i >= 1; i--){
        for(auto j: q[i]){
            int mx = find(max(a[j], b[j]));
            ll turn = getbit(sz(comp)) - getbit(mx - 1);
            //cout << i << " " << j << " " << turn << "\n";
            if(turn & 1){
                ans += min(a[j], b[j]);
            }else{
                ans += max(a[j], b[j]);
            }
        }
        updbit(find(c[i]), 1);
    }
    for(auto i: q[0]){
        int mx = find(max(a[i], b[i]));
        int turn = getbit(sz(comp)) - getbit(mx - 1);
        if(turn % 2 == 0)ans += a[i];
        else ans += b[i];
    }
    cout << ans;
    return(0);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 9 ms 14548 KB Output is correct
4 Correct 8 ms 14548 KB Output is correct
5 Correct 8 ms 14420 KB Output is correct
6 Correct 8 ms 14492 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 8 ms 14420 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 8 ms 14532 KB Output is correct
11 Correct 8 ms 14548 KB Output is correct
12 Correct 8 ms 14500 KB Output is correct
13 Correct 7 ms 14548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 9 ms 14548 KB Output is correct
4 Correct 8 ms 14548 KB Output is correct
5 Correct 8 ms 14420 KB Output is correct
6 Correct 8 ms 14492 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 8 ms 14420 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 8 ms 14532 KB Output is correct
11 Correct 8 ms 14548 KB Output is correct
12 Correct 8 ms 14500 KB Output is correct
13 Correct 7 ms 14548 KB Output is correct
14 Correct 19 ms 15236 KB Output is correct
15 Correct 35 ms 15936 KB Output is correct
16 Correct 48 ms 17040 KB Output is correct
17 Correct 64 ms 17504 KB Output is correct
18 Correct 70 ms 17532 KB Output is correct
19 Correct 66 ms 17584 KB Output is correct
20 Correct 65 ms 17540 KB Output is correct
21 Correct 78 ms 17648 KB Output is correct
22 Correct 51 ms 17476 KB Output is correct
23 Correct 52 ms 17592 KB Output is correct
24 Correct 55 ms 17528 KB Output is correct
25 Correct 50 ms 17772 KB Output is correct
26 Correct 51 ms 17036 KB Output is correct
27 Correct 62 ms 17528 KB Output is correct
28 Correct 53 ms 17072 KB Output is correct
29 Correct 62 ms 17696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 9 ms 14548 KB Output is correct
4 Correct 8 ms 14548 KB Output is correct
5 Correct 8 ms 14420 KB Output is correct
6 Correct 8 ms 14492 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 8 ms 14420 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 8 ms 14532 KB Output is correct
11 Correct 8 ms 14548 KB Output is correct
12 Correct 8 ms 14500 KB Output is correct
13 Correct 7 ms 14548 KB Output is correct
14 Correct 19 ms 15236 KB Output is correct
15 Correct 35 ms 15936 KB Output is correct
16 Correct 48 ms 17040 KB Output is correct
17 Correct 64 ms 17504 KB Output is correct
18 Correct 70 ms 17532 KB Output is correct
19 Correct 66 ms 17584 KB Output is correct
20 Correct 65 ms 17540 KB Output is correct
21 Correct 78 ms 17648 KB Output is correct
22 Correct 51 ms 17476 KB Output is correct
23 Correct 52 ms 17592 KB Output is correct
24 Correct 55 ms 17528 KB Output is correct
25 Correct 50 ms 17772 KB Output is correct
26 Correct 51 ms 17036 KB Output is correct
27 Correct 62 ms 17528 KB Output is correct
28 Correct 53 ms 17072 KB Output is correct
29 Correct 62 ms 17696 KB Output is correct
30 Correct 129 ms 20104 KB Output is correct
31 Correct 183 ms 23600 KB Output is correct
32 Correct 236 ms 25472 KB Output is correct
33 Correct 394 ms 33432 KB Output is correct
34 Correct 120 ms 21632 KB Output is correct
35 Correct 402 ms 39096 KB Output is correct
36 Correct 395 ms 38860 KB Output is correct
37 Correct 385 ms 38876 KB Output is correct
38 Correct 349 ms 39052 KB Output is correct
39 Correct 392 ms 38972 KB Output is correct
40 Correct 337 ms 39364 KB Output is correct
41 Correct 361 ms 38920 KB Output is correct
42 Correct 349 ms 38748 KB Output is correct
43 Correct 249 ms 34076 KB Output is correct
44 Correct 265 ms 34460 KB Output is correct
45 Correct 235 ms 35836 KB Output is correct
46 Correct 264 ms 37080 KB Output is correct
47 Correct 259 ms 36744 KB Output is correct
48 Correct 277 ms 35892 KB Output is correct
49 Correct 278 ms 35800 KB Output is correct