Submission #977293

# Submission time Handle Problem Language Result Execution time Memory
977293 2024-05-07T16:58:52 Z VinhLuu Fortune Telling 2 (JOI14_fortune_telling2) C++17
35 / 100
852 ms 126588 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 = 2e5 + 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 18 ms 52060 KB Output is correct
2 Correct 12 ms 52180 KB Output is correct
3 Correct 13 ms 50268 KB Output is correct
4 Correct 12 ms 50268 KB Output is correct
5 Correct 11 ms 50132 KB Output is correct
6 Correct 12 ms 50268 KB Output is correct
7 Correct 11 ms 50268 KB Output is correct
8 Correct 12 ms 52060 KB Output is correct
9 Correct 12 ms 52160 KB Output is correct
10 Correct 13 ms 52060 KB Output is correct
11 Correct 12 ms 52176 KB Output is correct
12 Correct 12 ms 52216 KB Output is correct
13 Correct 12 ms 52060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 52060 KB Output is correct
2 Correct 12 ms 52180 KB Output is correct
3 Correct 13 ms 50268 KB Output is correct
4 Correct 12 ms 50268 KB Output is correct
5 Correct 11 ms 50132 KB Output is correct
6 Correct 12 ms 50268 KB Output is correct
7 Correct 11 ms 50268 KB Output is correct
8 Correct 12 ms 52060 KB Output is correct
9 Correct 12 ms 52160 KB Output is correct
10 Correct 13 ms 52060 KB Output is correct
11 Correct 12 ms 52176 KB Output is correct
12 Correct 12 ms 52216 KB Output is correct
13 Correct 12 ms 52060 KB Output is correct
14 Correct 31 ms 61020 KB Output is correct
15 Correct 52 ms 64204 KB Output is correct
16 Correct 75 ms 67528 KB Output is correct
17 Correct 101 ms 72540 KB Output is correct
18 Correct 104 ms 72760 KB Output is correct
19 Correct 104 ms 72776 KB Output is correct
20 Correct 99 ms 70740 KB Output is correct
21 Correct 94 ms 72528 KB Output is correct
22 Correct 65 ms 67408 KB Output is correct
23 Correct 59 ms 65660 KB Output is correct
24 Correct 55 ms 65956 KB Output is correct
25 Correct 69 ms 70240 KB Output is correct
26 Correct 72 ms 68576 KB Output is correct
27 Correct 82 ms 69396 KB Output is correct
28 Correct 81 ms 69576 KB Output is correct
29 Correct 84 ms 70824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 52060 KB Output is correct
2 Correct 12 ms 52180 KB Output is correct
3 Correct 13 ms 50268 KB Output is correct
4 Correct 12 ms 50268 KB Output is correct
5 Correct 11 ms 50132 KB Output is correct
6 Correct 12 ms 50268 KB Output is correct
7 Correct 11 ms 50268 KB Output is correct
8 Correct 12 ms 52060 KB Output is correct
9 Correct 12 ms 52160 KB Output is correct
10 Correct 13 ms 52060 KB Output is correct
11 Correct 12 ms 52176 KB Output is correct
12 Correct 12 ms 52216 KB Output is correct
13 Correct 12 ms 52060 KB Output is correct
14 Correct 31 ms 61020 KB Output is correct
15 Correct 52 ms 64204 KB Output is correct
16 Correct 75 ms 67528 KB Output is correct
17 Correct 101 ms 72540 KB Output is correct
18 Correct 104 ms 72760 KB Output is correct
19 Correct 104 ms 72776 KB Output is correct
20 Correct 99 ms 70740 KB Output is correct
21 Correct 94 ms 72528 KB Output is correct
22 Correct 65 ms 67408 KB Output is correct
23 Correct 59 ms 65660 KB Output is correct
24 Correct 55 ms 65956 KB Output is correct
25 Correct 69 ms 70240 KB Output is correct
26 Correct 72 ms 68576 KB Output is correct
27 Correct 82 ms 69396 KB Output is correct
28 Correct 81 ms 69576 KB Output is correct
29 Correct 84 ms 70824 KB Output is correct
30 Correct 208 ms 88400 KB Output is correct
31 Correct 314 ms 98612 KB Output is correct
32 Correct 455 ms 107604 KB Output is correct
33 Incorrect 852 ms 126588 KB Output isn't correct
34 Halted 0 ms 0 KB -