Submission #977295

# Submission time Handle Problem Language Result Execution time Memory
977295 2024-05-07T17:00:46 Z VinhLuu Fortune Telling 2 (JOI14_fortune_telling2) C++17
35 / 100
401 ms 262144 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 = 1e6 + 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 << 2], t, mx[N << 2], mi[N << 2], st[N << 2], 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 << 2], op[N << 2];

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;
    int 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){
                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);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 100 ms 205904 KB Output is correct
2 Correct 46 ms 206028 KB Output is correct
3 Correct 46 ms 203860 KB Output is correct
4 Correct 48 ms 203852 KB Output is correct
5 Correct 47 ms 204112 KB Output is correct
6 Correct 47 ms 203860 KB Output is correct
7 Correct 46 ms 204124 KB Output is correct
8 Correct 47 ms 205908 KB Output is correct
9 Correct 46 ms 205912 KB Output is correct
10 Correct 45 ms 205872 KB Output is correct
11 Correct 46 ms 205904 KB Output is correct
12 Correct 45 ms 206068 KB Output is correct
13 Correct 46 ms 205876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 100 ms 205904 KB Output is correct
2 Correct 46 ms 206028 KB Output is correct
3 Correct 46 ms 203860 KB Output is correct
4 Correct 48 ms 203852 KB Output is correct
5 Correct 47 ms 204112 KB Output is correct
6 Correct 47 ms 203860 KB Output is correct
7 Correct 46 ms 204124 KB Output is correct
8 Correct 47 ms 205908 KB Output is correct
9 Correct 46 ms 205912 KB Output is correct
10 Correct 45 ms 205872 KB Output is correct
11 Correct 46 ms 205904 KB Output is correct
12 Correct 45 ms 206068 KB Output is correct
13 Correct 46 ms 205876 KB Output is correct
14 Correct 62 ms 208888 KB Output is correct
15 Correct 87 ms 215260 KB Output is correct
16 Correct 112 ms 220240 KB Output is correct
17 Correct 149 ms 224752 KB Output is correct
18 Correct 139 ms 224764 KB Output is correct
19 Correct 135 ms 223312 KB Output is correct
20 Correct 134 ms 223240 KB Output is correct
21 Correct 132 ms 224592 KB Output is correct
22 Correct 101 ms 219732 KB Output is correct
23 Correct 94 ms 216620 KB Output is correct
24 Correct 90 ms 215116 KB Output is correct
25 Correct 101 ms 222804 KB Output is correct
26 Correct 104 ms 218540 KB Output is correct
27 Correct 115 ms 219216 KB Output is correct
28 Correct 106 ms 219192 KB Output is correct
29 Correct 119 ms 221568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 100 ms 205904 KB Output is correct
2 Correct 46 ms 206028 KB Output is correct
3 Correct 46 ms 203860 KB Output is correct
4 Correct 48 ms 203852 KB Output is correct
5 Correct 47 ms 204112 KB Output is correct
6 Correct 47 ms 203860 KB Output is correct
7 Correct 46 ms 204124 KB Output is correct
8 Correct 47 ms 205908 KB Output is correct
9 Correct 46 ms 205912 KB Output is correct
10 Correct 45 ms 205872 KB Output is correct
11 Correct 46 ms 205904 KB Output is correct
12 Correct 45 ms 206068 KB Output is correct
13 Correct 46 ms 205876 KB Output is correct
14 Correct 62 ms 208888 KB Output is correct
15 Correct 87 ms 215260 KB Output is correct
16 Correct 112 ms 220240 KB Output is correct
17 Correct 149 ms 224752 KB Output is correct
18 Correct 139 ms 224764 KB Output is correct
19 Correct 135 ms 223312 KB Output is correct
20 Correct 134 ms 223240 KB Output is correct
21 Correct 132 ms 224592 KB Output is correct
22 Correct 101 ms 219732 KB Output is correct
23 Correct 94 ms 216620 KB Output is correct
24 Correct 90 ms 215116 KB Output is correct
25 Correct 101 ms 222804 KB Output is correct
26 Correct 104 ms 218540 KB Output is correct
27 Correct 115 ms 219216 KB Output is correct
28 Correct 106 ms 219192 KB Output is correct
29 Correct 119 ms 221568 KB Output is correct
30 Correct 274 ms 238268 KB Output is correct
31 Correct 350 ms 250196 KB Output is correct
32 Runtime error 401 ms 262144 KB Execution killed with signal 9
33 Halted 0 ms 0 KB -