QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#189996 | #4423. AMPPZ in the times of disease | berarchegas# | TL | 0ms | 0kb | C++20 | 13.7kb | 2023-09-28 04:58:41 | 2023-09-28 04:58:41 |
answer
#include <bits/stdc++.h>
#include <iostream>
// #define int long long
#define ld long double
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define Unique(v) sort(all(v)),v.erase(unique(all(v)),v.end());
#define sz(v) ((int)v.size())
using namespace std;
typedef vector<int> vi;
#define y1 abacaba
//#define left oooooopss
#define db(x) cerr << #x <<" == "<<x << endl;
#define db2(x,y) cerr<<#x <<" == "<<x<<", "<<#y<<" == "<<y<<endl;
#define db3(x,y,z) cerr << #x<<" == "<<x<<", "<<#y<<" == "<<y<<", "<<#z<<" == "<<z<<endl;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;
// std::mt19937_64 rng64((int) std::chrono::steady_clock::now().time_since_epoch().count());
std::mt19937 rng(
// (int) std::chrono::steady_clock::now().time_since_epoch().count()
1239010
);
ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
//inline ll mod(ll n, ll m ){ ll ret = n%m; if(ret < 0) ret += m; return ret; }
ll gcd(ll a, ll b){return (b == 0LL ? a : gcd(b, a%b));}
ll exp(ll b,ll e,ll m){
b%=m;
ll ans = 1;
for (; e; b = b * b % m, e /= 2)
if (e & 1) ans = ans * b % m;
return ans;
}
// debug:
template<class T>void print_vector(vector<T> &v){
rep(i,0,sz(v))cout<<v[i]<<" \n"[i==sz(v)-1];
}
void dbg_out() {cerr << endl; }
template<typename Head, typename ... Tail> void dbg_out(Head H,Tail... T){
cerr << ' ' << H;
dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__), cerr << endl
#else
#define dbg(...) 42
#endif
//
//BEGIN DELAUNAY
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
#define sq(x) ((x)*(ll)(x))
struct pt { // ponto
int x, y;
pt(int x_ = 0, int y_ = 0) : x(x_), y(y_) {}
bool operator < (const pt p) const {
if (x != p.x) return x < p.x;
return y < p.y;
}
bool operator == (const pt p) const {
return x == p.x and y == p.y;
}
pt operator + (const pt p) const { return pt(x+p.x, y+p.y); }
pt operator - (const pt p) const { return pt(x-p.x, y-p.y); }
pt operator * (const int c) const { return pt(x*c, y*c); }
ll operator * (const pt p) const { return x*(ll)p.x + y*(ll)p.y; }
ll operator ^ (const pt p) const { return x*(ll)p.y - y*(ll)p.x; }
friend istream& operator >> (istream& in, pt& p) {
return in >> p.x >> p.y;
}
};
struct line { // reta
pt p, q;
line() {}
line(pt p_, pt q_) : p(p_), q(q_) {}
friend istream& operator >> (istream& in, line& r) {
return in >> r.p >> r.q;
}
};
// PONTO & VETOR
ll dist2(pt p, pt q) { // quadrado da distancia
return sq(p.x - q.x) + sq(p.y - q.y);
}
ll sarea2(pt p, pt q, pt r) { // 2 * area com sinal
return (q-p)^(r-q);
}
bool col(pt p, pt q, pt r) { // se p, q e r sao colin.
return sarea2(p, q, r) == 0;
}
bool ccw(pt p, pt q, pt r) { // se p, q, r sao ccw
return sarea2(p, q, r) > 0;
}
int quad(pt p) { // quadrante de um ponto
return (p.x<0)^3*(p.y<0);
}
bool compare_angle(pt p, pt q) { // retorna se ang(p) < ang(q)
if (quad(p) != quad(q)) return quad(p) < quad(q);
return ccw(q, pt(0, 0), p);
}
pt rotate90(pt p) { // rotaciona 90 graus
return pt(-p.y, p.x);
}
// RETA
bool isinseg(pt p, line r) { // se p pertence ao seg de r
pt a = r.p - p, b = r.q - p;
return (a ^ b) == 0 and (a * b) <= 0;
}
bool interseg(line r, line s) { // se o seg de r intersecta o seg de s
if (isinseg(r.p, s) or isinseg(r.q, s)
or isinseg(s.p, r) or isinseg(s.q, r)) return 1;
return ccw(r.p, r.q, s.p) != ccw(r.p, r.q, s.q) and
ccw(s.p, s.q, r.p) != ccw(s.p, s.q, r.q);
}
int segpoints(line r) { // numero de pontos inteiros no segmento
return 1 + __gcd(abs(r.p.x - r.q.x), abs(r.p.y - r.q.y));
}
double get_t(pt v, line r) { // retorna t tal que t*v pertence a reta r
return (r.p^r.q) / (double) ((r.p-r.q)^v);
}
// POLIGONO
// quadrado da distancia entre os retangulos a e b (lados paralelos aos eixos)
// assume que ta representado (inferior esquerdo, superior direito)
ll dist2_rect(pair<pt, pt> a, pair<pt, pt> b) {
int hor = 0, vert = 0;
if (a.second.x < b.first.x) hor = b.first.x - a.second.x;
else if (b.second.x < a.first.x) hor = a.first.x - b.second.x;
if (a.second.y < b.first.y) vert = b.first.y - a.second.y;
else if (b.second.y < a.first.y) vert = a.first.y - b.second.y;
return sq(hor) + sq(vert);
}
ll polarea2(vector<pt> v) { // 2 * area do poligono
ll ret = 0;
for (int i = 0; i < v.size(); i++)
ret += sarea2(pt(0, 0), v[i], v[(i + 1) % v.size()]);
return abs(ret);
}
// se o ponto ta dentro do poligono: retorna 0 se ta fora,
// 1 se ta no interior e 2 se ta na borda
int inpol(vector<pt>& v, pt p) { // O(n)
int qt = 0;
for (int i = 0; i < v.size(); i++) {
if (p == v[i]) return 2;
int j = (i+1)%v.size();
if (p.y == v[i].y and p.y == v[j].y) {
if ((v[i]-p)*(v[j]-p) <= 0) return 2;
continue;
}
bool baixo = v[i].y < p.y;
if (baixo == (v[j].y < p.y)) continue;
auto t = (p-v[i])^(v[j]-v[i]);
if (!t) return 2;
if (baixo == (t > 0)) qt += baixo ? 1 : -1;
}
return qt != 0;
}
vector<pt> convex_hull(vector<pt> v) { // convex hull - O(n log(n))
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
if (v.size() <= 1) return v;
vector<pt> l, u;
for (int i = 0; i < v.size(); i++) {
while (l.size() > 1 and !ccw(l.end()[-2], l.end()[-1], v[i]))
l.pop_back();
l.push_back(v[i]);
}
for (int i = v.size() - 1; i >= 0; i--) {
while (u.size() > 1 and !ccw(u.end()[-2], u.end()[-1], v[i]))
u.pop_back();
u.push_back(v[i]);
}
l.pop_back(); u.pop_back();
for (pt i : u) l.push_back(i);
return l;
}
ll interior_points(vector<pt> v) { // pontos inteiros dentro de um poligono simples
ll b = 0;
for (int i = 0; i < v.size(); i++)
b += segpoints(line(v[i], v[(i+1)%v.size()])) - 1;
return (polarea2(v) - b) / 2 + 1;
}
struct convex_pol {
vector<pt> pol;
// nao pode ter ponto colinear no convex hull
convex_pol() {}
convex_pol(vector<pt> v) : pol(convex_hull(v)) {}
// se o ponto ta dentro do hull - O(log(n))
bool is_inside(pt p) {
if (pol.size() == 0) return false;
if (pol.size() == 1) return p == pol[0];
int l = 1, r = pol.size();
while (l < r) {
int m = (l+r)/2;
if (ccw(p, pol[0], pol[m])) l = m+1;
else r = m;
}
if (l == 1) return isinseg(p, line(pol[0], pol[1]));
if (l == pol.size()) return false;
return !ccw(p, pol[l], pol[l-1]);
}
// ponto extremo em relacao a cmp(p, q) = p mais extremo q
// (copiado de https://github.com/gustavoM32/caderno-zika)
int extreme(const function<bool(pt, pt)>& cmp) {
int n = pol.size();
auto extr = [&](int i, bool& cur_dir) {
cur_dir = cmp(pol[(i+1)%n], pol[i]);
return !cur_dir and !cmp(pol[(i+n-1)%n], pol[i]);
};
bool last_dir, cur_dir;
if (extr(0, last_dir)) return 0;
int l = 0, r = n;
while (l+1 < r) {
int m = (l+r)/2;
if (extr(m, cur_dir)) return m;
bool rel_dir = cmp(pol[m], pol[l]);
if ((!last_dir and cur_dir) or
(last_dir == cur_dir and rel_dir == cur_dir)) {
l = m;
last_dir = cur_dir;
} else r = m;
}
return l;
}
int max_dot(pt v) {
return extreme([&](pt p, pt q) { return p*v > q*v; });
}
pair<int, int> tangents(pt p) {
auto L = [&](pt q, pt r) { return ccw(p, r, q); };
auto R = [&](pt q, pt r) { return ccw(p, q, r); };
return {extreme(L), extreme(R)};
}
};
bool operator <(const line& a, const line& b) { // comparador pra reta
// assume que as retas tem p < q
pt v1 = a.q - a.p, v2 = b.q - b.p;
bool b1 = compare_angle(v1, v2), b2 = compare_angle(v2, v1);
if (b1 or b2) return b1;
return ccw(a.p, a.q, b.p); // mesmo angulo
}
bool operator ==(const line& a, const line& b) {
return !(a < b) and !(b < a);
}
// comparador pro set pra fazer sweep line com segmentos
struct cmp_sweepline {
bool operator () (const line& a, const line& b) const {
// assume que os segmentos tem p < q
if (a.p == b.p) return ccw(a.p, a.q, b.q);
if (a.p.x != a.q.x and (b.p.x == b.q.x or a.p.x < b.p.x))
return ccw(a.p, a.q, b.p);
return ccw(a.p, b.q, b.p);
}
};
// comparador pro set pra fazer sweep angle com segmentos
pt dir;
struct cmp_sweepangle {
bool operator () (const line& a, const line& b) const {
return get_t(dir, a) < get_t(dir, b);
}
};
typedef struct QuadEdge* Q;
struct QuadEdge {
int id;
pt o;
Q rot, nxt;
bool used;
QuadEdge(int id_ = -1, pt o_ = pt(INF, INF)) :
id(id_), o(o_), rot(nullptr), nxt(nullptr), used(false) {}
Q rev() const { return rot->rot; }
Q next() const { return nxt; }
Q prev() const { return rot->next()->rot; }
pt dest() const { return rev()->o; }
};
Q edge(pt from, pt to, int id_from, int id_to) {
Q e1 = new QuadEdge(id_from, from);
Q e2 = new QuadEdge(id_to, to);
Q e3 = new QuadEdge;
Q e4 = new QuadEdge;
tie(e1->rot, e2->rot, e3->rot, e4->rot) = {e3, e4, e2, e1};
tie(e1->nxt, e2->nxt, e3->nxt, e4->nxt) = {e1, e2, e4, e3};
return e1;
}
void splice(Q a, Q b) {
swap(a->nxt->rot->nxt, b->nxt->rot->nxt);
swap(a->nxt, b->nxt);
}
void del_edge(Q& e, Q ne) { // delete e and assign e <- ne
splice(e, e->prev());
splice(e->rev(), e->rev()->prev());
delete e->rev()->rot, delete e->rev();
delete e->rot; delete e;
e = ne;
}
Q conn(Q a, Q b) {
Q e = edge(a->dest(), b->o, a->rev()->id, b->id);
splice(e, a->rev()->prev());
splice(e->rev(), b);
return e;
}
bool in_c(pt a, pt b, pt c, pt p) { // p ta na circunf. (a, b, c) ?
__int128 p2 = p*p, A = a*a - p2, B = b*b - p2, C = c*c - p2;
return sarea2(p, a, b) * C + sarea2(p, b, c) * A + sarea2(p, c, a) * B > 0;
}
pair<Q, Q> build_tr(vector<pt>& p, int l, int r) {
if (r-l+1 <= 3) {
Q a = edge(p[l], p[l+1], l, l+1), b = edge(p[l+1], p[r], l+1, r);
if (r-l+1 == 2) return {a, a->rev()};
splice(a->rev(), b);
ll ar = sarea2(p[l], p[l+1], p[r]);
Q c = ar ? conn(b, a) : 0;
if (ar >= 0) return {a, b->rev()};
return {c->rev(), c};
}
int m = (l+r)/2;
auto [la, ra] = build_tr(p, l, m);
auto [lb, rb] = build_tr(p, m+1, r);
while (true) {
if (ccw(lb->o, ra->o, ra->dest())) ra = ra->rev()->prev();
else if (ccw(lb->o, ra->o, lb->dest())) lb = lb->rev()->next();
else break;
}
Q b = conn(lb->rev(), ra);
auto valid = [&](Q e) { return ccw(e->dest(), b->dest(), b->o); };
if (ra->o == la->o) la = b->rev();
if (lb->o == rb->o) rb = b;
while (true) {
Q L = b->rev()->next();
if (valid(L)) while (in_c(b->dest(), b->o, L->dest(), L->next()->dest()))
del_edge(L, L->next());
Q R = b->prev();
if (valid(R)) while (in_c(b->dest(), b->o, R->dest(), R->prev()->dest()))
del_edge(R, R->prev());
if (!valid(L) and !valid(R)) break;
if (!valid(L) or (valid(R) and in_c(L->dest(), L->o, R->o, R->dest())))
b = conn(R, b->rev());
else b = conn(b->rev(), L->rev());
}
return {la, rb};
}
vector<vector<int>> delaunay(vector<pt> v) {
int n = v.size();
auto tmp = v;
vector<int> idx(n);
iota(idx.begin(), idx.end(), 0);
sort(idx.begin(), idx.end(), [&](int l, int r) { return v[l] < v[r]; });
for (int i = 0; i < n; i++) v[i] = tmp[idx[i]];
assert(unique(v.begin(), v.end()) == v.end());
vector<vector<int>> g(n);
bool col = true;
for (int i = 2; i < n; i++) if (sarea2(v[i], v[i-1], v[i-2])) col = false;
if (col) {
for (int i = 1; i < n; i++)
g[idx[i-1]].push_back(idx[i]), g[idx[i]].push_back(idx[i-1]);
return g;
}
Q e = build_tr(v, 0, n-1).first;
vector<Q> edg = {e};
for (int i = 0; i < edg.size(); e = edg[i++]) {
for (Q at = e; !at->used; at = at->next()) {
at->used = true;
g[idx[at->id]].push_back(idx[at->rev()->id]);
edg.push_back(at->rev());
}
}
return g;
}
// END DELAUNAY
const int N = 1e6 + 10;
int a[N];
int pre[N][2];
int suf[N][2];
struct edg{
int a, b;
ll cost;
};
struct dsu{
vi p , ps;
dsu(int n){
p = vi(n+1),ps = vi(n+1,1);
rep(i,0,n+1) p[i] = i;
}
int find(int x){return p[x]==x?x:p[x]=find(p[x]);}
bool join(int x , int y){
x = find(x) , y = find(y);
if(x == y) return 0;
if(ps[x] > ps[y]) swap(x,y);
p[x] = y, ps[y] += ps[x];
return 1;
}
};
void solve(){
int n , k;
cin >> n >> k;
map<pair<ll,ll> , int> id;
vector<pt> v;
for(int i = 0 ; i < n; i ++){
pair<ll,ll> a;
cin >> a.first >> a.second;
id[a] = i;
v.push_back(pt(a.first,a.second));
}
vector<edg> e;
auto p = delaunay(v);
for(int i = 0 ; i < n; i ++){
for(auto w : p[i])
e.push_back({i,w,dist2(v[i] , v[w])});
}
sort(all(e) , [&](edg a , edg b){
return a.cost < b.cost;
});
dsu T(n);
vector<edg> mst;
for(auto [a,b,c] : e){
if(T.join(a,b)){
mst.push_back({a,b,c});
}
}
while(k > 1){
mst.pop_back();
k--;
}
dsu TT(n);
map<int,int> group;
for(auto [a,b,c] : mst){
TT.join(a,b);
}
int xp = 0;
for(int i = 0 ; i < n; i ++){
if(!group.count(TT.find(i)))
group[TT.find(i)] = ++xp;
cout << group[TT.find(i)] << " ";
}
cout << endl;
}
int32_t main(){
fastio;
int t;
cin >> t;
while(t--){
solve();
}
// math -> gcd it all
// Did you swap N,M? N=1?
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
100 100000 20 270505 510725 245104 76414 131477 992924 781607 895592 562469 622976 564354 637966 980036 112090 522903 687218 113871 977855 6615 123673 13847 347618 657794 165707 420561 183995 11367 136391 507836 694877 985069 105115 774110 486921 14319 338715 774937 118145 981468 99089 803866 491315...
output:
1 2 3 4 5 5 6 7 3 8 9 10 11 8 7 6 12 9 13 6 12 10 14 4 15 1 11 16 14 5 2 10 17 15 16 4 13 11 4 17 17 9 2 1 5 14 1 18 12 18 14 18 10 14 14 19 8 1 8 14 14 13 19 9 4 11 13 16 15 13 17 10 16 15 8 10 13 12 19 20 4 6 3 12 3 9 11 15 18 12 2 5 5 14 14 4 12 15 6 14 12 1 8 1 8 14 8 11 13 12 15 17 14 15 7 18 5...