QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#190001 | #4423. AMPPZ in the times of disease | berarchegas# | TL | 0ms | 0kb | C++17 | 8.1kb | 2023-09-28 05:08:31 | 2023-09-28 05:08:31 |
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
template <class T> int sgn(T x) { return (x > 0) - (x < 0); }
template<class T>
struct Point {
typedef Point P;
T x, y;
explicit Point(T x=0, T y=0) : x(x), y(y) {}
bool operator<(P p) const { return tie(x,y) < tie(p.x,p.y); }
bool operator==(P p) const { return tie(x,y)==tie(p.x,p.y); }
P operator+(P p) const { return P(x+p.x, y+p.y); }
P operator-(P p) const { return P(x-p.x, y-p.y); }
P operator*(T d) const { return P(x*d, y*d); }
P operator/(T d) const { return P(x/d, y/d); }
T dot(P p) const { return x*p.x + y*p.y; }
T cross(P p) const { return x*p.y - y*p.x; }
T cross(P a, P b) const { return (a-*this).cross(b-*this); }
T dist2() const { return x*x + y*y; }
double dist() const { return sqrt((double)dist2()); }
// angle to x-axis in interval [-pi, pi]
double angle() const { return atan2(y, x); }
P unit() const { return *this/dist(); } // makes dist()=1
P perp() const { return P(-y, x); } // rotates +90 degrees
P normal() const { return perp().unit(); }
// returns point rotated 'a' radians ccw around the origin
P rotate(double a) const {
return P(x*cos(a)-y*sin(a),x*sin(a)+y*cos(a)); }
friend ostream& operator<<(ostream& os, P p) {
return os << "(" << p.x << "," << p.y << ")"; }
};
typedef Point<ll> P;
typedef struct Quad* Q;
typedef __int128_t lll; // (can be ll if coords are < 2e4)
P arb(LLONG_MAX,LLONG_MAX); // not equal to any other point
struct Quad {
Q rot, o; P p = arb; bool mark;
P& F() { return r()->p; }
Q& r() { return rot->rot; }
Q prev() { return rot->o->rot; }
Q next() { return r()->prev(); }
} *H;
bool circ(P p, P a, P b, P c) { // is p in the circumcircle?
lll p2 = p.dist2(), A = a.dist2()-p2,
B = b.dist2()-p2, C = c.dist2()-p2;
return p.cross(a,b)*C + p.cross(b,c)*A + p.cross(c,a)*B > 0;
}
Q makeEdge(P orig, P dest) {
Q r = H ? H : new Quad{new Quad{new Quad{new Quad{0}}}};
H = r->o; r->r()->r() = r;
rep(i,0,4) r = r->rot, r->p = arb, r->o = i & 1 ? r : r->r();
r->p = orig; r->F() = dest;
return r;
}
void splice(Q a, Q b) {
swap(a->o->rot->o, b->o->rot->o); swap(a->o, b->o);
}
Q connect(Q a, Q b) {
Q q = makeEdge(a->F(), b->p);
splice(q, a->next());
splice(q->r(), b);
return q;
}
pair<Q,Q> rec(const vector<P>& s) {
if (sz(s) <= 3) {
Q a = makeEdge(s[0], s[1]), b = makeEdge(s[1], s.back());
if (sz(s) == 2) return { a, a->r() };
splice(a->r(), b);
auto side = s[0].cross(s[1], s[2]);
Q c = side ? connect(b, a) : 0;
return {side < 0 ? c->r() : a, side < 0 ? c : b->r() };
}
#define H(e) e->F(), e->p
#define valid(e) (e->F().cross(H(base)) > 0)
Q A, B, ra, rb;
int half = sz(s) / 2;
tie(ra, A) = rec({all(s) - half});
tie(B, rb) = rec({sz(s) - half + all(s)});
while ((B->p.cross(H(A)) < 0 && (A = A->next())) ||
(A->p.cross(H(B)) > 0 && (B = B->r()->o)));
Q base = connect(B->r(), A);
if (A->p == ra->p) ra = base->r();
if (B->p == rb->p) rb = base;
#define DEL(e, init, dir) Q e = init->dir; if (valid(e)) \
while (circ(e->dir->F(), H(base), e->F())) { \
Q t = e->dir; \
splice(e, e->prev()); \
splice(e->r(), e->r()->prev()); \
e->o = H; H = e; e = t; \
}
for (;;) {
DEL(LC, base->r(), o); DEL(RC, base, prev());
if (!valid(LC) && !valid(RC)) break;
if (!valid(LC) || (valid(RC) && circ(H(RC), H(LC))))
base = connect(RC, base->r());
else
base = connect(base->r(), LC->r());
}
return { ra, rb };
}
vector<P> triangulate(vector<P> pts) {
sort(all(pts)); assert(unique(all(pts)) == pts.end());
if (sz(pts) < 2) return {};
Q e = rec(pts).first;
vector<Q> q = {e};
int qi = 0;
while (e->o->F().cross(e->F(), e->p) < 0) e = e->o;
#define ADD { Q c = e; do { c->mark = 1; pts.push_back(c->p); \
q.push_back(c->r()); c = c->next(); } while (c != e); }
ADD; pts.clear();
while (qi < sz(q)) if (!(e = q[qi++])->mark) ADD;
return pts;
}
// END DELAUNAY
const int N = 1e6 + 10;
int a[N];
int pre[N][2];
int suf[N][2];
struct edge{
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<P> v;
for(int i = 0 ; i < n; i ++){
pair<ll,ll> a;
cin >> a.first >> a.second;
id[a] = i;
v.push_back(P(a.first,a.second));
}
vector<P> triangles = triangulate(v);
vector<edge> e;
for(int i = 0 ; i < sz(triangles) ; i += 3){
auto a = triangles[i%sz(triangles)] , b = triangles[(i+1)%sz(triangles)] , c = triangles[(i+2)%sz(triangles)];
int id_a = id[{a.x , a.y}] , id_b = id[{b.x , b.y}] , id_c = id[{c.x , c.y}];
e.push_back({id_a, id_b, (a - b).dist2()});
e.push_back({id_b , id_c , (b-c).dist2()});
e.push_back({id_a , id_c , (a-c).dist2()});
}
sort(all(v) , [&](P a , P b){
return a.x < b.x;
});
if(sz(triangles) == 0){
for(int i = 1 ; i < n; i ++){
int id_a = id[{v[i-1].x , v[i-1].y}] , id_b = id[{v[i].x , v[i].y}];
e.push_back({id_a, id_b , (v[i-1] - v[i]).dist2()});
}
}
sort(all(e) , [&](edge a , edge b){
return a.cost < b.cost;
});
dsu T(n);
vector<edge> 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...