Submission #814445

# Submission time Handle Problem Language Result Execution time Memory
814445 2023-08-08T07:30:49 Z 이동현(#10122) Arranging Tickets (JOI17_arranging_tickets) C++17
0 / 100
4181 ms 10976 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;

struct Seg{
    int n;
    vector<int> tr, lazy;

    Seg(int m){
        n = m + 4;
        tr.resize(n * 4 + 4);
        lazy.resize(n * 4 + 4);
    }

    void push(int x, int s, int e, int ps, int pe, int val){
        if(pe < s || ps > e || ps > pe) return;
        if(ps <= s && pe >= e){
            tr[x] += val;
            lazy[x] += val;
            return;
        }
        if(lazy[x]){
            tr[x * 2] += lazy[x], lazy[x * 2] += lazy[x];
            tr[x * 2 + 1] += lazy[x], lazy[x * 2 + 1] += lazy[x];
            lazy[x] = 0;
        }

        int m = s + e >> 1;
        push(x * 2, s, m, ps, pe, val);
        push(x * 2 + 1, m + 1, e, ps, pe, val);

        tr[x] = max(tr[x * 2], tr[x * 2 + 1]);
    }
};

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

    int L, n;
    cin >> L >> n;

    int test = 1;

    if(test){
        L = 200000;
        n = 100000;
    }
    Seg tr(L + 4);
    vector<array<int, 4>> a(n), b;
    long long alladd = 0;
    for(int i = 0; i < n; ++i){
        cin >> a[i][0] >> a[i][1] >> a[i][2];
        --a[i][0], --a[i][1];

        if(test){
            a[i][2] = (long long)rand() * rand() % (int)1e9;
            a[i][0] = rand() % 3000;
            a[i][1] = rand() % 3000;
        }

        if(a[i][0] > a[i][1]) swap(a[i][0], a[i][1]);

        if(a[i][2] % 2){
            alladd += a[i][2] / 2;
            a[i][2] = 1;
        }
        else{
            alladd += (a[i][2] - 2) / 2;
            a[i][2] = 1;
            tr.push(1, 0, L - 1, a[i][0], a[i][1] - 1, 1);
            b.push_back(a[i]);
        }

        tr.push(1, 0, L - 1, a[i][0], a[i][1] - 1, 1);
    }

    for(auto&i:b){
        a.push_back(i);
        ++n;
    }

    auto flip = [&](int id)->void{
        if(a[id][3] == 1){
            tr.push(1, 0, L - 1, a[id][1], L - 1, -1);
            tr.push(1, 0, L - 1, 0, a[id][0] - 1, -1);
            tr.push(1, 0, L - 1, a[id][0], a[id][1] - 1, 1);
        }
        else{
            tr.push(1, 0, L - 1, a[id][1], L - 1, 1);
            tr.push(1, 0, L - 1, 0, a[id][0] - 1, 1);
            tr.push(1, 0, L - 1, a[id][0], a[id][1] - 1, -1);
        }
        a[id][3] ^= 1;
    };

    int ans = (int)1e9;

    // assert(n <= 300);

    auto doit = [&]{
        for(int i = 0; i < 1; ++i){
            for(int j = i - 1; j < n; ++j){
                // cout << j << ' ' << ans << endl;
                if(j >= 0) flip(j);
                
                if(n <= 300 || j % 15000 == 0){
                    int nans = tr.tr[1];
                    while(true){
                        int now = nans;

                        for(int i = 0; i < n; ++i){
                            flip(i);

                            int prv = now;
                            now = min(now, tr.tr[1]);

                            if(now == prv) flip(i);
                        }

                        if(now == nans) break;

                        nans = now;
                    }

                    ans = min(ans, nans);
                }
            }
        }
    };
    
    // sort(a.begin(), a.end(), [&](array<int, 4>&x, array<int, 4>&y){return make_pair(x[1], x[0]) < make_pair(y[1], y[0]);});
    // doit();
    // sort(a.begin(), a.end(), [&](array<int, 4>&x, array<int, 4>&y){return make_pair(x[1], x[0] + x[1]) < make_pair(y[1], y[0] + y[1]);});
    // doit();
    // sort(a.begin(), a.end(), [&](array<int, 4>&x, array<int, 4>&y){return make_pair(x[1], x[0] - x[1]) < make_pair(y[1], y[0] - y[1]);});
    // doit();
    // sort(a.begin(), a.end(), [&](array<int, 4>&x, array<int, 4>&y){return make_pair(x[0], x[1]) < make_pair(y[0], y[1]);});
    // doit();
    // sort(a.begin(), a.end(), [&](array<int, 4>&x, array<int, 4>&y){return make_pair(x[0], x[0] + x[1]) < make_pair(y[0], y[0] + y[1]);});
    // doit();
    // sort(a.begin(), a.end(), [&](array<int, 4>&x, array<int, 4>&y){return make_pair(x[0], x[0] - x[1]) < make_pair(y[0], y[0] - y[1]);});
    // doit();
    sort(a.begin(), a.end(), [&](array<int, 4>&x, array<int, 4>&y){return x[0] + x[1] < y[0] + y[1];});
    doit();
    // sort(a.begin(), a.end(), [&](array<int, 4>&x, array<int, 4>&y){return x[0] < y[0];});
    // doit();
    // sort(a.begin(), a.end(), [&](array<int, 4>&x, array<int, 4>&y){return x[0] - x[1] < y[0] - y[1];});
    // doit();
    // sort(a.begin(), a.end(), [&](array<int, 4>&x, array<int, 4>&y){return x[1] < y[1];});
    // doit();

    cout << ans + alladd << '\n';
    
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 4181 ms 10976 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4181 ms 10976 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4181 ms 10976 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4181 ms 10976 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4181 ms 10976 KB Output isn't correct
2 Halted 0 ms 0 KB -