Submission #205857

# Submission time Handle Problem Language Result Execution time Memory
205857 2020-03-01T07:46:46 Z jh05013 None (JOI14_ho_t5) C++17
100 / 100
322 ms 14460 KB
#include <bits/stdc++.h>
#define sz(X) (int)((X).size())
#define entire(X) X.begin(),X.end()
using namespace std; typedef long long ll;
void OJize(){cin.tie(NULL);ios_base::sync_with_stdio(false);}
template <class T1, class T2>ostream&operator<<(ostream &os,pair<T1,T2>const&x){os<<'('<<x.first<<", "<<x.second<<')';return os;}
template <class Ch, class Tr, class Container>basic_ostream<Ch,Tr>&operator<<(basic_ostream<Ch,Tr>&os,Container const&x){for(auto&y:x)os<<y<<" ";return os;}

template <typename T>
struct Compress{
    int n; vector<T> arr;
    void add(T x){arr.push_back(x); n++;}
    void init(){sort(entire(arr)), 
        arr.erase(unique(entire(arr)), arr.end()), n=arr.size();}
    int lb(T x){return lower_bound(entire(arr), x) - arr.begin();}
};

struct DisjointSet{
    vector<int> par;
    DisjointSet(int n): par(n) {iota(entire(par), 0);}
    void un(int x, int y){par[fd(x)] = fd(y);} // yr becomes parent
    int fd(int x){return par[x]!=x? (par[x]=fd(par[x])):x;}
};

template <typename T>
struct Fenwick{
    int n; vector<T> arr;
    Fenwick(int N): n{N}, arr(N) {}
    void add(int i, T val){
        while(i < n) arr[i]+= val, i |= i+1;
    }
    T getsum(int i){
        T res = 0;
        while(i >= 0) res+= arr[i], i = (i&(i+1))-1;
        return res;
    }
    T intersum(int i, int j){
        if(j < i) return 0;
        return i? (getsum(j) - getsum(i-1)) : getsum(j);
    }
};

set<int> vert;
int side(int x, bool left){
    auto it = vert.lower_bound(x);
    return left? *(--it) : *(++it);
}
bool contains(int l, int r){
    return vert.lower_bound(l) != vert.upper_bound(r);
}

int main(){OJize();
    int w,h,n; cin>>w>>h>>n;
    vector<tuple<int,int,int,int,int>> ev; // y, type, l, r, idx
    Compress<int> C;
    C.add(0), C.add(w);
    // type 1 = vertical start (uses l=r)
    // type 2 = horizontal (uses l, r)
    // type 3 = vertical end (uses l=r)
    for(int i=0; i<n; i++){
        int xa,ya,xb,yb; cin>>xa>>ya>>xb>>yb;
        C.add(xa), C.add(xb);
        if(xa == xb){
            ev.push_back({ya, 1, xa, xb, i});
            ev.push_back({yb, 3, xa, xb, i});
        }
        else ev.push_back({ya, 2, xa, xb, i});
    }
    ev.push_back({0,2,0,w,n}), ev.push_back({h,2,0,w,n+1});
    ev.push_back({0,1,0,0,n+2}), ev.push_back({0,1,w,w,n+3});
    ev.push_back({h,3,0,0,n+2}), ev.push_back({h,3,w,w,n+3});
    C.init(), sort(entire(ev));
    Fenwick<int> F(sz(C.arr));

    // count V and E
    ll V=0, E=0, COMP=0;
    for(auto [y,ty,x1,x2,_]: ev){
        x1 = C.lb(x1), x2 = C.lb(x2);
        if(ty == 1) F.add(x1, 1), V++;
        else if(ty == 2){
            ll ic = F.intersum(x1, x2);
            V+= ic+2, E+= 2*ic+1;
        }
        else if(ty == 3) F.add(x1, -1), V++, E++;
    }

    // count COMP
    DisjointSet DS(n+4);
    COMP = 0;
    map<int, pair<int,int>> area; // {s, {e, idx}}
    area[-2] = {-1, -1}, area[w+1] = {w+2, -1};
    for(auto [y,ty,x1,x2,idx]: ev){
        x1 = C.lb(x1), x2 = C.lb(x2);
//cout<<area<<endl;
        if(ty == 1){
//cout<<"ADD "<<x1<<endl;
            vert.insert(x1);
            auto it = area.upper_bound(x1); it--;
            int IL = it->first; auto [IR, CID] = it->second;
            if(IL <= x1 && x1 <= IR){
                area.erase(it);
                if(IL < x1) area[IL] = {side(x1,1), CID};
                if(x1 < IR) area[side(x1,0)] = {IR, CID};
            }
            area[x1] = {x1, idx};
        }
        else if(ty == 2){
            if(!contains(x1, x2)) continue;
            auto it = area.upper_bound(x1); it--;
            if(it->second.first < x1) it++;
            int IL = it->first; auto [IR, CID] = it->second;
            if(IL <= x1 && x2 <= IR){
//if(contains(x1, x2)) cout<<idx<<' '<<CID<<endl;
                if(contains(x1, x2)) DS.un(idx, CID);
                continue;
            }
            vector<int> to_erase;
            for(; it->first <= x2; it++){
                to_erase.push_back(it->first);
                IR = it->second.first;
//cout<<idx<<' '<<(it->second.second)<<endl;
                DS.un(idx, it->second.second);
            }
            for(int x: to_erase) area.erase(x);
            area[to_erase[0]] = {IR, idx};
        }
        else if(ty == 3){
//cout<<"REM "<<x1<<endl;
            auto it = area.upper_bound(x1); it--;
            if(it->first == it->second.first) area.erase(it);
            else if(it->first == x1){
                area[side(x1,0)] = it->second;
                area.erase(x1);
            }
            else if(it->second.first == x1)
                it->second = {side(x1,1), it->second.second};
            vert.erase(x1);
        }
    }
    for(int i=0; i<sz(DS.par); i++) COMP+= DS.fd(i) == i;

//cout<<V<<' '<<E<<' '<<COMP<<endl;
    cout << E-V+COMP;
}

Compilation message

2014_ho_t5.cpp: In function 'int main()':
2014_ho_t5.cpp:77:27: warning: unused variable 'y' [-Wunused-variable]
     for(auto [y,ty,x1,x2,_]: ev){
                           ^
2014_ho_t5.cpp:77:27: warning: unused variable '_' [-Wunused-variable]
2014_ho_t5.cpp:92:29: warning: unused variable 'y' [-Wunused-variable]
     for(auto [y,ty,x1,x2,idx]: ev){
                             ^
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 380 KB Output is correct
7 Correct 6 ms 376 KB Output is correct
8 Correct 7 ms 380 KB Output is correct
9 Correct 6 ms 376 KB Output is correct
10 Correct 7 ms 504 KB Output is correct
11 Correct 6 ms 376 KB Output is correct
12 Correct 6 ms 376 KB Output is correct
13 Correct 7 ms 504 KB Output is correct
14 Correct 9 ms 376 KB Output is correct
15 Correct 6 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 5 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 380 KB Output is correct
7 Correct 6 ms 376 KB Output is correct
8 Correct 7 ms 380 KB Output is correct
9 Correct 6 ms 376 KB Output is correct
10 Correct 7 ms 504 KB Output is correct
11 Correct 6 ms 376 KB Output is correct
12 Correct 6 ms 376 KB Output is correct
13 Correct 7 ms 504 KB Output is correct
14 Correct 9 ms 376 KB Output is correct
15 Correct 6 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 5 ms 376 KB Output is correct
18 Correct 5 ms 376 KB Output is correct
19 Correct 5 ms 376 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
21 Correct 7 ms 504 KB Output is correct
22 Correct 6 ms 376 KB Output is correct
23 Correct 7 ms 376 KB Output is correct
24 Correct 7 ms 376 KB Output is correct
25 Correct 7 ms 504 KB Output is correct
26 Correct 6 ms 376 KB Output is correct
27 Correct 7 ms 504 KB Output is correct
28 Correct 7 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 6 ms 376 KB Output is correct
3 Correct 6 ms 376 KB Output is correct
4 Correct 7 ms 376 KB Output is correct
5 Correct 30 ms 1236 KB Output is correct
6 Correct 135 ms 5644 KB Output is correct
7 Correct 265 ms 10756 KB Output is correct
8 Correct 273 ms 10752 KB Output is correct
9 Correct 217 ms 10756 KB Output is correct
10 Correct 192 ms 10756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 7 ms 376 KB Output is correct
3 Correct 29 ms 1364 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 7 ms 376 KB Output is correct
6 Correct 305 ms 10876 KB Output is correct
7 Correct 20 ms 1752 KB Output is correct
8 Correct 174 ms 14460 KB Output is correct
9 Correct 183 ms 13944 KB Output is correct
10 Correct 183 ms 13816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 376 KB Output is correct
2 Correct 5 ms 376 KB Output is correct
3 Correct 5 ms 376 KB Output is correct
4 Correct 5 ms 376 KB Output is correct
5 Correct 5 ms 376 KB Output is correct
6 Correct 5 ms 380 KB Output is correct
7 Correct 6 ms 376 KB Output is correct
8 Correct 7 ms 380 KB Output is correct
9 Correct 6 ms 376 KB Output is correct
10 Correct 7 ms 504 KB Output is correct
11 Correct 6 ms 376 KB Output is correct
12 Correct 6 ms 376 KB Output is correct
13 Correct 7 ms 504 KB Output is correct
14 Correct 9 ms 376 KB Output is correct
15 Correct 6 ms 376 KB Output is correct
16 Correct 5 ms 376 KB Output is correct
17 Correct 5 ms 376 KB Output is correct
18 Correct 5 ms 376 KB Output is correct
19 Correct 5 ms 376 KB Output is correct
20 Correct 5 ms 376 KB Output is correct
21 Correct 7 ms 504 KB Output is correct
22 Correct 6 ms 376 KB Output is correct
23 Correct 7 ms 376 KB Output is correct
24 Correct 7 ms 376 KB Output is correct
25 Correct 7 ms 504 KB Output is correct
26 Correct 6 ms 376 KB Output is correct
27 Correct 7 ms 504 KB Output is correct
28 Correct 7 ms 376 KB Output is correct
29 Correct 6 ms 376 KB Output is correct
30 Correct 6 ms 376 KB Output is correct
31 Correct 6 ms 376 KB Output is correct
32 Correct 7 ms 376 KB Output is correct
33 Correct 30 ms 1236 KB Output is correct
34 Correct 135 ms 5644 KB Output is correct
35 Correct 265 ms 10756 KB Output is correct
36 Correct 273 ms 10752 KB Output is correct
37 Correct 217 ms 10756 KB Output is correct
38 Correct 192 ms 10756 KB Output is correct
39 Correct 5 ms 376 KB Output is correct
40 Correct 7 ms 376 KB Output is correct
41 Correct 29 ms 1364 KB Output is correct
42 Correct 5 ms 376 KB Output is correct
43 Correct 7 ms 376 KB Output is correct
44 Correct 305 ms 10876 KB Output is correct
45 Correct 20 ms 1752 KB Output is correct
46 Correct 174 ms 14460 KB Output is correct
47 Correct 183 ms 13944 KB Output is correct
48 Correct 183 ms 13816 KB Output is correct
49 Correct 7 ms 376 KB Output is correct
50 Correct 7 ms 504 KB Output is correct
51 Correct 6 ms 376 KB Output is correct
52 Correct 152 ms 5768 KB Output is correct
53 Correct 117 ms 5644 KB Output is correct
54 Correct 150 ms 5892 KB Output is correct
55 Correct 313 ms 10756 KB Output is correct
56 Correct 261 ms 10760 KB Output is correct
57 Correct 315 ms 11392 KB Output is correct
58 Correct 316 ms 10756 KB Output is correct
59 Correct 185 ms 13468 KB Output is correct
60 Correct 187 ms 13948 KB Output is correct
61 Correct 214 ms 13820 KB Output is correct
62 Correct 232 ms 13944 KB Output is correct
63 Correct 322 ms 11004 KB Output is correct
64 Correct 209 ms 10756 KB Output is correct
65 Correct 315 ms 11128 KB Output is correct
66 Correct 316 ms 10752 KB Output is correct
67 Correct 306 ms 10756 KB Output is correct
68 Correct 298 ms 10756 KB Output is correct
69 Correct 288 ms 10768 KB Output is correct
70 Correct 299 ms 10756 KB Output is correct
71 Correct 314 ms 11772 KB Output is correct
72 Correct 113 ms 6660 KB Output is correct