Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //나는 가상 소녀들에게 큰 호감이 있습니다.
- #include <iostream>
- #include <cmath>
- #include <algorithm>
- #include <stdio.h>
- #include <cstring>
- #include <string>
- #include <cstdlib>
- #include <vector>
- #include <bitset>
- #include <map>
- #include <chrono>
- #include <functional>
- #include <unordered_set>
- #include <unordered_map>
- #include <numeric>
- #include <queue>
- #include <ctime>
- #include <stack>
- #include <set>
- #include <list>
- #include <deque>
- #include <iomanip>
- #include <sstream>
- #include <fstream>
- #include <stdarg.h>
- #include <utility>
- using namespace std;
- #define pb push_back
- #define mp make_pair
- #define ll long long
- #define ull unisgned long long
- #define ld long double
- #define all(a) a.begin(), a.end()
- #define SORT(a) sort(all(a))
- #define pii pair<int, int>
- #define tii triple<int, int, int>
- #define e 1e-7
- #define PI acos(-1)
- #define sz(a) (int)(a.size())
- #define inf 1e17
- #define vi vector<int>
- #define F first
- #define S second
- #define rng(x) for(int _ = 0; _ < (x); _++)
- #define rngi(i, x) for(int i = 0; i < (x); i++)
- #define rngsi(s, i, x) for(int i = (s); i <(x); i++)
- #define int long long
- #define problem "a"
- template<typename A, typename B, typename C>
- struct triple {
- A X;
- B Y;
- C Z;
- triple(A a = 0, B b = 0, C c = 0) :X(a), Y(b), Z(c) {}
- };
- template<typename A, typename B, typename C>
- triple<A, B, C> make_triple(A a = 0, B b = 0, C c = 0) {
- return triple<A, B, C>(a, b, c);
- }
- template<typename A, typename B, typename C>
- bool operator<(const triple<A, B, C>& a, const triple<A, B, C>& b) {
- if (a.X != b.X)
- return a.X < b.X;
- if (a.Y != b.Y)
- return a.Y < b.Y;
- return a.Z < b.Z;
- }
- template<typename T, typename SS>
- ostream& operator<<(ostream& ofs, const pair<T, SS>& p) {
- ofs << "( " << p.F << " , " << p.S << " )";
- return ofs;
- }
- template<typename T>
- void print(T a) {
- for (auto i : a)
- cout << i << ' ';
- cout << '\n';
- }
- template<typename T>
- T max(T a, T b, T c) {
- return max(a, max(b, c));
- }
- template<typename T>
- T min(T a, T b, T c) {
- return min(a, min(b, c));
- }
- template<typename T, typename D>
- D min(T a) {
- return *min_element(all(a));
- }
- template<typename T, typename D>
- D max(T a) {
- return *max_element(all(a));
- }
- struct custom_hash {
- static uint64_t splitmix64(uint64_t x) {
- x += 0x9e3779b97f4a7c15;
- x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
- x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
- return x ^ (x >> 31);
- }
- size_t operator()(uint64_t x) const {
- static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
- return splitmix64(x + FIXED_RANDOM);
- }
- };
- void solve() {
- int n; cin >> n;
- vector<pii> a(n);
- rngi(i, n) cin >> a[i].first >> a[i].second;
- int ans = 4;
- rng(2) {
- vi xx, yy;
- rngi(i, n) xx.push_back(a[i].first), yy.push_back(a[i].second);
- SORT(xx); xx.erase(unique(all(xx)), xx.end());
- SORT(yy); yy.erase(unique(all(yy)), yy.end());
- vector<vi> mx(n), my(n);
- vector<pii> b;
- rngi(i, n) {
- int x = a[i].first, y = a[i].second;
- x = lower_bound(all(xx), x) - xx.begin();
- y = lower_bound(all(yy), y) - yy.begin();
- b.push_back({ x,y });
- mx[x].push_back(y);
- }
- rngi(i, n) SORT(mx[i]), SORT(my[i]);
- int lim = sqrt(n) + 2;
- vector<pii> c;
- rngi(i, n)
- if (sz(mx[i]) < lim)for (auto v : mx[i]) c.push_back({ i, v });
- for (auto p : c) my[p.second].push_back(p.first);
- rngi(i, n) SORT(my[i]);
- vi cnt(n);
- rngi(i, n) {
- for (auto x : my[i]) for (auto d : mx[x]) if (d > i) {
- cnt[d]++; if (cnt[d] == 2)return void(cout << "0\n");
- }
- for (auto x : my[i]) for (auto d : mx[x]) if (d > i) {
- cnt[d]--;
- }
- }
- vector<vi> ptr(n);
- cnt.assign(n, 0);
- rngi(i, n) if (sz(mx[i]) >= lim) for (auto d : mx[i]) ptr[d].push_back(i);
- rngi(i, n) {
- if (sz(mx[i]) < lim) for (auto d : mx[i]) for (auto t : ptr[d]) { cnt[t]++; if (cnt[d] == 2)return void(cout << "0\n"); }
- if (sz(mx[i]) < lim) for (auto d : mx[i]) for (auto t : ptr[d]) { cnt[t]--; }
- }
- rngi(i, n) swap(a[i].first, a[i].second);
- }
- vi xx, yy;
- rngi(i, n) xx.push_back(a[i].first), yy.push_back(a[i].second);
- SORT(xx); xx.erase(unique(all(xx)), xx.end());
- SORT(yy); yy.erase(unique(all(yy)), yy.end());
- vi cx(n), cy(n);
- vector<pii> b;
- vector< vi > mx(n), my(n);
- rngi(i, n) {
- int x = a[i].first, y = a[i].second;
- x = lower_bound(all(xx), x) - xx.begin();
- y = lower_bound(all(yy), y) - yy.begin();
- b.push_back({ x,y });
- cx[x]++; cy[y]++;
- mx[x].push_back(y);
- my[y].push_back(x);
- }
- vi d(n);
- rngi(i, n) if (cx[i] > 1) for (auto v : mx[i]) {
- d[v]++; if (d[v] == 2) return void(cout << "1\n");
- }
- d.assign(n, 0);
- rngi(i, n) if (cy[i] > 1) for (auto v : my[i]) {
- d[v]++; if (d[v] == 2) return void(cout << "1\n");
- }
- int aa = 0;
- rngi(i, n) if (cx[i] > 1) aa++;
- if(aa > 1)return void(cout << "2\n");
- aa = 0;
- rngi(i, n) if (cy[i] > 1) aa++;
- if (aa > 1)return void(cout << "2\n");
- rngi(i, n) if (cx[b[i].first] > 1 && cy[b[i].second] > 1)return void(cout << "2\n");
- rngi(i, n) if((cx[b[i].first] > 1 || cy[b[i].second] > 1) && (cx[b[i].first] != n && cy[b[i].second] != n))return void(cout << "3\n");
- cout << 4 << '\n';
- };
- void working() {
- int n; cin >> n;
- vector<pii> a(n);
- rngi(i, n) cin >> a[i].first >> a[i].second;
- int ans = 4;
- rng(2) {
- vi xx, yy;
- rngi(i, n) xx.push_back(a[i].first), yy.push_back(a[i].second);
- SORT(xx); xx.erase(unique(all(xx)), xx.end());
- SORT(yy); yy.erase(unique(all(yy)), yy.end());
- vector<vector<char> > have(n, vector<char>(n));
- vector<pii> b;
- rngi(i, n) {
- int x = a[i].first, y = a[i].second;
- x = lower_bound(all(xx), x) - xx.begin();
- y = lower_bound(all(yy), y) - yy.begin();
- b.push_back({ x,y });
- have[x][y] = 1;
- }
- vi cx(n), cy(n);
- rngi(i, n) cx[b[i].first]++;
- rngi(i, n) cy[b[i].second]++;
- rngi(i, n) rngi(j, n) {
- if (!ans)break;
- int x1 = b[i].first, y1 = b[i].second, x2 = b[j].first, y2 = b[j].second;
- if (x1 < x2 && y1 < y2) {
- int d = 4;
- if (have[x1][y2]) d -= 2;
- else if (cx[x1] > 1 || cy[y2] > 1) d--;
- if (have[x2][y1]) d -= 2;
- else if (cx[x2] > 1 || cy[y1] > 1) d--;
- ans = min(ans, d);
- }
- }
- rngi(i, n) a[i].second = -a[i].second;
- }
- cout << ans << '\n';
- }
- signed main()
- {
- if (0) {
- freopen("1.in", "r", stdin);
- freopen("bad.out", "w", stdout);
- }
- srand(time(NULL));
- cin.tie(0);
- cout.tie(0);
- ios_base::sync_with_stdio(false);
- if (0) {
- cout << "50\n";
- rng(50) {
- int n = 1 + rand() % 10;
- cout << n << '\n';
- rng(n) {
- int x = rand() % 10, y = rand() % 10;
- cout << x << ' ' << y << '\n';
- }
- }
- return 0;
- }
- int t = 1; cin >> t;
- rng(t) solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement