Submission #977296

# Submission time Handle Problem Language Result Execution time Memory
977296 2024-05-07T17:04:44 Z VinhLuu Fortune Telling 2 (JOI14_fortune_telling2) C++17
35 / 100
587 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;
    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);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 86 ms 205908 KB Output is correct
2 Correct 46 ms 205916 KB Output is correct
3 Correct 46 ms 203864 KB Output is correct
4 Correct 45 ms 203868 KB Output is correct
5 Correct 46 ms 203868 KB Output is correct
6 Correct 46 ms 204072 KB Output is correct
7 Correct 49 ms 204112 KB Output is correct
8 Correct 46 ms 205912 KB Output is correct
9 Correct 46 ms 206004 KB Output is correct
10 Correct 45 ms 205908 KB Output is correct
11 Correct 45 ms 205912 KB Output is correct
12 Correct 45 ms 205920 KB Output is correct
13 Correct 56 ms 206020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 205908 KB Output is correct
2 Correct 46 ms 205916 KB Output is correct
3 Correct 46 ms 203864 KB Output is correct
4 Correct 45 ms 203868 KB Output is correct
5 Correct 46 ms 203868 KB Output is correct
6 Correct 46 ms 204072 KB Output is correct
7 Correct 49 ms 204112 KB Output is correct
8 Correct 46 ms 205912 KB Output is correct
9 Correct 46 ms 206004 KB Output is correct
10 Correct 45 ms 205908 KB Output is correct
11 Correct 45 ms 205912 KB Output is correct
12 Correct 45 ms 205920 KB Output is correct
13 Correct 56 ms 206020 KB Output is correct
14 Correct 64 ms 210576 KB Output is correct
15 Correct 98 ms 211296 KB Output is correct
16 Correct 106 ms 214176 KB Output is correct
17 Correct 143 ms 219644 KB Output is correct
18 Correct 130 ms 219728 KB Output is correct
19 Correct 129 ms 217936 KB Output is correct
20 Correct 138 ms 217868 KB Output is correct
21 Correct 129 ms 219612 KB Output is correct
22 Correct 102 ms 215888 KB Output is correct
23 Correct 93 ms 212452 KB Output is correct
24 Correct 87 ms 212564 KB Output is correct
25 Correct 101 ms 217012 KB Output is correct
26 Correct 102 ms 214616 KB Output is correct
27 Correct 110 ms 215460 KB Output is correct
28 Correct 105 ms 215120 KB Output is correct
29 Correct 126 ms 214928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 205908 KB Output is correct
2 Correct 46 ms 205916 KB Output is correct
3 Correct 46 ms 203864 KB Output is correct
4 Correct 45 ms 203868 KB Output is correct
5 Correct 46 ms 203868 KB Output is correct
6 Correct 46 ms 204072 KB Output is correct
7 Correct 49 ms 204112 KB Output is correct
8 Correct 46 ms 205912 KB Output is correct
9 Correct 46 ms 206004 KB Output is correct
10 Correct 45 ms 205908 KB Output is correct
11 Correct 45 ms 205912 KB Output is correct
12 Correct 45 ms 205920 KB Output is correct
13 Correct 56 ms 206020 KB Output is correct
14 Correct 64 ms 210576 KB Output is correct
15 Correct 98 ms 211296 KB Output is correct
16 Correct 106 ms 214176 KB Output is correct
17 Correct 143 ms 219644 KB Output is correct
18 Correct 130 ms 219728 KB Output is correct
19 Correct 129 ms 217936 KB Output is correct
20 Correct 138 ms 217868 KB Output is correct
21 Correct 129 ms 219612 KB Output is correct
22 Correct 102 ms 215888 KB Output is correct
23 Correct 93 ms 212452 KB Output is correct
24 Correct 87 ms 212564 KB Output is correct
25 Correct 101 ms 217012 KB Output is correct
26 Correct 102 ms 214616 KB Output is correct
27 Correct 110 ms 215460 KB Output is correct
28 Correct 105 ms 215120 KB Output is correct
29 Correct 126 ms 214928 KB Output is correct
30 Correct 269 ms 230744 KB Output is correct
31 Correct 329 ms 240204 KB Output is correct
32 Correct 539 ms 246320 KB Output is correct
33 Runtime error 587 ms 262144 KB Execution killed with signal 9
34 Halted 0 ms 0 KB -