QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#492976 | #9137. Ancient Country | ucup-team087# | AC ✓ | 1ms | 3896kb | C++20 | 29.1kb | 2024-07-26 17:51:40 | 2024-07-26 17:51:41 |
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,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
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;
}
using ln=pair<pt,pt>;
pt dir(ln a){return a.b-a.a;}
pt eval(ln a,ld b){return a.a+dir(a)*b;}
ld crs(ln a,pt b){return crs(a.a,a.b,b);}
int ccw(ln a,pt b){return ccw(a.a,a.b,b);}
int bet(ln a,pt b){return bet(a.a,a.b,b);}
ld projt(ln a,pt b){
pt c=dir(a);
return dot(c,b-a.a)/norm(c);
}
pt proj(ln a,pt b){
pt c=dir(a);
return a.a+c*dot(c,b-a.a)/norm(c);
}
pt refl(ln a,pt b){
return proj(a,b)*2-b;
}
//AOJ1157
//0-no,1-yes(endpoint),2-yes(innner),3-overelap
//if the two line touch like this
// x----x----x
//it returns 1
int iss(ln a,ln b){
int c1=ccw(a.a,a.b,b.a),c2=ccw(a.a,a.b,b.b);
int d1=ccw(b.a,b.b,a.a),d2=ccw(b.a,b.b,a.b);
if(c1||c2||d1||d2)return 1-max(c1*c2,d1*d2);
int f=bet(a.a,a.b,b.a),g=bet(a.a,a.b,b.b);
if(max(f,g)==-2||min(f,g)==2)return 0;
if(max(f,g)==-1||min(f,g)==1)return 1;
return 3;
}
//segment a intersects line b?
//endpoints inclusive
bool isl(ln a,ln b){
int d1=ccw(b.a,b.b,a.a),d2=ccw(b.a,b.b,a.b);
return d1*d2<=0;
}
//並行でない->true, というだけ
//直線が一致とかは考えてないことに注意
bool ill(ln a,ln b){
return ccw(dir(a),dir(b));
}
ld cllt(ln a,ln b){
return crs(b.a,b.b,a.a)/crs(dir(a),dir(b));
}
//ICPC Yokohama 2022 J
pair<ld,ld> clltf(ln a,ln b){
return mp(crs(b.a,b.b,a.a),crs(dir(a),dir(b)));
}
//AOJ1033
pt cll(ln a,ln b){
return eval(a,crs(b.a,b.b,a.a)/crs(dir(a),dir(b)));
}
//UCUP3-4-H
bool isp(ln a,pt b){
return ccw(a,b)==0&&inc(-1,bet(a.a,a.b,b),1);
}
#ifdef GEOF
//AOJ2201
ld dlp(ln a,pt b){
return abs(crs(a,b)/abs(dir(a)));
}
//AOJ0153
ld dsp(ln a,pt b){
pt c=proj(a,b);
if(abs(bet(a.a,a.b,c))<=1)return abs(b-c);
return min(abs(b-a.a),abs(b-a.b));
}
//ABC314H
//b から最も近い a 上の点を返す
pt dsp_tar(ln a,pt b){
pt c=proj(a,b);
if(abs(bet(a.a,a.b,c))<=1)return c;
return abs(b-a.a)<abs(b-a.b)?a.a:a.b;
}
//AOJ1157
ld dss(ln a,ln b){
if(iss(a,b))return 0;
return min({dsp(a,b.a),dsp(a,b.b),dsp(b,a.a),dsp(b,a.b)});
}
//AOJ2160
//反時計回り方向に伸びる垂直二等分線
ln vbis(pt a,pt b){
pt c=(a+b)*ld(0.5),d=b-a;
return ln(c,pt(c.x-d.y,c.y+d.x));
}
ld cutareat(ln z,ld l,ld r){
pt a=eval(z,l);
pt b=eval(z,r);
return -(b.x-a.x)*(a.y+b.y)/2;
}
#endif
//ABC286H
//simple polygon と線分が交わるか
//接している場合も true
/*
bool icl(const vc<pt>&ps,ln z){
int n=si(ps);
rep(i,n){
pt p=ps[i],q=ps[(i+1)%n];
if(iss(z,ln(p,q)))return true;
}
return cont(ps,z.a);
}*/
//線分が polygon の中に含まれているかどうか
//polygon の周に乗っている場合も OK
//ICPC Yokohama 2022 J
bool within_polygon(const vc<pt>&ps,ln z){
int n=si(ps);
bool ok=true;
int cnt=0;
auto add=[&](ld num,ld den){
if(den<0){
num=-num;
den=-den;
}
if(num<=0){
cnt++;
}else if(num>=den){
}else{
ok=false;
}
};
rep(i,n){
pt p=ps[i],q=ps[(i+1)%n],r=ps[(i+n-1)%n];
int a=ccw(z,p),b=ccw(z,q);
if(a*b==-1){
auto [num,den]=clltf(z,ln(p,q));
add(num,den);
}
if(a==0){
int c=ccw(z,r);
if(b!=c&&(b*c<0||ccw(p,q,r)>0))
add(dot(ps[i]-z.a,dir(z)),norm(dir(z)));
}
}
return ok&&cnt%2==1;
}
bool is_simple_polygon(const vc<pt>&ps){
int n=si(ps);
rep(i,n)rng(j,i+1,n){
ln a(ps[i],ps[i+1]);
ln b(ps[j],ps[(j+1)%n]);
int z=iss(a,b);
//0-no,1-yes(endpoint),2-yes(innner),3-overelap
//if the two line touch like this
// x----x----x
//it returns 1
if(z==0){
//ok
}else if(z==1){
if(j==i+1||(i==0&&j==n-1)){
//ok
}else{
return false;
}
}else{
return false;
}
}
return true;
}
vc<pt> generete_simple_polygon(int n){
while(1){
const int vmax=ten(6);
set<pi>xy;
while(si(xy)<n){
int x=rand_int(-vmax,vmax);
int y=rand_int(-vmax,vmax);
xy.emplace(x,y);
}
vc<pt> ps;
for(auto [x,y]:xy)
ps.eb(x,y);
myshuffle(ps);
if(is_simple_polygon(ps)){
int a=area2(ps);
if(a<0){
rein(ps);
}
return ps;
}
}
}
//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;
}
bool intersect_polygon(const vc<pt>&a,const vc<pt>&b){
for(auto v:a)if(cont(b,v))return true;
for(auto v:b)if(cont(a,v))return true;
rep(i,si(a)){
ln c(a[i],a[(i+1)%si(a)]);
rep(j,si(b)){
ln d(b[j],b[(j+1)%si(b)]);
if(iss(c,d))return true;
}
}
return false;
}
template<class F>
void subset_for(int b,F f){
//subset of b(descending)
int a=b;
do{
f(a);
a=(a-1)&b;
}while(a!=b);
}
void slv(){
INT(n);
if(dbg){
n+=2;
}
vc<pt> ps(n);
if(dbg){
ps=generete_simple_polygon(n);
assert(is_simple_polygon(ps));
//int n=si(ps);
/*rep(i,n)rng(j,i+1,n){
ln a(ps[i],ps[i+1]);
ln b(ps[j],ps[(j+1)%n]);
int z=iss(a,b);
//0-no,1-yes(endpoint),2-yes(innner),3-overelap
//if the two line touch like this
// x----x----x
//it returns 1
dmp2(a,b,z);
if(z==0){
//ok
}else if(z==1){
if(j==i+1||(i==0&&j==n-1)){
//ok
}else{
//return false;
}
}else{
//return false;
}
}
//return true;*/
}else{
rep(i,n)cin>>ps[i];
}
INT(W,C);
vc<pi> es;
//VVC(bool,avail,n,n);
rep(i,n)rng(j,i+1,n){
if(within_polygon(ps,ln(ps[i],ps[j]))){
bool bad=false;
rep(k,n)if(i!=k&&j!=k)
if(isp(ln(ps[i],ps[j]),ps[k])){
bad=true;
break;
}
if(!bad){
//avail[i][j]=true;
//avail[j][i]=true;
es.eb(i,j);
es.eb(j,i);
}
}
}
//dmp(es);
soin(es,[&](pi a,pi b){
pt c=ps[a.b]-ps[a.a];
pt d=ps[b.b]-ps[b.a];
int z=argcmp(c,d);
if(z)return z<0;
pt w=normdir(c);
return dot(w,ps[a.a])<dot(w,ps[b.a]);
});
VVC(int,idx,n,n,-1);
{
vc<pi> tmp;
for(auto [i,j]:es){
if(i<j)tmp.eb(i,j);
else idx[i][j]=si(tmp);
}
es.swap(tmp);
}
VVC(int,dp,n,n,0);
vi buf(n),has(n);
per(l,n)rng(r,l+1,n){
if(int ini=idx[r][l];ini!=-1){
fila(buf,-inf);
fila(has,-inf);
buf[l]=crs(ps[r],ps[l])+W+C;
auto adv=[&](int u,int v){
assert(u<v);
if(l<=u&&v<=r){
{
int val=buf[u]+crs(ps[u],ps[v])+W+dp[u+1][v-1];
if(ccw(ps[l],ps[r],ps[v]))
chmax(has[v],val);
else
chmax(buf[v],val);
}
{
int val=has[u]+crs(ps[u],ps[v])+W+dp[u+1][v-1];
chmax(has[v],val);
}
}
};
rng(i,ini,si(es))adv(es[i].a,es[i].b);
rep(i,ini)adv(es[i].a,es[i].b);
chmax(dp[l][r],has[r]);
}
//chmax(dp[l][r],dp[l+1][r]);
//chmax(dp[l][r],dp[l][r-1]);
rng(mid,l,r){
chmax(dp[l][r],dp[l][mid]+dp[mid+1][r]);
}
}
int ans=dp[0][n-1];
if(dbg){
vi city(1<<n);
vvc<pt> unko(1<<n);
rep(bit,1<<n){
vc<pt> ls;
rep(i,n)if(bit&1<<i){
ls.pb(ps[i]);
}
int k=si(ls);
if(k>=3){
bool ok=true;
rep(i,k){
int j=(i+1)%k;
if(!within_polygon(ps,ln(ls[i],ls[j]))){
ok=false;
break;
}
}
if(ok){
rep(i,k){
pt a=ls[i];
pt b=ls[(i+1)%k];
pt c=ls[(i+2)%k];
if(ccw(a,b,c)<0)ok=false;
}
}
if(area2(ls)&&ok){
city[bit]=area2(ls)+W*k+C;
unko[bit]=ls;
}
}
}
int god=0;
vi st;
auto dfs=[&](auto self,int bit)->void{
if(bit==0){
int sum=0;
for(auto i:st)sum+=city[i];
chmax(god,sum);
}else{
int q=botbit(bit);
{
self(self,bit^(1<<q));
}
subset_for(bit^(1<<q),[&](int use){
use|=1<<q;
if(si(unko[use])){
bool ok=true;
for(auto pre:st){
if(intersect_polygon(unko[pre],unko[use])){
ok=false;
}
}
if(ok){
st.pb(use);
self(self,bit^use);
st.pop_back();
}
}
});
}
};
dfs(dfs,mask(n));
if(god!=ans){
cerr<<ps<<endl;
cerr<<W<<" "<<C<<endl;
cerr<<god<<" "<<ans<<endl;
}
assert(god==ans);
}else{
print(ans);
}
}
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();
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3664kb
input:
6 0 0 1 -1 1 2 0 1 -1 2 -1 -1 1 2
output:
16
result:
ok 1 number(s): "16"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3696kb
input:
12 0 0 1 1 2 0 4 1 5 0 5 1 8 0 9 1 10 0 11 1 5 5 1 4 3 1000000
output:
3000063
result:
ok 1 number(s): "3000063"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3596kb
input:
12 0 0 1 1 2 0 4 1 5 0 5 1 8 0 9 1 10 0 11 1 5 5 1 4 0 9
output:
61
result:
ok 1 number(s): "61"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3664kb
input:
7 0 20 40 0 40 20 70 50 50 70 30 50 0 50 8 12
output:
3980
result:
ok 1 number(s): "3980"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3604kb
input:
3 0 2017 -2017 -2017 2017 0 1 12
output:
12204882
result:
ok 1 number(s): "12204882"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3668kb
input:
3 4 0 0 3 0 0 10 11
output:
53
result:
ok 1 number(s): "53"
Test #7:
score: 0
Accepted
time: 0ms
memory: 3896kb
input:
3 -10 0 10 0 0 18 3 10
output:
379
result:
ok 1 number(s): "379"
Test #8:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
4 0 0 -10 0 -10 -10 0 -10 11 9
output:
253
result:
ok 1 number(s): "253"
Test #9:
score: 0
Accepted
time: 0ms
memory: 3896kb
input:
4 -100 0 0 -100 100 0 0 100 4 9
output:
40025
result:
ok 1 number(s): "40025"
Test #10:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
4 0 0 10 5 20 0 10 10 13 8
output:
97
result:
ok 1 number(s): "97"
Test #11:
score: 0
Accepted
time: 0ms
memory: 3676kb
input:
4 0 0 20 0 30 10 10 10 6 7
output:
431
result:
ok 1 number(s): "431"
Test #12:
score: 0
Accepted
time: 0ms
memory: 3680kb
input:
4 -100 -10 100 -10 100 10 -100 10 15 7
output:
8067
result:
ok 1 number(s): "8067"
Test #13:
score: 0
Accepted
time: 0ms
memory: 3552kb
input:
4 0 0 100 0 60 30 0 30 14 7
output:
4863
result:
ok 1 number(s): "4863"
Test #14:
score: 0
Accepted
time: 0ms
memory: 3664kb
input:
4 0 0 100 0 60 30 40 30 7 7
output:
3635
result:
ok 1 number(s): "3635"
Test #15:
score: 0
Accepted
time: 0ms
memory: 3680kb
input:
7 0 0 10 10 20 0 30 11 20 22 10 11 0 22 15 6
output:
777
result:
ok 1 number(s): "777"
Test #16:
score: 0
Accepted
time: 0ms
memory: 3884kb
input:
10 0 0 10 10 20 0 30 10 40 0 40 21 30 11 20 21 10 11 0 21 8 5
output:
935
result:
ok 1 number(s): "935"
Test #17:
score: 0
Accepted
time: 0ms
memory: 3680kb
input:
7 0 0 50 50 80 20 110 50 70 90 40 60 0 100 1 5
output:
8217
result:
ok 1 number(s): "8217"
Test #18:
score: 0
Accepted
time: 0ms
memory: 3864kb
input:
16 0 0 20 0 20 10 40 10 40 20 60 20 60 30 70 30 70 40 50 40 50 30 30 30 30 20 10 20 10 10 0 10 10 4
output:
1576
result:
ok 1 number(s): "1576"
Test #19:
score: 0
Accepted
time: 0ms
memory: 3888kb
input:
12 0 0 400 0 400 500 0 500 0 200 200 200 200 300 100 300 100 400 300 400 300 100 0 100 3 3
output:
160045
result:
ok 1 number(s): "160045"
Test #20:
score: 0
Accepted
time: 0ms
memory: 3820kb
input:
10 0 500 -10 20 -350 350 -20 0 -350 -350 0 -20 350 -350 20 0 350 350 10 20 11 3
output:
31594
result:
ok 1 number(s): "31594"
Test #21:
score: 0
Accepted
time: 0ms
memory: 3596kb
input:
8 0 0 20 -10 40 0 40 5 60 0 60 -5 80 0 40 20 98 44
output:
2172
result:
ok 1 number(s): "2172"
Test #22:
score: 0
Accepted
time: 0ms
memory: 3688kb
input:
10 0 0 20 -10 40 0 45 0 40 5 60 0 65 0 60 -5 80 0 40 20 76 73
output:
2029
result:
ok 1 number(s): "2029"
Test #23:
score: 0
Accepted
time: 0ms
memory: 3676kb
input:
4 0 0 50 -20 100 0 50 20 57 77
output:
4305
result:
ok 1 number(s): "4305"
Test #24:
score: 0
Accepted
time: 0ms
memory: 3884kb
input:
8 0 0 0 10 -10 0 50 -20 110 0 100 10 100 0 50 -10 72 60
output:
1424
result:
ok 1 number(s): "1424"
Test #25:
score: 0
Accepted
time: 0ms
memory: 3852kb
input:
8 100 90 90 100 -90 100 -100 90 -100 -90 -90 -100 90 -100 100 -90 1014818403091 6142309236000
output:
20403165768728
result:
ok 1 number(s): "20403165768728"
Test #26:
score: 0
Accepted
time: 0ms
memory: 3820kb
input:
8 0 0 10 100 90 100 100 0 100 201 90 101 10 101 0 201 2554103132320 5961656865249
output:
32356138793098
result:
ok 1 number(s): "32356138793098"
Test #27:
score: 0
Accepted
time: 0ms
memory: 3884kb
input:
8 0 0 100 100 900 100 1000 0 1000 210 900 110 100 110 0 210 744569926410 2151448654813
output:
10259456764906
result:
ok 1 number(s): "10259456764906"
Test #28:
score: 0
Accepted
time: 0ms
memory: 3552kb
input:
3 1000000 -1000000 1000000 1000000 -1000000 -1000000 8943626655093 6304386590908
output:
37135266556187
result:
ok 1 number(s): "37135266556187"
Test #29:
score: 0
Accepted
time: 0ms
memory: 3664kb
input:
4 1000000 -1000000 1000000 1000000 1 0 -1000000 -1000000 7138388416478 453029559705
output:
23868194809139
result:
ok 1 number(s): "23868194809139"
Test #30:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
4 1000000 -1000000 1000000 1000000 0 1 -1000000 -1000000 5337445145160 4610262463095
output:
29960045043735
result:
ok 1 number(s): "29960045043735"
Test #31:
score: -100
Wrong Answer
time: 0ms
memory: 3608kb
input:
16 0 0 10 -5 20 0 30 15 40 0 45 10 40 20 25 30 40 40 30 45 20 40 10 25 0 40 -5 30 0 20 15 10 87 175
output:
3692
result:
wrong answer 1st numbers differ - expected: '3169', found: '3692'