Submission #6070754


Source Code Expand

Copy
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pl = pair<ll, ll>;
int n;
int chk[1<<17];
struct dj{
    vector<int> root, siz;

    void init(int n)
    {
        root.resize(n);
        siz.resize(n);
        for(int i=0;i<n;i++)
            root[i] = i, siz[i] = 1;
    }

    int find(int x)
    {
        return root[x] = (root[x] == x ? x : find(root[x]));
    }

    int merge(int x, int y)
    {
        x = find(x);
        y = find(y);
        if(x == y) return 0;
        siz[x] += siz[y];
        root[y] = x;
        return 1;
    }

    ll get_size(int x)
    {
        return siz[find(x)];
    }
};
dj djx, djy;
vector<int> vx, vy, cx, cy;
vector<int> mx[1<<17], my[1<<17];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n;
    for(int i=0;i<n;i++)
    {
        int x, y;
        cin >> x >> y;
        vx.push_back(x), vy.push_back(y);
    }
    cx = vx, cy = vy;
    sort(cx.begin(), cx.end());
    cx.resize(unique(cx.begin(), cx.end()) - cx.begin());
    sort(cy.begin(), cy.end());
    cy.resize(unique(cy.begin(), cy.end()) - cy.begin());
    for(int i=0;i<n;i++)
    {
        vx[i] = lower_bound(cx.begin(), cx.end(), vx[i]) - cx.begin();
        vy[i] = lower_bound(cy.begin(), cy.end(), vy[i]) - cy.begin();
        mx[vx[i]].push_back(i);
        my[vy[i]].push_back(i);
    }

    djx.init(n);
    djy.init(n);
    ll ans = 0;
    for(int i=0;i<n;i++)
    {
        ll cnt = 0;
        if(mx[vx[i]].size() > 1 && my[vy[i]].size() > 1 && !chk[i])
        {
            queue<int> q;
            for(int j : mx[vx[i]])
            {
                if(!chk[j])
                {
                    q.push(j);
                    chk[j] = 1;
                    cnt++;
                }
            }
            for(int j : my[vy[i]])
            {
                if(!chk[j])
                {
                    q.push(j);
                    chk[j] = 1;
                    cnt++;
                }
            }
            while(!q.empty())
            {
                int j = q.front();
                q.pop();
                if(djx.root[vx[i]] != djx.root[vx[j]])
                {
                    djx.merge(vx[i], vx[j]);
                    for(int k : mx[vx[j]])
                    {
                        if(!chk[k])
                        {
                            q.push(k);
                            chk[k] = 1;
                            cnt++;
                        }
                    }
                }
                if(djy.root[vy[i]] != djy.root[vy[j]])
                {
                    djy.merge(vy[i], vy[j]);
                    for(int k : my[vy[j]])
                    {
                        if(!chk[k])
                        {
                            q.push(k);
                            chk[k] = 1;
                            cnt++;
                        }
                    }
                }
            }
            ans += djx.get_size(vx[i]) * djy.get_size(vy[i]) - cnt;
        }
    }
    cout << ans;
}

Submission Info

Submission Time
Task F - Must Be Rectangular!
User evenharder
Language C++14 (GCC 5.4.1)
Score 600
Code Size 3202 Byte
Status AC
Exec Time 84 ms
Memory 13044 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 20
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt AC 3 ms 6400 KB
02.txt AC 3 ms 6400 KB
03.txt AC 3 ms 6400 KB
04.txt AC 3 ms 6400 KB
05.txt AC 3 ms 6400 KB
06.txt AC 4 ms 6528 KB
07.txt AC 4 ms 6528 KB
08.txt AC 4 ms 6528 KB
09.txt AC 84 ms 13044 KB
10.txt AC 82 ms 13044 KB
11.txt AC 81 ms 13044 KB
12.txt AC 81 ms 13044 KB
13.txt AC 81 ms 13044 KB
14.txt AC 81 ms 13044 KB
15.txt AC 83 ms 13044 KB
16.txt AC 82 ms 13044 KB
17.txt AC 42 ms 11252 KB
s1.txt AC 3 ms 6400 KB
s2.txt AC 3 ms 6400 KB
s3.txt AC 3 ms 6400 KB