QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#511246 | #9172. Alternating Cycle | ucup-team087# | RE | 1ms | 3880kb | C++20 | 30.3kb | 2024-08-09 17:59:31 | 2024-08-09 17:59:32 |
Judging History
answer
#ifndef LOCAL
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#endif
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define int ll
bool dbg=false;
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
using pi=pair<int,int>;
using vi=vc<int>;
using vvi=vc<vc<int>>;
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
return os<<"{"<<p.a<<","<<p.b<<"}";
}
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
os<<"{";
for(auto e:v)os<<e<<",";
return os<<"}";
}
#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ";
dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif
using uint=unsigned;
using ull=unsigned long long;
template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
return os<<vc<t>(all(a));
}
ll rand_int(ll l, ll r) { //[l, r]
//#ifdef LOCAL
static mt19937_64 gen;
/*#else
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
#endif*/
return uniform_int_distribution<ll>(l, r)(gen);
}
ll rand_int(ll k){ //[0,k)
return rand_int(0,k-1);
}
string rand_string(int n,char lw,char up){
string s(n,'?');
rep(i,n)s[i]=rand_int(lw,up);
return s;
}
int current_run_id,run_batch_size=1000;
int calc_random_limit(){
return current_run_id/run_batch_size+1;
}
template<class t>
void generate_single(t&a){
a=rand_int(1,calc_random_limit());
}
void generate_single(string&a){
int n;generate_single(n);
a=rand_string(n,'a','b');
}
template<class t,class u>
void generate_single(pair<t,u>&a){
generate_single(a.a);
generate_single(a.b);
}
//https://trap.jp/post/1224/
template<class... Args>
void input(Args&... a){
if(dbg){
(generate_single(a),...);
}else{
(cin >> ... >> a);
}
}
#define INT(...) int __VA_ARGS__;input(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;input(__VA_ARGS__)
#define ULL(...) ull __VA_ARGS__;input(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;input(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;input(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;input(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;input(__VA_ARGS__)
#define overload3(a,b,c,d,...) d
#define VI2(name,size) vi name(size);rep(i_##name,size)input(name[i_##name]);
#define VI3(name,size,offset) vi name(size);rep(i_##name,size)input(name[i_##name]),name[i_##name]+=offset;
#define VI(...) overload3(__VA_ARGS__,VI3,VI2)(__VA_ARGS__)
#define VPI(name,size) vc<pi> name(size);rep(i_##name,size)input(name[i_##name]);
#define VVI(name,sizeN,sizeM) vvi name(sizeN,vi(sizeM));\
rep(i_##name,sizeN)rep(j_##name,sizeM)input(name[i_##name][j_##name]);
#define VS(name,size) vc<string> name(size);rep(i_##name,size)input(name[i_##name]);
#define overload5(a,b,c,d,e,f,...) f
#define VVC4(type,name,sizeN,sizeM) vvc<type> name(sizeN,vc<type>(sizeM));
#define VVC5(type,name,sizeN,sizeM,ini) vvc<type> name(sizeN,vc<type>(sizeM,ini));
#define VVC(...) overload5(__VA_ARGS__,VVC5,VVC4)(__VA_ARGS__)
template<class T>
T vvvc(T v){
return v;
}
template<class T,class...Args>
auto vvvc(int n,T v,Args...args){
return vector(n,vvvc(v,args...));
}
template<int i,class T>
void print_tuple(ostream&,const T&){
}
template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
if(i)os<<",";
os<<get<i>(t);
print_tuple<i+1,T,Args...>(os,t);
}
template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
os<<"{";
print_tuple<0,tuple<Args...>,Args...>(os,t);
return os<<"}";
}
ll read(){
ll i;
cin>>i;
return i;
}
vi readvi(int n,int off=0){
vi v(n);
rep(i,n)v[i]=read()+off;
return v;
}
pi readpi(int off=0){
int a,b;cin>>a>>b;
return pi(a+off,b+off);
}
template<class t>
void print_single(t x,int suc=1){
cout<<x;
if(suc==1){
if(dbg)cout<<endl;
else cout<<"\n";
}
if(suc==2)
cout<<" ";
}
template<class t,class u>
void print_single(const pair<t,u>&p,int suc=1){
print_single(p.a,2);
print_single(p.b,suc);
}
template<class T>
void print_single(const vector<T>&v,int suc=1){
rep(i,v.size())
print_single(v[i],i==int(v.size())-1?suc:2);
}
template<class T>
void print_offset(const vector<T>&v,ll off,int suc=1){
rep(i,v.size())
print_single(v[i]+off,i==int(v.size())-1?suc:2);
}
template<class T,size_t N>
void print_single(const array<T,N>&v,int suc=1){
rep(i,N)
print_single(v[i],i==int(N)-1?suc:2);
}
template<class T>
void print(const T&t){
print_single(t);
}
template<class T,class ...Args>
void print(const T&t,const Args&...args){
print_single(t,2);
print(args...);
}
template<class T>
void printvv(const vvc<T>&vs){
for(const auto&row:vs)print(row);
}
string readString(){
string s;
cin>>s;
return s;
}
template<class T>
T sq(const T& t){
return t*t;
}
void YES(bool ex=true){
cout<<"YES\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void NO(bool ex=true){
cout<<"NO\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void Yes(bool ex=true){
cout<<"Yes\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void No(bool ex=true){
cout<<"No\n";
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
//#define CAPITAL
/*
void yes(bool ex=true){
#ifdef CAPITAL
cout<<"YES"<<"\n";
#else
cout<<"Yes"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void no(bool ex=true){
#ifdef CAPITAL
cout<<"NO"<<"\n";
#else
cout<<"No"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}*/
void possible(bool ex=true){
#ifdef CAPITAL
cout<<"POSSIBLE"<<"\n";
#else
cout<<"Possible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
void impossible(bool ex=true){
#ifdef CAPITAL
cout<<"IMPOSSIBLE"<<"\n";
#else
cout<<"Impossible"<<"\n";
#endif
if(ex)exit(0);
#ifdef LOCAL
cout.flush();
#endif
}
constexpr ll ten(int n){
return n==0?1:ten(n-1)*10;
}
const ll infLL=LLONG_MAX/3;
#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif
int topbit(signed t){
return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
return t==0?-1:63-__builtin_clzll(t);
}
int topbit(ull t){
return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
return a==0?64:__builtin_ctzll(a);
}
int botbit(ull a){
return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
return __builtin_popcount(t);
}
int popcount(ll t){
return __builtin_popcountll(t);
}
int popcount(ull t){
return __builtin_popcountll(t);
}
int bitparity(ll t){
return __builtin_parityll(t);
}
bool ispow2(int i){
return i&&(i&-i)==i;
}
ll mask(int i){
return (ll(1)<<i)-1;
}
ull umask(int i){
return (ull(1)<<i)-1;
}
ll minp2(ll n){
if(n<=1)return 1;
else return ll(1)<<(topbit(n-1)+1);
}
bool inc(int a,int b,int c){
return a<=b&&b<=c;
}
template<class S> void mkuni(S&v){
sort(all(v));
v.erase(unique(all(v)),v.ed);
}
template<class t> bool isuni(vc<t> v){
int s=si(v);
mkuni(v);
return si(v)==s;
}
template<class t>
void myshuffle(vc<t>&a){
rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
}
template<class S,class u>
int lwb(const S&v,const u&a){
return lower_bound(all(v),a)-v.bg;
}
template<class t,class u>
bool bis(const vc<t>&v,const u&a){
return binary_search(all(v),a);
}
//VERIFY: yosupo
//KUPC2017J
//AOJDSL1A
//without rank
struct unionfind{
vi p,s;
int c;
unionfind(int n):p(n,-1),s(n,1),c(n){}
void clear(){
fill(all(p),-1);
fill(all(s),1);
c=si(p);
}
int find(int a){
return p[a]==-1?a:(p[a]=find(p[a]));
}
//set b to a child of a
bool unite(int a,int b){
a=find(a);
b=find(b);
if(a==b)return false;
p[b]=a;
s[a]+=s[b];
c--;
return true;
}
bool same(int a,int b){
return find(a)==find(b);
}
int sz(int a){
return s[find(a)];
}
};
vvc<int> readGraph(int n,int m){
vvc<int> g(n);
rep(i,m){
int a,b;
cin>>a>>b;
//sc.read(a,b);
a--;b--;
g[a].pb(b);
g[b].pb(a);
}
return g;
}
vvc<int> rand_tree(int n){
vvc<int> t(n);
unionfind uf(n);
while(uf.c>1){
int a=rand_int(n);
int b=rand_int(n);
if(uf.unite(a,b)){
t[a].pb(b);
t[b].pb(a);
}
}
return t;
}
vvc<int> readTree(int n){
if(dbg){
return rand_tree(n);
}else{
return readGraph(n,n-1);
}
}
void printTree(const vvc<int> t){
int n=si(t);
int degsum=0;
rep(i,n)degsum+=si(t[i]);
if(degsum==n-1){
//directed
rep(i,si(t))for(auto j:t[i]){
print(i+1,j+1);
}
}else if(degsum==2*(n-1)){
//undirected
rep(i,si(t))for(auto j:t[i])if(i<j){
print(i+1,j+1);
}
}else{
assert(false);
}
}
template<class t>
vc<t> presum(const vc<t>&a){
vc<t> s(si(a)+1);
rep(i,si(a))s[i+1]=s[i]+a[i];
return s;
}
vc<ll> presum(const vi&a){
vc<ll> s(si(a)+1);
rep(i,si(a))s[i+1]=s[i]+a[i];
return s;
}
//BIT で数列を管理するときに使う (CF850C)
template<class t>
vc<t> predif(vc<t> a){
gnr(i,1,si(a))a[i]-=a[i-1];
return a;
}
template<class t>
vvc<ll> imos(const vvc<t>&a){
int n=si(a),m=si(a[0]);
vvc<ll> b(n+1,vc<ll>(m+1));
rep(i,n)rep(j,m)
b[i+1][j+1]=b[i+1][j]+b[i][j+1]-b[i][j]+a[i][j];
return b;
}
//verify してないや
void transvvc(int&n,int&m){
swap(n,m);
}
template<class t,class... Args>
void transvvc(int&n,int&m,vvc<t>&a,Args&...args){
assert(si(a)==n);
vvc<t> b(m,vi(n));
rep(i,n){
assert(si(a[i])==m);
rep(j,m)b[j][i]=a[i][j];
}
a.swap(b);
transvvc(n,m,args...);
}
//CF854E
void rotvvc(int&n,int&m){
swap(n,m);
}
template<class t,class... Args>
void rotvvc(int&n,int&m,vvc<t>&a,Args&...args){
assert(si(a)==n);
vvc<t> b(m,vi(n));
rep(i,n){
assert(si(a[i])==m);
rep(j,m)b[m-1-j][i]=a[i][j];
}
a.swap(b);
rotvvc(n,m,args...);
}
//ソートして i 番目が idx[i]
//CF850C
template<class t>
vi sortidx(const vc<t>&a){
int n=si(a);
vi idx(n);iota(all(idx),0);
sort(all(idx),[&](int i,int j){return a[i]<a[j];});
return idx;
}
//vs[i]=a[idx[i]]
//例えば sortidx で得た idx を使えば単にソート列になって返ってくる
//CF850C
template<class t>
vc<t> a_idx(const vc<t>&a,const vi&idx){
int n=si(a);
assert(si(idx)==n);
vc<t> vs(n);
rep(i,n)vs[i]=a[idx[i]];
return vs;
}
//CF850C
vi invperm(const vi&p){
int n=si(p);
vi q(n);
rep(i,n)q[p[i]]=i;
return q;
}
template<class t,class s=t>
s SUM(const vc<t>&a){
return accumulate(all(a),s(0));
}
template<class t,size_t K,class s=t>
s SUM(const array<t,K>&a){
return accumulate(all(a),s(0));
}
template<class t>
t MAX(const vc<t>&a){
return *max_element(all(a));
}
template<class t>
pair<t,int> MAXi(const vc<t>&a){
auto itr=max_element(all(a));
return mp(*itr,itr-a.bg);
}
template<class A>
auto MIN(const A&a){
return *min_element(all(a));
}
template<class t>
pair<t,int> MINi(const vc<t>&a){
auto itr=min_element(all(a));
return mp(*itr,itr-a.bg);
}
vi vid(int n){
vi res(n);iota(all(res),0);
return res;
}
template<class S>
void soin(S&s){
sort(all(s));
}
template<class S,class F>
void soin(S&s,F&&f){
sort(all(s),forward<F>(f));
}
template<class S>
S soout(S s){
soin(s);
return s;
}
template<class S>
void rein(S&s){
reverse(all(s));
}
template<class S>
S reout(S s){
rein(s);
return s;
}
template<class t,class u>
pair<t,u>&operator+=(pair<t,u>&a,pair<t,u> b){
a.a+=b.a;a.b+=b.b;return a;}
template<class t,class u>
pair<t,u>&operator-=(pair<t,u>&a,pair<t,u> b){
a.a-=b.a;a.b-=b.b;return a;}
template<class t,class u>
pair<t,u> operator+(pair<t,u> a,pair<t,u> b){return mp(a.a+b.a,a.b+b.b);}
template<class t,class u>
pair<t,u> operator-(pair<t,u> a,pair<t,u> b){return mp(a.a-b.a,a.b-b.b);}
template<class t,class u,class v>
pair<t,u>&operator*=(pair<t,u>&a,v b){
a.a*=b;a.b*=b;return a;}
template<class t,class u,class v>
pair<t,u> operator*(pair<t,u> a,v b){return a*=b;}
template<class t,class u>
pair<t,u> operator-(pair<t,u> a){return mp(-a.a,-a.b);}
namespace std{
template<class t,class u>
istream&operator>>(istream&is,pair<t,u>&a){
return is>>a.a>>a.b;
}
}
template<class t>
t gpp(vc<t>&vs){
assert(si(vs));
t res=move(vs.back());
vs.pop_back();
return res;
}
template<class t,class u>
void pb(vc<t>&a,const vc<u>&b){
a.insert(a.ed,all(b));
}
template<class t,class...Args>
vc<t> cat(vc<t> a,Args&&...b){
(pb(a,forward<Args>(b)),...);
return a;
}
template<class t,class u>
vc<t>& operator+=(vc<t>&a,u x){
for(auto&v:a)v+=x;
return a;
}
template<class t,class u>
vc<t> operator+(vc<t> a,u x){
return a+=x;
}
template<class t>
vc<t>& operator+=(vc<t>&a,const vc<t>&b){
a.resize(max(si(a),si(b)));
rep(i,si(b))a[i]+=b[i];
return a;
}
template<class t>
vc<t> operator+(const vc<t>&a,const vc<t>&b){
vc<t> c(max(si(a),si(b)));
rep(i,si(a))c[i]+=a[i];
rep(i,si(b))c[i]+=b[i];
return c;
}
template<class t,class u>
vc<t>& operator-=(vc<t>&a,u x){
for(auto&v:a)v-=x;
return a;
}
template<class t,class u>
vc<t> operator-(vc<t> a,u x){
return a-=x;
}
template<class t,class u>
vc<t>& operator*=(vc<t>&a,u x){
for(auto&v:a)v*=x;
return a;
}
template<class t,class u>
vc<t> operator*(vc<t> a,u x){
return a*=x;
}
template<class t,class u>
vc<t>& operator/=(vc<t>&a,u x){
for(auto&v:a)v/=x;
return a;
}
template<class t,class u>
vc<t> operator/(vc<t> a,u x){
return a/=x;
}
template<class t>
vc<t>& operator<<=(vc<t>&a,int k){
assert(k>=0);
a.insert(a.bg,k,t(0));
return a;
}
template<class t>
vc<t> operator<<(vc<t> a,int k){
return a<<=k;
}
template<class t,class u>
void remval(vc<t>&a,const u&v){
a.erase(remove(all(a),v),a.ed);
}
//消した要素の個数を返してくれる
//UCUP 2-8-F
template<class t,class F>
int remif(vc<t>&a,F f){
auto itr=remove_if(all(a),f);
int res=a.ed-itr;
a.erase(itr,a.ed);
return res;
}
template<class VS,class u>
void fila(VS&vs,const u&a){
fill(all(vs),a);
}
template<class t,class u>
int findid(const vc<t>&vs,const u&a){
auto itr=find(all(vs),a);
if(itr==vs.ed)return -1;
else return itr-vs.bg;
}
template<class t>
void rtt(vc<t>&vs,int i){
rotate(vs.bg,vs.bg+i,vs.ed);
}
//Multiuni2023-8 C
//f(lw)=false,...,f(n-1)=false,f(n)=true,...,f(up)=true,
//のときに n を返す
template<class F>
int find_min_true(int lw,int up,F f){
while(up-lw>1){
const int mid=(lw+up)/2;
if(f(mid))up=mid;
else lw=mid;
}
return up;
}
//f(lw)=true,f(up)=false
template<class F>
int find_max_true(int lw,int up,F f){
while(up-lw>1){
const int mid=(lw+up)/2;
if(f(mid))lw=mid;
else up=mid;
}
return lw;
}
template<class t> using pqmin=priority_queue<t,vc<t>,greater<t>>;
template<class t> using pqmax=priority_queue<t>;
using T=tuple<int,int,int>;
//copied from yosupo's library
//PARTLY VERIFIED
//USACO 2022 January ptlatinum C
//#define GEOF
#ifdef GEOF
using ld=long double;
//using ld=double;
const ld PI=acos(ld(-1));
#else
using ld=ll;
#endif
const ld eps=1e-9;
int sgn(ld a){return a<-eps?-1:(a>eps?1:0);}
int sgn(ld a,ld b){return sgn(a-b);}
/*
using pt=complex<ld>;
#define x real()
#define y imag()
*/
struct pt {
ld x,y;
//pt(ld _x = ld(0), ld _y = ld(0)) : x(_x), y(_y) {}
pt():x(0),y(0){}
pt(ld xx,ld yy):x(xx),y(yy){}
pt operator+(const pt& r) const { return {x + r.x, y + r.y}; }
pt operator-(const pt& r) const { return {x - r.x, y - r.y}; }
pt operator*(const pt& r) const {
return {x * r.x - y * r.y, x * r.y + y * r.x};
}
pt inv()const{
ld d=norm();
return {x/d,-y/d};
}
pt operator/(const pt&r)const{
return operator*(r.inv());
}
pt operator*(const ld& r) const { return {x * r, y * r}; }
pt operator/(const ld& r) const { return {x / r, y / r}; }
pt& operator+=(const pt& r) { return *this = *this + r; }
pt& operator-=(const pt& r) { return *this = *this - r; }
pt& operator*=(const pt& r) { return *this = *this * r; }
pt& operator/=(const pt& r) { return *this = *this / r; }
pt& operator*=(const ld& r) { return *this = *this * r; }
pt& operator/=(const ld& r) { return *this = *this / r; }
pt operator-() const { return {-x, -y}; }
static int cmp(const pt&a,const pt&b){
int v=sgn(a.x,b.x);
return v?v:sgn(a.y,b.y);
}
bool operator<(const pt& r) const {
return cmp(*this,r)<0;
}
bool operator<=(const pt& r) const {
return cmp(*this,r)<=0;
}
bool operator>(const pt& r) const {
return cmp(*this,r)>0;
}
bool operator==(const pt& r) const { return sgn((*this - r).rabs()) == 0; }
bool operator!=(const pt& r) const { return !(*this == r); }
ld norm() const { return x * x + y * y; }
ld rabs() const { return max(std::abs(x), std::abs(y)); } // robust abs
pair<ld, ld> to_pair() const { return {x, y}; }
#ifdef GEOF
ld abs() const { return sqrt(norm()); }
ld arg() const { return atan2(y, x); }
static pt polar(ld le, ld th) { return {le * cos(th), le * sin(th)}; }
#endif
};
istream& operator>>(istream& is, pt& p){
return is>>p.x>>p.y;
}
ostream& operator<<(ostream& os, const pt& p) {
return os << "pt(" << p.x << ", " << p.y << ")";
}
ld norm(const pt&a){
return a.norm();
}
ld rabs(const pt&a){
return a.rabs();
}
#ifdef GEOF
ld abs(const pt&a){
return sqrt(norm(a));
}
//XXII Opencup Gpt of Ural K
pt normalized(const pt&a){
return a/abs(a);
}
ld arg(const pt&a){return a.arg();}
//normalize to [-PI,PI)
//Contest 2: ptKU Contest 1, ptTZ Summer 2022 Day 4
ld normarg(ld a){
ld res=fmod(a+PI,2*PI);
if(res<0)res+=PI;
else res-=PI;
return res;
}
//normalize to [0,2*PI)
//Multiuni2023-10-E
ld normarg_nonnegative(ld a){
ld res=fmod(a,2*PI);
if(res<0)res+=2*PI;
return res;
}
//AOJ1183
//arg between ab
//assume given lengths are valid
ld arg(ld a,ld b,ld c){
return acos(min(max((a*a+b*b-c*c)/(2*a*b),ld(-1)),ld(1)));
}
//UCUP 2-20-D
ld heron(ld a,ld b,ld c){
ld s=(a+b+c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
#endif
ld norm(const pt&a,const pt&b){
return (a-b).norm();
}
ld dot(const pt&a,const pt&b){return a.x*b.x+a.y*b.y;}
ld crs(const pt& a,const pt& b){return a.x*b.y-a.y*b.x;}
ld crs(const pt& a,const pt& b,const pt& c){return crs(b-a,c-a);}
int ccw(const pt& a,const pt& b){return sgn(crs(a,b));}
int ccw(const pt& a,const pt& b,const pt& c){return ccw(b-a,c-a);}
//(-pi,0](0,pi]
int argtype(const pt&a){
if(sgn(a.y)==0)return a.x<0?1:0;
return a.y<0?0:1;
}
int argcmp(const pt&a,const pt&b){
int at=argtype(a),bt=argtype(b);
if(at!=bt)return at<bt?-1:1;
return -ccw(a,b);
};
bool argless(const pt&a,const pt&b){return argcmp(a,b)<0;}
//c の位置を聞く関数です,b じゃないです
//(-2)[a,-1](0)[b,1](2)
int bet(pt a,pt b,pt c){
pt d=b-a;
ld e=dot(d,c-a);
if(sgn(e)<=0)return sgn(e)-1;
return sgn(e-norm(d))+1;
}
//AOJ0153
//三角形 abc に d が含まれるか?0-no,1-edge,2-in
int cont(pt a,pt b,pt c,pt d){
if(ccw(a,b,c)==-1) swap(b,c);
return min({ccw(a,b,d),ccw(b,c,d),ccw(c,a,d)})+1;
}
//(a,b) を結ぶ直線を考え,x 座標との交点の y 座標を求める
//(分子,分母)を返す
pair<ld,ld> xcut(const pt&a,const pt&b,ld x){
return mp(a.y*(b.x-x)-b.y*(a.x-x),b.x-a.x);
}
//XXII Opencup Gpt of Ural K
pt rot90(pt a){
return pt(-a.y,a.x);
}
#ifdef GEOF
//Multiuni 2024-6-C
pt rot(pt a,ld b){
ld c=cos(b),s=sin(b);
return pt(a.x*c-a.y*s,a.x*s+a.y*c);
}
ld xcutval(const pt&a,const pt&b,ld x){
auto [p,q]=xcut(a,b,x);
return p/q;
}
//AOJ1183
//Xmas2010 E
//-+ の 順で返す
//a の符号により,small/large が決まる
int qeq(ld a,ld b,ld c,ld&d,ld&e){
if(sgn(a)==0){
if(sgn(b)==0)return 0;
d=-c/b;
return 1;
}
ld f=b*b-4*a*c;
if(sgn(f)<0)return 0;
ld g=sqrt(max(f,ld(0)));
d=(-b-g)/(2*a);
e=(-b+g)/(2*a);
return sgn(f)+1;
}
#else
pt normdir(pt a){
if(a==pt(0,0))return a;
int g=gcd(a.x,a.y);
return pt(a.x/g,a.y/g);
}
#endif
ld area2(const vc<pt>&ps){
ld res=0;
rep(i,si(ps))res+=crs(ps[i],ps[(i+1)%si(ps)]);
return res;
}
//stress-test してません
vc<pt> lower_convex(const vc<pt>&ps,bool onedge){
vc<pt> res;
assert(is_sorted(all(ps)));
for(auto p:ps){
int s;
while((s=si(res))>=2){
if(ccw(res[s-2],res[s-1],p)<=(onedge?-1:0))res.pop_back();
else break;
}
res.pb(p);
}
return res;
}
vc<pt> upper_convex(const vc<pt>&ps,bool onedge){
vc<pt> res;
assert(is_sorted(all(ps)));
for(auto p:ps){
int s;
while((s=si(res))>=2){
if(ccw(res[s-2],res[s-1],p)>=(onedge?1:0))res.pop_back();
else break;
}
res.pb(p);
}
return res;
}
vc<pt> convex(vc<pt> ps,bool onedge){
mkuni(ps);
auto lw=lower_convex(ps,onedge);
auto up=upper_convex(ps,onedge);
lw.pop_back();
rein(up);
up.pop_back();
pb(lw,up);
return lw;
}
//GCJ2019R3D
//0-out,1-edge,2-in
int cont(const vc<pt>&a,pt b){
int n=a.size();
int res=0;
rep(i,n){
pt c=a[i],d=a[(i+1)%n];
if(ccw(c,d,b)==0&&abs(bet(c,d,b))<=1)
return 1;
if(c.y>d.y)swap(c,d);
if(sgn(b.y-c.y)>0&&sgn(d.y-b.y)>=0&&ccw(c,d,b)==1)
res^=1;
}
return res*2;
}
//長さ N,要素 0-M-1 のソート列を全探索し,F を呼ぶ
template<class F>
void sorted_array_rec(int n,int m,F f){
vi a(n);
auto rec=[&](auto self,int i)->void{
if(i==n)f(a);
else{
int last=i?a[i-1]:0;
rng(nx,last,m){
a[i]=nx;
self(self,i+1);
}
}
};
rec(rec,0);
}
//UCUP 2-27-B
//[0,m) の値を distinct に n 個取ってきたやつを並べる
template<class F>
void sorted_distinct_rec(int n,int m,F f){
sorted_array_rec(n,m-n+1,[&](vi a){
rep(i,n)a[i]+=i;
f(a);
});
}
//vs から k 要素選んだ tuple すべてに対して f を呼ぶ
//ARC177 E
template<class t,class F>
void tuple_rec(const vc<t>&vs,int k,F f){
int n=si(vs);
if(k<=n){
vc<t> a(k);
sorted_array_rec(k,n-k+1,[&](const vi&idx){
rep(i,k)a[i]=vs[idx[i]+i];
f(a);
});
}
};
template<class F> void permrep(int n,F f){
vi p(n);iota(all(p),0);
do{
f(p);
}while(next_permutation(all(p)));
};
//N() が単位元
//VERIFY: yosupo
//CF407E
template<class N>
struct segtree{
vc<N> x;
int n,s;
segtree(){}
template<class t>
segtree(const vc<t>&a){
n=a.size();
s=1;
while(s<n){s*=2;}
x.resize(s*2);
rep(i,n)
x[s+i]=N(a[i]);
gnr(i,1,s)
x[i]=N::merge(x[i*2],x[i*2+1]);
}
//NOT Verified
segtree(int nn){
resize(nn);
}
void resize(int nn){
n=nn;
s=1;
while(s<n){s*=2;}
x.assign(s*2,N());
gnr(i,1,s)
x[i]=N::merge(x[i*2],x[i*2+1]);
}
template<class t>
void init(const vc<t>&a){
n=a.size();
s=1;
while(s<n){s*=2;}
x.resize(s*2);
rep(i,n)
x[s+i]=N(a[i]);
rng(i,n,s)
x[s+i]=N();
gnr(i,1,s)
x[i]=N::merge(x[i*2],x[i*2+1]);
}
void clear(){
rep(i,n)
x[s+i]=N();
gnr(i,1,s)
x[i]=N::merge(x[i*2],x[i*2+1]);
}
N point_get(int i){
assert(inc(0,i,n-1));
return x[i+s];
}
void point_set(int i,const N&t){
assert(inc(0,i,n-1));
i+=s;
x[i]=t;
while(i>>=1)x[i]=N::merge(x[i*2],x[i*2+1]);
}
void point_merge(int i,const N&t){
assert(inc(0,i,n-1));
i+=s;
x[i]=N::merge(x[i],t);
while(i>>=1)x[i]=N::merge(x[i*2],x[i*2+1]);
}
template<class F,class...Args>
void point_change(int i,F f,Args&&...args){
assert(inc(0,i,n-1));
i+=s;
(x[i].*f)(forward<Args>(args)...);
while(i>>=1)x[i]=N::merge(x[i*2],x[i*2+1]);
}
N composite(int b,int e){
assert(0<=b&&b<=e&&e<=n);
N lf,rt;
for(int l=b+s,r=e+s;l<r;l>>=1,r>>=1){
if (l&1){
lf=N::merge(lf,x[l]);
l++;
}
if (r&1){
r--;
rt=N::merge(x[r],rt);
}
}
return N::merge(lf,rt);
}
N getall(){
return x[1];
}
//UTPC2020E
//n 超えるかもしれない
template <class F,class... Args>
pair<int,N> max_right(int l,F f,Args&&... args){
assert((N().*f)(forward<Args>(args)...));
assert(0<=l&&l<=n);
if(l==n)return mp(n,N());
l+=s;
N sm;
assert((sm.*f)(forward<Args>(args)...));
do {
while (l % 2 == 0) l >>= 1;
if (!(N::merge(sm,x[l]).*f)(forward<Args>(args)...)){
while (l < s) {
l = (2 * l);
N tmp=N::merge(sm,x[l]);
if ((tmp.*f)(forward<Args>(args)...)) {
sm = tmp;
l++;
}
}
return mp(l - s,sm);
}
sm = N::merge(sm, x[l]);
l++;
} while ((l & -l) != l);
return mp(n,sm);
}
//UCUP-2-22-K
template <class F,class... Args>
pair<int,N> min_left_withinit(int r,N sm,F f,Args&&... args){
assert((sm.*f)(forward<Args>(args)...));
assert(0<=r&&r<=n);
if(r==0)return mp(0,sm);
r+=s;
do {
r--;
while (r > 1 && (r % 2)) r >>= 1;
if (!(N::merge(x[r],sm).*f)(forward<Args>(args)...)) {
while (r < s) {
r = (2 * r + 1);
N tmp=N::merge(x[r],sm);
if ((tmp.*f)(forward<Args>(args)...)) {
sm = tmp;
r--;
}
}
return mp(r + 1 - s,sm);
}
sm = N::merge(x[r], sm);
} while ((r & -r) != r);
return mp(0,sm);
}
//UTPC2020E
template <class F,class... Args>
pair<int,N> min_left(int r,F f,Args&&... args){
return min_left_withinit(r,N(),f,forward<Args>(args)...);
}
//行列とか乗せて必要なのはベクトルとの積,みたいなときに使える?
//CF Goodbye 2016 E
//CF 896 F
template<class F,class T,class... Args>
T accumulate(int l,int r,F f,T t,Args&&... args) {
assert(0<=l&&l<=r&&r<=n);
static int buf[2][30];
int cnt[2]{};
for(l+=s,r+=s;l<r;l>>=1,r>>=1){
if(l&1)buf[0][cnt[0]++]=l;
if(r&1)buf[1][cnt[1]++]=r-1;
l++;
}
rep(i,cnt[0])t=(x[buf[0][i]].*f)(t,forward<Args>(args)...);
per(i,cnt[1])t=(x[buf[1][i]].*f)(t,forward<Args>(args)...);
return t;
}
};
struct MinNode{
pi v;
MinNode(pi vv=pi(inf,-1)):v(vv){}
MinNode(int val,int idx):v(val,idx){}
static MinNode merge(const MinNode&a,const MinNode&b){
return MinNode(min(a.v,b.v));
}
};
bool brutecheck(vc<pt> ps){
assert(isuni(ps));
int n=si(ps);assert(n%2==0);
int pre=1;
rep(i,n+1){
pt a=ps[(i+0)%n];
pt b=ps[(i+1)%n];
pt c=ps[(i+2)%n];
int k=ccw(a,b,c);
assert(k!=0);
if(pre==k)return false;
pre=k;
}
return true;
}
void slv(){
INT(n);
vc<pt> ps(n);
rep(i,n)cin>>ps[i];
auto cv=convex(ps,false);
int inner=n-si(cv);
if(inner<=1){
return print(-1);
}else if(inner==2){
pt A,B;
{
vc<pt> in;
auto vs=cv;
soin(vs);
for(auto p:ps)if(!bis(vs,p)){
in.pb(p);
}
A=in[0];
B=in[1];
}
vc<pt> lf,rt;
ps=cv;
n=si(cv);
rep(i,n){
if(ccw(A,B,ps[i])==-1){
rtt(ps,i);
break;
}
}
rep(i,n){
if(ccw(A,B,ps[i])==1){
rtt(ps,i);
break;
}
}
for(auto p:ps){
if(ccw(A,B,p)==1){
lf.pb(p);
}else{
rt.pb(p);
}
}
const int V=si(rt);
segtree<MinNode> seg(V+1);
vc<pi> ab;
//vc<R> ue;
rep(i,si(lf)){
auto p=lf[i];
int a=find_min_true(0,V,[&](int x){
return ccw(p,A,rt[x])<0;
});
int b=find_min_true(0,V,[&](int x){
return ccw(p,B,rt[x])<0;
});
//ue [a,b)*[b,V)
//sita [0,a),[a,b)
ab.eb(a,b);
}
pi nice(-1,-1);
rep(i,si(lf)){
auto [a,b]=ab[i];
if(0<a&&a<b){
auto [v,pos]=seg.composite(0,a).v;
if(v<b){
nice=pi(i,pos);
}
}
if(a<b&&b<V){
seg.point_merge(a,pi(b,i));
}
}
if(nice==pi(-1,-1)){
return print(-1);
}
ps=vc<pt>{A,B};
ps.pb(lf[nice.a]);
ps.pb(lf[nice.b]);
{
int x=ab[nice.b].a;
int y=ab[nice.a].b-1;
ps.pb(rt[x]);
ps.pb(rt[y]);
}
}else{
vc<pt> in,out;
{
auto vs=cv;
soin(vs);
for(auto p:ps)if(!bis(vs,p)){
in.pb(p);
}
}
assert(si(in)>=3);
in.resize(3);
in=convex(in,false);
assert(si(in)==3);
auto kind=[&](pt p){
int res=0;
rep(k,3){
pt A=in[k],B=in[(k+1)%3];
if(ccw(A,B,p)<0)res|=1<<k;
}
return res;
};
dmp(cv);
rep(i,si(cv))dmp(kind(cv[i]));
rep(k,3){
int tar=mask(3)-(1<<k);
dmp(tar);
bool done=false;
for(auto p:cv)if((kind(p)&tar)==tar){
out.pb(p);
done=true;
break;
}
if(!done){
pt x=in[k],y=in[(k+1)%3],z=in[(k+2)%3];
rep(i,si(cv)){
pt p=cv[i],q=cv[(i+1)%si(cv)];
if(ccw(x,z,p)<0&&ccw(y,z,q)>0){
out.pb(p);
out.pb(q);
done=true;
break;
}
}
}
assert(done);
dmp(out);
}
mkuni(out);
assert(si(out)<=6);
out=convex(out,false);
dmp(in);
dmp(out);
for(auto p:in)assert(cont(out,p)==2);
ps=cat(in,out);
}
assert(inc(6,si(ps),9));
vc<pt> ans;
tuple_rec(ps,6,[&](vc<pt> ls){
permrep(6,[&](vi p){
auto now=a_idx(ls,p);
if(brutecheck(now)){
ans=now;
}
});
});
assert(si(ans)==6);
print(6);
for(auto [x,y]:ans){
print(x,y);
}
}
signed main(signed argc,char*argv[]){
if(argc>1&&strcmp(argv[1],"D")==0)dbg=true;
cin.tie(0);
ios::sync_with_stdio(0);
cout<<fixed<<setprecision(20);
if(dbg){
while(1){
if(current_run_id%run_batch_size==0){
cerr<<"Current Run "<<current_run_id<<endl;
}
slv();
current_run_id++;
}
}else{
//int t;cin>>t;rep(_,t)
slv();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3812kb
input:
6 10 15 20 15 15 23 0 31 15 0 30 30
output:
6 30 30 15 23 0 31 10 15 15 0 20 15
result:
ok Everything ok
Test #2:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
4 0 0 0 1 1 0 1 1
output:
-1
result:
ok Everything ok
Test #3:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
9 693150551 810053304 26684055 999173154 767007508 725151476 697948407 601311897 593914132 156628727 294286621 249587903 361249906 42266067 110658137 698550461 923704821 886066345
output:
6 923704821 886066345 693150551 810053304 26684055 999173154 697948407 601311897 593914132 156628727 767007508 725151476
result:
ok Everything ok
Test #4:
score: 0
Accepted
time: 1ms
memory: 3880kb
input:
9 870407732 947373192 362190573 311657719 792350850 916217578 908809410 529664178 147184624 105531482 800863654 27569449 290489622 819212758 64484618 355712627 474856219 425123887
output:
6 290489622 819212758 800863654 27569449 474856219 425123887 147184624 105531482 870407732 947373192 792350850 916217578
result:
ok Everything ok
Test #5:
score: 0
Accepted
time: 0ms
memory: 3588kb
input:
9 47664912 379660370 66293309 34207701 186290410 443720168 456106901 458016459 995422410 349401528 602407977 731922069 588325559 932595937 608245683 644278574 657411398 627744942
output:
6 588325559 932595937 186290410 443720168 47664912 379660370 456106901 458016459 995422410 349401528 602407977 731922069
result:
ok Everything ok
Test #6:
score: 0
Accepted
time: 0ms
memory: 3672kb
input:
9 224922093 516980257 696767119 51724974 580229972 266190050 593338977 91401448 843660194 888238866 108985009 509903616 591194203 709542627 225635675 932844521 618628214 461769776
output:
6 843660194 888238866 591194203 709542627 108985009 509903616 224922093 516980257 593338977 91401448 580229972 266190050
result:
ok Everything ok
Test #7:
score: 0
Accepted
time: 1ms
memory: 3660kb
input:
9 107211982 359332853 695837148 142871176 605573313 162288860 509232688 314721021 396930687 132108911 205496625 287885162 183997430 822925807 474429448 221410467 801183393 664390830
output:
6 801183393 664390830 509232688 314721021 107211982 359332853 695837148 142871176 605573313 162288860 396930687 132108911
result:
ok Everything ok
Test #8:
score: 0
Accepted
time: 0ms
memory: 3656kb
input:
9 284469163 791620032 31343664 160388450 294480166 689791450 351497471 948106011 245168471 81011666 7040948 65866709 481833366 304905205 91819440 215009122 983738573 203448372
output:
6 351497471 948106011 294480166 689791450 7040948 65866709 91819440 215009122 983738573 203448372 481833366 304905205
result:
ok Everything ok
Test #9:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
9 461726344 560343699 661817474 882938432 319823507 217294040 562358475 876458292 93406256 619849004 513617981 138815548 484702011 418288384 340613213 503575069 534889971 406069427
output:
6 661817474 882938432 461726344 560343699 93406256 619849004 340613213 503575069 513617981 138815548 484702011 418288384
result:
ok Everything ok
Test #10:
score: -100
Runtime Error
input:
9 638983525 992630879 660887503 900455706 713763069 113392850 404623258 99777864 646676749 863719050 315162304 548200875 782537947 195235074 958003206 160737235 422477859 945126970