Submission #1195961

#TimeUsernameProblemLanguageResultExecution timeMemory
1195961InvMOD절취선 (JOI14_ho_t5)C++17
100 / 100
184 ms12888 KiB
#include<bits/stdc++.h>

using namespace std;

#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())

#define dbg(x) "[" #x " = " << (x) << "]"

using ll = long long;

struct Query{
    int p, op, l, r;
    Query() {}
    Query(int p, int op, int l, int r): p(p), op(op), l(l), r(r) {}
    inline bool operator < (const Query& q) const{
        return make_tuple(p, op, l) < make_tuple(q.p, q.op, q.l);
    }
};

const int N = 1e5 + 10;

int n, W, H, cut[N][4], id[N << 1]; ll ans;

vector<Query> Q; vector<int> comp; set<int> line;

namespace tr{
    int st[N << 3];

    void down(int id){
        if(st[id]){
            st[id << 1] |= st[id];
            st[id << 1|1] |= st[id];
            st[id] = 0;
        }
    }

    inline void update(int id, int l, int r, int u, int v){
        if(l >= u && r <= v){
            st[id] = 1;
        }
        else{
            int m = l+r>>1; down(id);
            if(u <= m) update(id << 1, l, m, u, v);
            if(v > m) update(id << 1|1, m+1, r, u, v);
        }
    }

    void updPoint(int x, int val){
        int id = 1, l = 0, r = W;
        for(; l < r ;){
            int m = l+r>>1; down(id);
            if(x <= m)
                id = id << 1, r = m;
            else id = id << 1|1, l = m+1;
        }
        st[id] = val;
    }

    int query(int x){
        int id = 1, l = 0, r = W;
        for(; l < r ;){
            int m = l+r>>1; down(id);
            if(x <= m)
                id = id << 1, r = m;
            else id = id << 1|1, l = m+1;
        }
        assert(l == x);
        return st[id];
    }

    void update(int l, int r){
        update(1, 0, W, l, r);
    }
}

namespace bit{
    int bit[N << 1];

    void update(int p, int val){
        for(; p <= W; p += p & -p){
            bit[p] += val;
        }
    }

    int get(int p){
        int ans = 0;
        for(; p > 0; p -= p & -p){
            ans += bit[p];
        }
        return ans;
    }

    int query(int l, int r){
        return get(min(W, r)) - get(l - 1);
    }
}

namespace dsu{
    vector<int> par;

    int asc(int x){
        return par[x] < 0 ? x : par[x] = asc(par[x]);
    }

    void join(int u, int v){
        u = asc(u), v = asc(v);
        if(u != v){
            --ans;
            if(par[u] > par[v]) swap(u, v);
            par[u] += par[v], par[v] = u;
        }
    }

    void upd(int p){
        if(tr::query(p)){
            id[p] = sz(par);
            par.push_back(-1);
            tr::updPoint(p, 0);
        }
    }
}

void Main()
{
    cin >> W >> H >> n;

    for(int i = 1; i <= n; i++){
        for(int j = 0; j < 4; j++){
            cin >> cut[i][j];
        }
        if(cut[i][0] > cut[i][2]) swap(cut[i][0], cut[i][2]);
        if(cut[i][1] > cut[i][3]) swap(cut[i][1], cut[i][3]);
    }

    auto def = [&](int p, int x1, int y1, int x2, int y2) -> void{
        cut[p][0] = x1, cut[p][1] = y1, cut[p][2] = x2, cut[p][3] = y2;
    };

    def(n + 1, 0, 0, W, 0);
    def(n + 2, 0, 0, 0, H);
    def(n + 3, W, 0, W, H);
    def(n + 4, 0, H, W, H);
    n += 4;

    for(int i = 1; i <= n; i++){
        comp.push_back(cut[i][0]);
        comp.push_back(cut[i][2]);
    }
    sort(all(comp)), compact(comp); W = sz(comp);

    for(int i = 1; i <= n; i++){
        cut[i][0] = lower_bound(all(comp), cut[i][0]) - comp.begin() + 1;
        cut[i][2] = lower_bound(all(comp), cut[i][2]) - comp.begin() + 1;
        assert(cut[i][0] <= cut[i][2]);

        if(cut[i][0] == cut[i][2]){
            // create a line and end a line at [cut[i][1], cut[i][3]]
            Q.emplace_back(cut[i][1]  , +1, cut[i][0], -1);
            Q.emplace_back(cut[i][3]+1, -1, cut[i][0], -1);
        }
        else{
            // create a horizontal line (renew element in range [cut[i][0], cut[i][2]])
            Q.emplace_back(cut[i][1]  , +2, cut[i][0], cut[i][2]);
        }
    }
    sort(all(Q));

    tr::updPoint(0, 1); line.insert(0); dsu::upd(0);

    for(Query& cur_q : Q){
        if(cur_q.op == +1){
            int l = cur_q.l, pl = *(--line.lower_bound(l));

            dsu::upd(pl), dsu::upd(l);
            id[l] = id[pl];

            line.insert(l);
            bit::update(l, +1);
        }
        else if(cur_q.op == +2){
            int l = cur_q.l, r = cur_q.r;

            ans = ans + 1ll * (max(0, bit::query(l, r) - 1));
            tr::update(*line.lower_bound(l), *--line.upper_bound(r) - 1);
        }
        else if(cur_q.op == -1){
            int l = cur_q.l, pl = *(--line.lower_bound(l));

            dsu::upd(pl), dsu::upd(l);
            dsu::join(id[pl], id[l]);

            line.erase(l);
            bit::update(l, -1);
        }
    }

    cout << ans << "\n";
}

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

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP", "r", stdin);
        freopen(name".OUT", "w", stdout);
    }

    int t = 1; while(t--) Main();
    return 0;
}

Compilation message (stderr)

2014_ho_t5.cpp: In function 'int32_t main()':
2014_ho_t5.cpp:209:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  209 |         freopen(name".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
2014_ho_t5.cpp:210:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  210 |         freopen(name".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...