답안 #977299

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
977299 2024-05-07T17:07:16 Z VinhLuu 운세 보기 2 (JOI14_fortune_telling2) C++17
100 / 100
833 ms 183888 KB
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
//#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(lmao) lmao.begin(), lmao.end()

using namespace std;

typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
const int N = 2e6 + 5;
const int oo = 1e9 + 1;
const int mod = 1e9 + 7;
//const ll oo = 5e18;

int n, k, d[N], a[N], b[N], x[N], y[N], pt[N], t, mx[N], mi[N], st[N], f[N], e[N];

void update(int i,int x){
    i += t - 1;
    mx[i] = max(mx[i], x);
    mi[i] = min(mi[i], x);
    while(i > 1){
        i /= 2;
        mx[i] = max(mx[i << 1], mx[i << 1|1]);
        mi[i] = min(mi[i << 1], mi[i << 1|1]);
    }
}

void add(int i){
    i += k - 1;
    st[i]++;
    while(i > 1){i /= 2;st[i]++;}
}

int get(int type,int l,int r){
    r++;
    int Max = 0, Min = 1e9;
    for(l += t - 1, r += t - 1; l < r; l /= 2, r /= 2){
        if(l & 1) Min = min(Min, mi[l]), Max = max(Max, mx[l]), l++;
        if(r & 1) --r, Min = min(Min, mi[r]), Max = max(Max, mx[r]);
    }
    if(type == 0) return Min;
    return Max;
}

int query(int l,int r){
    if(l > r) return 0;
    r++;
    int ret = 0;
    for(l += k - 1, r += k - 1; l < r; l /= 2, r /= 2){
        if(l & 1) ret += st[l++];
        if(r & 1) ret += st[--r];
    }
    return ret;
}

vector<int> T[N], op[N];

signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    #define task "v"
    if(fopen(task ".inp","r")){
        freopen(task ".inp","r",stdin);
        freopen(task ".out","w",stdout);
    }
    cin >> n >> k;
    set<int> s;
    ll ans = 0;
    for(int i = 1; i <= n; i ++){
        cin >> a[i] >> b[i];
        if(a[i] == b[i]){
            ans += a[i];
            continue;
        }
        s.insert(a[i]); s.insert(b[i]);
    }
    for(int i = 1; i <= k; i ++){
        cin >> d[i];
        s.insert(d[i]);
    }
    for(auto j : s) pt[++t] = j;
//    for(int i = 1; i <= t; i ++) cerr << pt[i] << " ";
//    cerr << "\n";
    for(int i = 1; i <= 2 * t; i ++) mi[i] = 1e9;
    for(int i = 1; i <= k; i ++){
        d[i] = lower_bound(pt + 1, pt + t + 1, d[i]) - pt;
//        cerr << d[i] << " e\n";
        op[d[i]].pb(i);
    }

    for(int i = 1; i <= n; i ++){
        if(a[i] == b[i]) continue;

        a[i] = lower_bound(pt + 1, pt + t + 1, a[i]) - pt;
        b[i] = lower_bound(pt + 1, pt + t + 1, b[i]) - pt;
//        cerr << a[i] << " " << b[i] << " y\n";
        x[i] = min(a[i], b[i]);
        y[i] = max(a[i], b[i]);
        T[x[i]].pb(i);
    }

    for(int i = t; i >= 1; i --){
        for(auto j : op[i]) update(d[j], j);
        for(auto j : T[i]){
            f[j] = get(0, x[j], y[j] - 1);
            e[j] = get(1, x[j], y[j] - 1);
        }
        T[i].clear();
    }

    for(int i = 1; i <= n; i ++) T[y[i]].pb(i);
    for(int i = t; i >= 1; i --){
        for(auto j : op[i]) add(j);
//        cerr << i << " h\n";
        for(auto j : T[i]){
//            cerr << j << " " << f[j] << " " << e[j] << " t\n";
            if(f[j] == 1e9 || e[j] == 0){
                int tmp = query(1, k);
                ans += pt[(tmp % 2 ? b[j] : a[j])];
//                cerr << j << " " << tmp << " after\n";
            }else{
                int pre = query(1, f[j] - 1);
                int suf = query(e[j] + 1, k);
//                cerr << pre << " " << suf << " t\n";
                if(pre % 2) swap(a[j], b[j]);
//                cerr << a[j] << " " << b[j] << "\n";
                if(a[j] <= b[j]){
                    swap(a[j], b[j]);
//                    cerr << a[j] << " " << b[j] << "\n";
                    if(suf % 2) swap(a[j], b[j]);
//                    cerr << a[j] << " " << pt[a[j]] << " after\n";
                    ans += pt[a[j]];
                }else{
                    if(suf % 2) swap(a[j], b[j]);
//                    cerr << a[j] << " " << pt[a[j]] << " after\n";
                    ans += pt[a[j]];
                }
            }
        }
        T[i].clear();

    }
    cout << ans;

}

Compilation message

fortune_telling2.cpp: In function 'int main()':
fortune_telling2.cpp:68:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |         freopen(task ".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
fortune_telling2.cpp:69:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |         freopen(task ".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 111452 KB Output is correct
2 Correct 24 ms 111452 KB Output is correct
3 Correct 25 ms 109480 KB Output is correct
4 Correct 25 ms 109620 KB Output is correct
5 Correct 26 ms 109660 KB Output is correct
6 Correct 24 ms 109660 KB Output is correct
7 Correct 27 ms 109660 KB Output is correct
8 Correct 30 ms 111704 KB Output is correct
9 Correct 27 ms 111720 KB Output is correct
10 Correct 24 ms 111360 KB Output is correct
11 Correct 25 ms 111452 KB Output is correct
12 Correct 24 ms 111448 KB Output is correct
13 Correct 25 ms 111452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 111452 KB Output is correct
2 Correct 24 ms 111452 KB Output is correct
3 Correct 25 ms 109480 KB Output is correct
4 Correct 25 ms 109620 KB Output is correct
5 Correct 26 ms 109660 KB Output is correct
6 Correct 24 ms 109660 KB Output is correct
7 Correct 27 ms 109660 KB Output is correct
8 Correct 30 ms 111704 KB Output is correct
9 Correct 27 ms 111720 KB Output is correct
10 Correct 24 ms 111360 KB Output is correct
11 Correct 25 ms 111452 KB Output is correct
12 Correct 24 ms 111448 KB Output is correct
13 Correct 25 ms 111452 KB Output is correct
14 Correct 42 ms 115952 KB Output is correct
15 Correct 65 ms 116772 KB Output is correct
16 Correct 87 ms 119632 KB Output is correct
17 Correct 110 ms 124184 KB Output is correct
18 Correct 114 ms 124060 KB Output is correct
19 Correct 125 ms 122304 KB Output is correct
20 Correct 108 ms 122380 KB Output is correct
21 Correct 106 ms 124044 KB Output is correct
22 Correct 76 ms 121428 KB Output is correct
23 Correct 95 ms 117968 KB Output is correct
24 Correct 69 ms 118252 KB Output is correct
25 Correct 78 ms 122192 KB Output is correct
26 Correct 81 ms 120148 KB Output is correct
27 Correct 91 ms 120772 KB Output is correct
28 Correct 94 ms 120840 KB Output is correct
29 Correct 93 ms 120440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 111452 KB Output is correct
2 Correct 24 ms 111452 KB Output is correct
3 Correct 25 ms 109480 KB Output is correct
4 Correct 25 ms 109620 KB Output is correct
5 Correct 26 ms 109660 KB Output is correct
6 Correct 24 ms 109660 KB Output is correct
7 Correct 27 ms 109660 KB Output is correct
8 Correct 30 ms 111704 KB Output is correct
9 Correct 27 ms 111720 KB Output is correct
10 Correct 24 ms 111360 KB Output is correct
11 Correct 25 ms 111452 KB Output is correct
12 Correct 24 ms 111448 KB Output is correct
13 Correct 25 ms 111452 KB Output is correct
14 Correct 42 ms 115952 KB Output is correct
15 Correct 65 ms 116772 KB Output is correct
16 Correct 87 ms 119632 KB Output is correct
17 Correct 110 ms 124184 KB Output is correct
18 Correct 114 ms 124060 KB Output is correct
19 Correct 125 ms 122304 KB Output is correct
20 Correct 108 ms 122380 KB Output is correct
21 Correct 106 ms 124044 KB Output is correct
22 Correct 76 ms 121428 KB Output is correct
23 Correct 95 ms 117968 KB Output is correct
24 Correct 69 ms 118252 KB Output is correct
25 Correct 78 ms 122192 KB Output is correct
26 Correct 81 ms 120148 KB Output is correct
27 Correct 91 ms 120772 KB Output is correct
28 Correct 94 ms 120840 KB Output is correct
29 Correct 93 ms 120440 KB Output is correct
30 Correct 232 ms 138960 KB Output is correct
31 Correct 310 ms 147900 KB Output is correct
32 Correct 450 ms 153936 KB Output is correct
33 Correct 812 ms 178048 KB Output is correct
34 Correct 173 ms 139088 KB Output is correct
35 Correct 815 ms 183720 KB Output is correct
36 Correct 791 ms 181736 KB Output is correct
37 Correct 802 ms 183540 KB Output is correct
38 Correct 833 ms 181584 KB Output is correct
39 Correct 804 ms 181132 KB Output is correct
40 Correct 665 ms 183352 KB Output is correct
41 Correct 766 ms 183888 KB Output is correct
42 Correct 783 ms 181964 KB Output is correct
43 Correct 380 ms 182356 KB Output is correct
44 Correct 419 ms 182588 KB Output is correct
45 Correct 387 ms 182612 KB Output is correct
46 Correct 367 ms 157692 KB Output is correct
47 Correct 286 ms 145176 KB Output is correct
48 Correct 495 ms 165308 KB Output is correct
49 Correct 445 ms 164988 KB Output is correct