Submission #55204

# Submission time Handle Problem Language Result Execution time Memory
55204 2018-07-06T10:29:24 Z 노영훈(#1526) None (JOI14_ho_t5) C++11
30 / 100
3000 ms 111632 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MX=100010, inf=2e9, XMX=200010;

int w, h, n;

struct line {
    int x1, y1, x2, y2;
};
vector<line> L;

struct node {
    ll sum; int l, r;
} tree[XMX*20];
int now=0;

int root0[XMX];
int rootx[XMX];

int init(int v, int s, int e, int y, int val){
    if(y<s || e<y) return v;
    node &nd=tree[++now];
    nd=tree[v]; v=now;
    nd.sum+=val;
    if(s!=e){
        nd.l=init(nd.l, s, (s+e)/2, y, val);
        nd.r=init(nd.r, (s+e)/2+1, e, y, val);
    }
    return v;
}


ll sum(int v, int s, int e, int l, int r){
    if(e<l || r<s) return 0;
    node &nd=tree[v]; // 2-1
    if(l<=s && e<=r) return nd.sum;
    return sum(nd.l, s, (s+e)/2, l, r) + sum(nd.r, (s+e)/2+1, e, l, r);
}


ll vcnt=0, ecnt=0;


bool hit(line a, line b){
    if(a.y1==a.y2) swap(a,b);
    if(a.y1==a.y2) return false;
    if(b.x1==b.x2) return false;
    return (b.x1<=a.x1 && a.x1<=b.x2) && (a.y1<=b.y1 && b.y1<=a.y2);
}

int U[MX];

int find(int x){ return x==U[x] ? x : U[x]=find(U[x]); }
void unite(int x, int y){
    if(find(x)==find(y)) return;
    U[U[y]]=U[x];
}


map<int, int> Seg[4*XMX];

void put(int v, int s, int e, int y, int val){
    if(y<s || e<y) return;
    // cout<<"PUT: "<<y<<' '<<val<<'\n';
    Seg[v][val]++;
    if(s!=e){
        put(v*2,s,(s+e)/2,y,val);
        put(v*2+1,(s+e)/2+1,e,y,val);
    }
}
void pop(int v, int s, int e, int y, int val){
    if(y<s || e<y) return;
    // cout<<"POP: "<<y<<' '<<val<<'\n';
    Seg[v][val]--;
    if(Seg[v][val]==0) Seg[v].erase(val);
    if(s!=e){
        pop(v*2,s,(s+e)/2,y,val);
        pop(v*2+1,(s+e)/2+1,e,y,val);
    }
}

void get(int v, int s, int e, int l, int r, set<int> &res){
    if(e<l || r<s) return;
    if(l<=s && e<=r){
        for(pii p:Seg[v]) res.insert(p.first);
        return;
    }
    if(s!=e){
        get(v*2,s,(s+e)/2,l,r,res);
        get(v*2+1,(s+e)/2+1,e,l,r,res);
    }
}


int components(){
    int sz=L.size();
    for(int i=0; i<sz; i++) U[i]=i;
    vector<int> H[XMX];
    vector<int> V;
    for(int i=0; i<sz; i++){
        line &l=L[i];
        if(l.x1==l.x2){
            V.push_back(i);
            continue;
        }
        H[l.x1].push_back(i+1);
        H[l.x2+1].push_back(-i-1);
    }
    sort(V.begin(), V.end(), [](int a, int b){
        return L[a].x1<L[b].x1;
    });
    int x=0;
    for(int i:V){
        line &l=L[i];
        while(x<l.x1){
            x++;
            for(int idx:H[x]){
                if(idx>0) put(1,1,h,L[idx-1].y1,idx-1);
                else pop(1,1,h,L[-idx-1].y1,-idx-1);
            }
        }
        set<int> now;
        get(1,1,h,l.y1, l.y2, now);
        // cout<<l.x1<<' '<<l.y1<<' '<<l.x2<<' '<<l.y2<<": \n";
        for(int x:now) unite(i, x);
        // for(int x:now) cout<<L[x].x1<<' '<<L[x].y1<<' '<<L[x].x2<<' '<<L[x].y2<<'\n';
    }
    set<int> cnt;
    for(int i=0; i<sz; i++) cnt.insert(find(i));
    // cout<<cnt.size()<<'\n';
    return cnt.size();
}



vector<pii> P;
vector<line> H[XMX];

int fenwick[XMX];
void upt(int y, int val){
    for(; 0<y; y-=y&(-y)) fenwick[y]+=val;
}
void upt(int l, int r, int val){
    upt(r,val); upt(l-1,-val);
}
int val(int y){
    int res=0;
    for(; y<=w; y+=y&(-y)) res+=fenwick[y];
    return res;
}
void normalize(){
    // cout<<"NORM\n";
    for(line &l:L){
        if(l.y1==l.y2){
            P.push_back({l.x1,l.y1});
            P.push_back({l.x2,l.y2});
        }
        else{
            H[l.x1].push_back(l);
        }
    }
    sort(P.begin(), P.end());
    for(int i=0, x=0; i<(int)P.size(); i++){
        if(x!=P[i].first){
            for(line &l:H[x]){
                upt(l.y1, l.y2, -1);
            }
            x=P[i].first;
            for(line &l:H[x]){
                upt(l.y1, l.y2, 1);
            }
        }
        int now=val(P[i].second);
        if(now==0) vcnt++;
        if(now!=0) ecnt--;
    }
    // cout<<ecnt<<' '<<vcnt<<'\n';
}

void solve(){
    // cout<<"SOLVE!\n";
    for(line &l:L){
        if(l.y1==l.y2) continue;
        int rt=rootx[l.x1];
        int hit=sum(rt, 1, h, l.y1, l.y2);
        ecnt+=hit;
        int nowv=hit+2-sum(rt, 1, h, l.y1, l.y1)-sum(rt, 1, h, l.y2, l.y2);
        vcnt+=nowv;
        ecnt+=nowv-1;
        // cout<<l.x1<<' '<<l.y1<<" ~ "<<l.y2<<": "<<hit<<'\n';
        // cout<<ecnt<<' '<<vcnt<<'\n';
    }
    // cout<<ecnt<<' '<<vcnt<<'\n';
}

map<int, int> diff[XMX];
void put_horz(){
    for(line &l:L){
        if(l.x1==l.x2) continue;
        ecnt++;
        diff[l.x1][l.y1]++;
        diff[l.x2+1][l.y2]--;
    }
    int idx=0;
    for(int x=1; x<=w; x++){
        for(pii p:diff[x]){
            idx++;
            root0[idx]=init(root0[idx-1], 1, h, p.first, p.second);
        }
        rootx[x]=root0[idx];
    }
    // for(int i=1; i<=w; i++) cout<<rootx[i]<<' ';
    // cout<<'\n';
    // cout<<ecnt<<' '<<vcnt<<'\n';
}


int find(int x, vector<int> &X){
    return lower_bound(X.begin(), X.end(), x)-X.begin()+1;
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0);
    cin>>w>>h>>n;

    vector<int> X, Y;
    for(int i=1; i<=n; i++){
        int x1, y1, x2, y2; cin>>x1>>y1>>x2>>y2;
        L.push_back({x1,y1,x2,y2});
        X.push_back(x1); X.push_back(x2);
        Y.push_back(y1); Y.push_back(y2);
    }
    L.push_back({0,0,w,0}); L.push_back({0,h,w,h});
    L.push_back({0,0,0,h}); L.push_back({w,0,w,h});
    X.push_back(0); X.push_back(w);
    Y.push_back(0); Y.push_back(h);

    sort(X.begin(), X.end());
    X.resize(unique(X.begin(), X.end())-X.begin());
    sort(Y.begin(), Y.end());
    Y.resize(unique(Y.begin(), Y.end())-Y.begin());


    w=find(w,X); h=find(h,Y);
    for(line &l:L){
        l={find(l.x1,X), find(l.y1,Y), find(l.x2,X), find(l.y2,Y)};
    }

    put_horz();
    solve();
    normalize();
    ll ans=ecnt-vcnt+components();
    cout<<ans;

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 44 ms 56696 KB Output is correct
2 Correct 45 ms 56840 KB Output is correct
3 Correct 43 ms 56840 KB Output is correct
4 Correct 45 ms 56876 KB Output is correct
5 Correct 45 ms 56880 KB Output is correct
6 Correct 52 ms 56880 KB Output is correct
7 Correct 53 ms 57036 KB Output is correct
8 Correct 63 ms 57420 KB Output is correct
9 Correct 60 ms 57420 KB Output is correct
10 Correct 70 ms 57420 KB Output is correct
11 Correct 84 ms 57512 KB Output is correct
12 Correct 55 ms 57512 KB Output is correct
13 Correct 75 ms 57512 KB Output is correct
14 Correct 57 ms 57512 KB Output is correct
15 Correct 61 ms 57708 KB Output is correct
16 Correct 44 ms 57708 KB Output is correct
17 Correct 52 ms 57708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 56696 KB Output is correct
2 Correct 45 ms 56840 KB Output is correct
3 Correct 43 ms 56840 KB Output is correct
4 Correct 45 ms 56876 KB Output is correct
5 Correct 45 ms 56880 KB Output is correct
6 Correct 52 ms 56880 KB Output is correct
7 Correct 53 ms 57036 KB Output is correct
8 Correct 63 ms 57420 KB Output is correct
9 Correct 60 ms 57420 KB Output is correct
10 Correct 70 ms 57420 KB Output is correct
11 Correct 84 ms 57512 KB Output is correct
12 Correct 55 ms 57512 KB Output is correct
13 Correct 75 ms 57512 KB Output is correct
14 Correct 57 ms 57512 KB Output is correct
15 Correct 61 ms 57708 KB Output is correct
16 Correct 44 ms 57708 KB Output is correct
17 Correct 52 ms 57708 KB Output is correct
18 Correct 50 ms 57708 KB Output is correct
19 Correct 54 ms 57708 KB Output is correct
20 Correct 49 ms 57708 KB Output is correct
21 Correct 54 ms 57708 KB Output is correct
22 Correct 50 ms 57708 KB Output is correct
23 Correct 62 ms 57708 KB Output is correct
24 Correct 74 ms 57708 KB Output is correct
25 Correct 57 ms 57708 KB Output is correct
26 Correct 55 ms 57708 KB Output is correct
27 Correct 78 ms 57712 KB Output is correct
28 Correct 71 ms 57712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 57712 KB Output is correct
2 Correct 55 ms 57712 KB Output is correct
3 Correct 53 ms 57712 KB Output is correct
4 Correct 66 ms 57712 KB Output is correct
5 Correct 105 ms 61060 KB Output is correct
6 Correct 406 ms 78472 KB Output is correct
7 Correct 876 ms 102464 KB Output is correct
8 Correct 854 ms 102464 KB Output is correct
9 Correct 834 ms 104712 KB Output is correct
10 Correct 772 ms 104712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 104712 KB Output is correct
2 Correct 73 ms 104712 KB Output is correct
3 Correct 2382 ms 104712 KB Output is correct
4 Correct 44 ms 104712 KB Output is correct
5 Correct 71 ms 104712 KB Output is correct
6 Execution timed out 3044 ms 111632 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 44 ms 56696 KB Output is correct
2 Correct 45 ms 56840 KB Output is correct
3 Correct 43 ms 56840 KB Output is correct
4 Correct 45 ms 56876 KB Output is correct
5 Correct 45 ms 56880 KB Output is correct
6 Correct 52 ms 56880 KB Output is correct
7 Correct 53 ms 57036 KB Output is correct
8 Correct 63 ms 57420 KB Output is correct
9 Correct 60 ms 57420 KB Output is correct
10 Correct 70 ms 57420 KB Output is correct
11 Correct 84 ms 57512 KB Output is correct
12 Correct 55 ms 57512 KB Output is correct
13 Correct 75 ms 57512 KB Output is correct
14 Correct 57 ms 57512 KB Output is correct
15 Correct 61 ms 57708 KB Output is correct
16 Correct 44 ms 57708 KB Output is correct
17 Correct 52 ms 57708 KB Output is correct
18 Correct 50 ms 57708 KB Output is correct
19 Correct 54 ms 57708 KB Output is correct
20 Correct 49 ms 57708 KB Output is correct
21 Correct 54 ms 57708 KB Output is correct
22 Correct 50 ms 57708 KB Output is correct
23 Correct 62 ms 57708 KB Output is correct
24 Correct 74 ms 57708 KB Output is correct
25 Correct 57 ms 57708 KB Output is correct
26 Correct 55 ms 57708 KB Output is correct
27 Correct 78 ms 57712 KB Output is correct
28 Correct 71 ms 57712 KB Output is correct
29 Correct 64 ms 57712 KB Output is correct
30 Correct 55 ms 57712 KB Output is correct
31 Correct 53 ms 57712 KB Output is correct
32 Correct 66 ms 57712 KB Output is correct
33 Correct 105 ms 61060 KB Output is correct
34 Correct 406 ms 78472 KB Output is correct
35 Correct 876 ms 102464 KB Output is correct
36 Correct 854 ms 102464 KB Output is correct
37 Correct 834 ms 104712 KB Output is correct
38 Correct 772 ms 104712 KB Output is correct
39 Correct 51 ms 104712 KB Output is correct
40 Correct 73 ms 104712 KB Output is correct
41 Correct 2382 ms 104712 KB Output is correct
42 Correct 44 ms 104712 KB Output is correct
43 Correct 71 ms 104712 KB Output is correct
44 Execution timed out 3044 ms 111632 KB Time limit exceeded
45 Halted 0 ms 0 KB -