Submission #1114112

# Submission time Handle Problem Language Result Execution time Memory
1114112 2024-11-18T08:04:15 Z Mike_Vu None (JOI14_ho_t5) C++14
100 / 100
1002 ms 79552 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double dou;
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define MASK(x) (1LL<<(x))
#define BITj(x, j) (((x)>>(j))&1)
#define y1 LNTD
#define pil pair<ll, int>

template<typename T> bool maximize(T &x, const T &y) {
    if (x < y) {x = y; return 1;}
    return 0;
}
template<typename T> bool minimize(T &x, const T &y) {
    if (x > y) {x = y; return 1;}
    return 0;
}

struct BIT{
    int n;
    vector<int> bit;
    void init(int _n = 0) {
        n = _n;
        bit.assign(n+1, 0);
    }
    void update(int k, int x) {
        while (k <= n) {
            bit[k] += x;
            k += k & (-k);
        }
    }
    int getsum(int k) {
        int res = 0;
        while (k > 0) {
            res += bit[k];
            k -= k & (-k);
        }
        return res;
    }
    int query(int l, int r) {
        return getsum(r)-getsum(l-1);
    }
};

struct DSU{
    int numcc = 0;
    vector<int> dsu;
    void init(int _n = 0) {
        numcc= _n;
        dsu.assign(numcc+1, -1);
    }
    int newnode() {
        ++numcc;
        dsu.pb(-1);
        return (int)dsu.size()-1;
    }
    int f(int u) {
        return dsu[u] < 0 ? u : dsu[u] = f(dsu[u]);
    }
    int join(int u, int v) {
        u = f(u);
        v = f(v);
        if (u == v) return 0;
        if (dsu[u] > dsu[v]) swap(u, v);
        dsu[u] += dsu[v];
        dsu[v] = u;
        --numcc;
        return u;
    }
};

struct SMT{
    DSU dsu;
    int trsz;
    vector<multiset<pil>> tr;
    int numcc() {
        return dsu.numcc;
    }
    void addnumcc() {
        ++dsu.numcc;
    }
    void init(int n = 0) {
        trsz = 1;
        dsu.newnode();
        --dsu.numcc;
        while (trsz < n) trsz <<= 1;
        tr.assign(trsz<<1, multiset<pil>());
    }
    void insval(int k, ll e) {
        int id = k+trsz-1;
        int curval = dsu.newnode();
        pil x = {e, curval};
        tr[id].insert(x);
        id >>= 1;
        while (id > 0) {
            tr[id].insert(x);
            id >>= 1;
        }
    }
    void delval(int k) {
        int id = k+trsz-1;
        pil x = (*tr[id].begin());
        tr[id].erase(tr[id].begin());
        id >>= 1;
        while (id > 0) {
            auto it = tr[id].lower_bound(x);
            if (it != tr[id].end() && (*it) == x) {
                tr[id].erase(it);
            }
            id >>= 1;
        }
    }
    void unite(int id, int &curmatch) {
//        cout << "unite " << id << "\n";
        while (tr[id].size() > 1) {
            auto it1 = tr[id].begin(), it2 = it1;
            ++it2;
            dsu.join((*it1).se, (*it2).se);
            tr[id].erase(it1);
        }
        if (tr[id].size() == 1) {
            int curid = (*tr[id].begin()).se;
            if (curmatch == 0) {
                curmatch = curid;
            }
            else dsu.join(curmatch, curid);
        }
//        cout << curmatch << "\n";
    }
    void query(int ql, int qr) {
//        cout << "query " << ql << ' ' << qr << "\n";
        int curmatch = 0;
        int l = ql+trsz-1, r = qr+trsz;
        while (l < r) {
            if (l&1) {
                unite(l++, curmatch);
            }
            if (r&1) {
                unite(--r, curmatch);
            }
            l >>= 1;
            r >>= 1;
        }
    }
//    void check() {
//        cout << "tree check " << numcc() << "\n";
//        for (int i= 1; i < trsz*2; i++) {
//            cout << "node " << i << ": ";
//            for (auto it = tr[i].begin(); it != tr[i].end(); it++) {
//                cout << (*it) << ' ';
//            }
//            cout << "\n";
//        }
//    }
};

struct event{
    int type; ///hori - vert
    int x, l;
    ll r;
    event(int _type, int _l, ll _r, int _x) {
        type = _type;
        l = _l;
        r = _r;
        x = _x;
    }
};
bool cmp1(event a, event b) {
    if (a.type != b.type) return a.type < b.type;
    if (a.x != b.x) return a.x < b.x;
    return a.l < b.l;
}
bool cmp2(event a, event b) {
    if (a.x != b.x) return a.x < b.x;
    if (a.type != b.type) return a.type > b.type;
    return a.r < b.r;
}

const int maxn = 1e5+5;
int n;
vector<event> e, cuts;
vector<int> values;
ll ans = 0;
BIT bit;
SMT tr;

int mp(int x){
    return lower_bound(values.begin(), values.end(), x)-values.begin()+1;
}

vector<event> discretize(vector<event> a){
    vector<event> af, res;
    //discretize
    sort(a.begin(), a.end(), cmp1);
    bool curtype = a[0].type;
    int curl = a[0].l, curr = a[0].r, curx = a[0].x;
    for (int i = 1; i < (int)a.size(); i++) {
        bool type = a[i].type;
        int l = a[i].l, r = a[i].r, x = a[i].x;
//        cout << type << ' ' << l << ' ' << r << ' ' << x << "\n";
        if ((type == curtype && x == curx) && l <= curr) {
            maximize(curr, r);
        }
        else {
            af.pb(event(curtype, curl, curr, curx));
            curtype = a[i].type;
            curl = a[i].l;
            curr = a[i].r;
            curx = a[i].x;
        }
    }
    af.pb(event(curtype, curl, curr, curx));
    //transform + values
    ll incval = maxn*3, sumval = 0;
    for (event cur : af) {
//        cout << cur.type << ' ' << cur.l << ' ' << cur.r << ' ' << cur.x << "\n";
        if (cur.type == 0) {
            values.pb(cur.x);
            ++sumval;
            res.pb(event(1, cur.x, (ll)cur.r*incval+sumval, cur.l));
            res.pb(event(-1, cur.x, (ll)cur.r*incval+sumval, cur.r));
        }
        else {
            values.pb(cur.l);
            values.pb(cur.r);
            res.pb(event(0, cur.l, cur.r, cur.x));
//            cout << res.back().l << "\n";
        }
    }
//    cout << "discretize completed\n";
    sort(values.begin(), values.end());
    values.erase(unique(values.begin(), values.end()), values.end());
    return res;
}

void sweepline() {
    sort(e.begin(), e.end(), cmp2);
    bit.init((int)values.size());
    tr.init((int)values.size());
    ll sumv = 0, sume = 0;
    for (event cure : e) {
        int type = cure.type, l = cure.l;
        ll r = cure.r;
//        cout << "event " << type << ' ' << cure.x << ' ' << cure.l << ' ' << cure.r << "\n";
        if (type == 0) {
            //vertical
            int val = bit.query(mp(l), mp(r));
            if (val == 0) {
                //case not intersect
                ++sumv;
                tr.addnumcc();
            }
            else {
                sume += val-1;
                tr.query(mp(l), mp(r));
            }
        }
        else if (type == 1) {
            //new
            ++sumv;
            bit.update(mp(l), 1);
            tr.insval(mp(l), r);
        }
        else {
            bit.update(mp(l), -1);
            tr.delval(mp(l));
        }
//        tr.check();
    }
    //ans = e-v+c
//    cout << sume << ' ' << sumv << ' ' << tr.numcc() << "\n";
    ans = sume-sumv+tr.numcc();
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    #define name "task"
//    if (fopen(name".inp", "r")) {
//        freopen(name".inp", "r", stdin);
//        freopen(name".out", "w", stdout);
//    }
    int w, h;
    cin >> w >> h >> n;
    cuts.pb(event(0, 0, w, 0));
    cuts.pb(event(0, 0, w, h));
    cuts.pb(event(1, 0, h, 0));
    cuts.pb(event(1, 0, h, w));
    for (int i = 1; i <= n; i++) {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        if (x1 == x2 && y1 == y2) continue;
        if (x1 == x2) cuts.pb(event(1, y1, y2, x1));
        else cuts.pb(event(0, x1, x2, y1));
    }
    e = discretize(cuts);
    //sweepline
    sweepline();
    //active SMT, - waiting - set
    cout << ans;
}
/*
10 8 12
3 2 9 2
5 3 5 6
6 3 9 3
1 7 7 7
2 3 5 3
3 1 4 1
10 3 10 8
1 1 1 7
6 5 6 5
6 3 6 5
4 2 6 2
8 2 9 2
1

6 8 8
3 6 4 6
4 6 4 6
4 7 4 7
5 3 5 3
1 1 1 3
1 7 6 7
6 3 6 7
4 2 4 7
1

6 8 5
1 1 5 1
3 4 4 4
1 1 2 1
3 5 3 7
1 7 3 7
1

15 13 7
8 8 13 8
11 3 11 12
13 4 13 12
4 11 8 11
3 5 7 5
8 3 15 3
15 6 15 11
1
*/

# 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 2 ms 348 KB Output is correct
5 Correct 1 ms 504 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 592 KB Output is correct
9 Correct 3 ms 592 KB Output is correct
10 Correct 3 ms 592 KB Output is correct
11 Correct 3 ms 848 KB Output is correct
12 Correct 2 ms 592 KB Output is correct
13 Correct 3 ms 800 KB Output is correct
14 Correct 2 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 2 ms 348 KB Output is correct
5 Correct 1 ms 504 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 592 KB Output is correct
9 Correct 3 ms 592 KB Output is correct
10 Correct 3 ms 592 KB Output is correct
11 Correct 3 ms 848 KB Output is correct
12 Correct 2 ms 592 KB Output is correct
13 Correct 3 ms 800 KB Output is correct
14 Correct 2 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 432 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 2 ms 848 KB Output is correct
22 Correct 2 ms 592 KB Output is correct
23 Correct 2 ms 848 KB Output is correct
24 Correct 3 ms 848 KB Output is correct
25 Correct 3 ms 848 KB Output is correct
26 Correct 2 ms 848 KB Output is correct
27 Correct 3 ms 848 KB Output is correct
28 Correct 3 ms 848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 592 KB Output is correct
2 Correct 2 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 3 ms 848 KB Output is correct
5 Correct 22 ms 3256 KB Output is correct
6 Correct 122 ms 19468 KB Output is correct
7 Correct 234 ms 38372 KB Output is correct
8 Correct 242 ms 38356 KB Output is correct
9 Correct 187 ms 38216 KB Output is correct
10 Correct 145 ms 38352 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 24 ms 4660 KB Output is correct
4 Correct 1 ms 504 KB Output is correct
5 Correct 3 ms 848 KB Output is correct
6 Correct 375 ms 53204 KB Output is correct
7 Correct 17 ms 6428 KB Output is correct
8 Correct 217 ms 70604 KB Output is correct
9 Correct 318 ms 69840 KB Output is correct
10 Correct 301 ms 69840 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 2 ms 348 KB Output is correct
5 Correct 1 ms 504 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 592 KB Output is correct
9 Correct 3 ms 592 KB Output is correct
10 Correct 3 ms 592 KB Output is correct
11 Correct 3 ms 848 KB Output is correct
12 Correct 2 ms 592 KB Output is correct
13 Correct 3 ms 800 KB Output is correct
14 Correct 2 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 432 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 2 ms 848 KB Output is correct
22 Correct 2 ms 592 KB Output is correct
23 Correct 2 ms 848 KB Output is correct
24 Correct 3 ms 848 KB Output is correct
25 Correct 3 ms 848 KB Output is correct
26 Correct 2 ms 848 KB Output is correct
27 Correct 3 ms 848 KB Output is correct
28 Correct 3 ms 848 KB Output is correct
29 Correct 2 ms 592 KB Output is correct
30 Correct 2 ms 592 KB Output is correct
31 Correct 1 ms 592 KB Output is correct
32 Correct 3 ms 848 KB Output is correct
33 Correct 22 ms 3256 KB Output is correct
34 Correct 122 ms 19468 KB Output is correct
35 Correct 234 ms 38372 KB Output is correct
36 Correct 242 ms 38356 KB Output is correct
37 Correct 187 ms 38216 KB Output is correct
38 Correct 145 ms 38352 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 2 ms 848 KB Output is correct
41 Correct 24 ms 4660 KB Output is correct
42 Correct 1 ms 504 KB Output is correct
43 Correct 3 ms 848 KB Output is correct
44 Correct 375 ms 53204 KB Output is correct
45 Correct 17 ms 6428 KB Output is correct
46 Correct 217 ms 70604 KB Output is correct
47 Correct 318 ms 69840 KB Output is correct
48 Correct 301 ms 69840 KB Output is correct
49 Correct 2 ms 848 KB Output is correct
50 Correct 2 ms 592 KB Output is correct
51 Correct 3 ms 848 KB Output is correct
52 Correct 154 ms 25608 KB Output is correct
53 Correct 105 ms 19464 KB Output is correct
54 Correct 193 ms 36848 KB Output is correct
55 Correct 382 ms 50896 KB Output is correct
56 Correct 249 ms 38372 KB Output is correct
57 Correct 549 ms 74848 KB Output is correct
58 Correct 443 ms 50644 KB Output is correct
59 Correct 270 ms 69212 KB Output is correct
60 Correct 295 ms 70100 KB Output is correct
61 Correct 406 ms 70608 KB Output is correct
62 Correct 433 ms 70864 KB Output is correct
63 Correct 425 ms 53968 KB Output is correct
64 Correct 184 ms 38292 KB Output is correct
65 Correct 560 ms 69584 KB Output is correct
66 Correct 495 ms 63252 KB Output is correct
67 Correct 326 ms 46952 KB Output is correct
68 Correct 281 ms 44652 KB Output is correct
69 Correct 476 ms 70868 KB Output is correct
70 Correct 449 ms 70904 KB Output is correct
71 Correct 87 ms 15424 KB Output is correct
72 Correct 1002 ms 79552 KB Output is correct