Submission #1113218

# Submission time Handle Problem Language Result Execution time Memory
1113218 2024-11-16T04:13:08 Z Zero_OP None (JOI14_ho_t5) C++14
100 / 100
974 ms 77232 KB
#include <bits/stdc++.h>

using namespace std;

struct fenwick_tree{
    static const int offset = 1;
    vector<int> bit;
    fenwick_tree(int n) : bit(n + 1, 0) {}

    void update(int id, int val){
        id += offset;
        for(; id < (int)bit.size(); id += id & (-id)) bit[id] += val;
    }

    int query(int id){
        int sum = 0;
        id += offset;
        for(; id > 0; id -= id & (-id)) sum += bit[id];
        return sum;
    }

    void update(int l, int r, int val){
        update(l, val);
        update(r + 1, -val);
    }

    int query(int l, int r){
        int cur = query(r) - query(l - 1);
        return cur;
    }
};

struct disjoint_set{
    vector<int> lab;
    int components;
    disjoint_set(int n) : components(n), lab(n, -1) {}

    int root(int u){
        return lab[u] < 0 ? u : (lab[u] = root(lab[u]));
    }

    bool unite(int u, int v){
        u = root(u); v = root(v);
        if(u == v) return false;
        if(lab[u] > lab[v]) swap(u, v);
        lab[u] += lab[v];
        lab[v] = u;
        --components;
        return true;
    }

    int n_component(){
        return components;
    }
};

struct segment_tree_dsu{
    using info = pair<int, int>;
    vector<set<info>> st;

    segment_tree_dsu(int n) : st(n << 2) {}

    void insert(int id, int l, int r, int pos, info v){
        st[id].insert(v);
        if(l < r){
            int mid = l + r >> 1;
            if(pos <= mid) insert(id << 1, l, mid, pos, v);
            else insert(id << 1 | 1, mid + 1, r, pos, v);
        }
    }

    void erase(int id, int l, int r, int pos, info v){
        auto it = st[id].find(v);
        if(it != st[id].end()) st[id].erase(it);
        if(l < r){
            int mid = l + r >> 1;
            if(pos <= mid) erase(id << 1, l, mid, pos, v);
            else erase(id << 1 | 1, mid + 1, r, pos, v);
        }
    }

    void join_segments(int id, int l, int r, int u, int v, int match, disjoint_set& dsu){
        if(u <= l && r <= v){
            while((int)st[id].size() > 1){
                int p = st[id].begin() -> second;
                st[id].erase(st[id].begin());
                dsu.unite(match, p);
            }

            if(!st[id].empty()) dsu.unite(match, st[id].begin() -> second);
        } else{
            int mid = l + r >> 1;
            if(u <= mid) join_segments(id << 1, l, mid, u, v, match, dsu);
            if(mid < v) join_segments(id << 1 | 1, mid + 1, r, u, v, match, dsu);
        }
    }
};

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

    if(fopen("task.inp", "r")){
        freopen("task.inp", "r", stdin);
        freopen("task.out", "w", stdout);
    }

    int W, H, N;
    cin >> W >> H >> N;

    vector<tuple<int, int, int, int>> cuts;
    vector<int> x, y;
    for(int i = 0; i < N; ++i){
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        cuts.emplace_back(a, b, c, d);
    }

    cuts.emplace_back(0, 0, W, 0);
    cuts.emplace_back(0, 0, 0, H);
    cuts.emplace_back(W, 0, W, H);
    cuts.emplace_back(0, H, W, H);

    sort(cuts.begin(), cuts.end());
    cuts.erase(unique(cuts.begin(), cuts.end()), cuts.end());

    for(auto [a, b, c, d] : cuts){
        x.emplace_back(a);
        x.emplace_back(c);
        y.emplace_back(b);
        y.emplace_back(d);
    }

    sort(x.begin(), x.end());
    x.erase(unique(x.begin(), x.end()), x.end());

    sort(y.begin(), y.end());
    y.erase(unique(y.begin(), y.end()), y.end());

    int nx = (int)x.size(), ny = (int)y.size();
    vector<vector<pair<int, int>>> horizontal(ny), vertical(nx);

    for(auto& [a, b, c, d] : cuts){
        a = lower_bound(x.begin(), x.end(), a) - x.begin();
        c = lower_bound(x.begin(), x.end(), c) - x.begin();
        b = lower_bound(y.begin(), y.end(), b) - y.begin();
        d = lower_bound(y.begin(), y.end(), d) - y.begin();

        if(a == c){
            vertical[a].emplace_back(b, d);
        } else{
            assert(b == d);
            horizontal[b].emplace_back(a, c);
        }
    }

    struct event{
        int t, type, l, r, id;
        bool operator < (const event& o){
            return make_pair(t, type) < make_pair(o.t, o.type);
        }
    };

    vector<event> events;

    N = 0;
    for(int i = 0; i < nx; ++i){
        auto& vec = vertical[i];
        for(int j = 0; j < (int)vec.size(); ++j){
            int l, r; tie(l, r) = vec[j];
            while(j + 1 < (int)vec.size() && vec[j + 1].first <= r){
                r = max(r, vec[j + 1].second);
                ++j;
            }

            events.push_back({i, 1, l, r, N++});
        }
    }

    for(int i = 0; i < ny; ++i){
        auto& vec = horizontal[i];
        for(int j = 0; j < (int)vec.size(); ++j){
            int l, r; tie(l, r) = vec[j];
            while(j + 1 < (int)vec.size() && vec[j + 1].first <= r){
                r = max(r, vec[j + 1].second);
                ++j;
            }

            events.push_back({l, 0, i, r, N});
            events.push_back({r, 2, i, r, N});
            ++N;
        }
    }

    sort(events.begin(), events.end());
    long long n_intersection = 0;

    fenwick_tree ft(ny);
    disjoint_set dsu(N);
    segment_tree_dsu segtree(ny);

    for(auto [t, type, l, r, id] : events){
        if(type == 0){
            segtree.insert(1, 0, ny - 1, l, make_pair(r, id));
            ft.update(l, +1);
        } else if(type == 1){
            segtree.join_segments(1, 0, ny - 1, l, r, id, dsu);
            n_intersection += ft.query(l, r);
        } else{
            segtree.erase(1, 0, ny - 1, l, make_pair(r, id));
            ft.update(l, -1);
        }
    }

    long long V = n_intersection;
    long long E = N;
    long long C = dsu.n_component();

    cout << V - E + C << '\n';

    return 0;
}

Compilation message

2014_ho_t5.cpp: In constructor 'disjoint_set::disjoint_set(int)':
2014_ho_t5.cpp:35:9: warning: 'disjoint_set::components' will be initialized after [-Wreorder]
   35 |     int components;
      |         ^~~~~~~~~~
2014_ho_t5.cpp:34:17: warning:   'std::vector<int> disjoint_set::lab' [-Wreorder]
   34 |     vector<int> lab;
      |                 ^~~
2014_ho_t5.cpp:36:5: warning:   when initialized here [-Wreorder]
   36 |     disjoint_set(int n) : components(n), lab(n, -1) {}
      |     ^~~~~~~~~~~~
2014_ho_t5.cpp: In member function 'void segment_tree_dsu::insert(int, int, int, int, segment_tree_dsu::info)':
2014_ho_t5.cpp:66:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   66 |             int mid = l + r >> 1;
      |                       ~~^~~
2014_ho_t5.cpp: In member function 'void segment_tree_dsu::erase(int, int, int, int, segment_tree_dsu::info)':
2014_ho_t5.cpp:76:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   76 |             int mid = l + r >> 1;
      |                       ~~^~~
2014_ho_t5.cpp: In member function 'void segment_tree_dsu::join_segments(int, int, int, int, int, int, disjoint_set&)':
2014_ho_t5.cpp:92:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   92 |             int mid = l + r >> 1;
      |                       ~~^~~
2014_ho_t5.cpp: In function 'int main()':
2014_ho_t5.cpp:127:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  127 |     for(auto [a, b, c, d] : cuts){
      |              ^
2014_ho_t5.cpp:143:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  143 |     for(auto& [a, b, c, d] : cuts){
      |               ^
2014_ho_t5.cpp:202:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  202 |     for(auto [t, type, l, r, id] : events){
      |              ^
2014_ho_t5.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  104 |         freopen("task.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
2014_ho_t5.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |         freopen("task.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 3 ms 848 KB Output is correct
9 Correct 2 ms 592 KB Output is correct
10 Correct 2 ms 848 KB Output is correct
11 Correct 2 ms 848 KB Output is correct
12 Correct 1 ms 592 KB Output is correct
13 Correct 2 ms 848 KB Output is correct
14 Correct 1 ms 592 KB Output is correct
15 Correct 3 ms 848 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 3 ms 848 KB Output is correct
9 Correct 2 ms 592 KB Output is correct
10 Correct 2 ms 848 KB Output is correct
11 Correct 2 ms 848 KB Output is correct
12 Correct 1 ms 592 KB Output is correct
13 Correct 2 ms 848 KB Output is correct
14 Correct 1 ms 592 KB Output is correct
15 Correct 3 ms 848 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 3 ms 848 KB Output is correct
22 Correct 2 ms 948 KB Output is correct
23 Correct 3 ms 848 KB Output is correct
24 Correct 3 ms 1104 KB Output is correct
25 Correct 2 ms 848 KB Output is correct
26 Correct 2 ms 848 KB Output is correct
27 Correct 3 ms 1104 KB Output is correct
28 Correct 3 ms 848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 760 KB Output is correct
2 Correct 2 ms 592 KB Output is correct
3 Correct 2 ms 592 KB Output is correct
4 Correct 3 ms 848 KB Output is correct
5 Correct 24 ms 5644 KB Output is correct
6 Correct 143 ms 25404 KB Output is correct
7 Correct 279 ms 50008 KB Output is correct
8 Correct 284 ms 49980 KB Output is correct
9 Correct 231 ms 50184 KB Output is correct
10 Correct 206 ms 49848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 2 ms 848 KB Output is correct
3 Correct 26 ms 6420 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 3 ms 848 KB Output is correct
6 Correct 374 ms 61408 KB Output is correct
7 Correct 19 ms 5904 KB Output is correct
8 Correct 239 ms 64044 KB Output is correct
9 Correct 314 ms 63660 KB Output is correct
10 Correct 321 ms 63664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 2 ms 592 KB Output is correct
8 Correct 3 ms 848 KB Output is correct
9 Correct 2 ms 592 KB Output is correct
10 Correct 2 ms 848 KB Output is correct
11 Correct 2 ms 848 KB Output is correct
12 Correct 1 ms 592 KB Output is correct
13 Correct 2 ms 848 KB Output is correct
14 Correct 1 ms 592 KB Output is correct
15 Correct 3 ms 848 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 3 ms 848 KB Output is correct
22 Correct 2 ms 948 KB Output is correct
23 Correct 3 ms 848 KB Output is correct
24 Correct 3 ms 1104 KB Output is correct
25 Correct 2 ms 848 KB Output is correct
26 Correct 2 ms 848 KB Output is correct
27 Correct 3 ms 1104 KB Output is correct
28 Correct 3 ms 848 KB Output is correct
29 Correct 2 ms 760 KB Output is correct
30 Correct 2 ms 592 KB Output is correct
31 Correct 2 ms 592 KB Output is correct
32 Correct 3 ms 848 KB Output is correct
33 Correct 24 ms 5644 KB Output is correct
34 Correct 143 ms 25404 KB Output is correct
35 Correct 279 ms 50008 KB Output is correct
36 Correct 284 ms 49980 KB Output is correct
37 Correct 231 ms 50184 KB Output is correct
38 Correct 206 ms 49848 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 2 ms 848 KB Output is correct
41 Correct 26 ms 6420 KB Output is correct
42 Correct 1 ms 336 KB Output is correct
43 Correct 3 ms 848 KB Output is correct
44 Correct 374 ms 61408 KB Output is correct
45 Correct 19 ms 5904 KB Output is correct
46 Correct 239 ms 64044 KB Output is correct
47 Correct 314 ms 63660 KB Output is correct
48 Correct 321 ms 63664 KB Output is correct
49 Correct 2 ms 848 KB Output is correct
50 Correct 2 ms 516 KB Output is correct
51 Correct 3 ms 848 KB Output is correct
52 Correct 168 ms 30292 KB Output is correct
53 Correct 115 ms 25152 KB Output is correct
54 Correct 202 ms 38284 KB Output is correct
55 Correct 403 ms 59804 KB Output is correct
56 Correct 296 ms 49880 KB Output is correct
57 Correct 501 ms 77232 KB Output is correct
58 Correct 362 ms 59724 KB Output is correct
59 Correct 311 ms 63152 KB Output is correct
60 Correct 353 ms 63676 KB Output is correct
61 Correct 400 ms 64220 KB Output is correct
62 Correct 432 ms 64780 KB Output is correct
63 Correct 428 ms 61756 KB Output is correct
64 Correct 226 ms 49848 KB Output is correct
65 Correct 471 ms 73392 KB Output is correct
66 Correct 422 ms 68528 KB Output is correct
67 Correct 305 ms 56752 KB Output is correct
68 Correct 289 ms 54960 KB Output is correct
69 Correct 369 ms 73976 KB Output is correct
70 Correct 374 ms 74164 KB Output is correct
71 Correct 91 ms 15292 KB Output is correct
72 Correct 974 ms 75124 KB Output is correct