快读
template<typename T>void in(T &a)
{
T ans=0;
bool f=0;
char c=getchar();
for(;c<'0'||c>'9';c=getchar())if(c=='-')f=1;
for(;c>='0'&&c<='9';c=getchar())ans=ans*10+c-'0';
a=f?-ans:ans;
}
template<typename T,typename... Args>void in(T &a,Args&...args)
{
in(a),in(args...);
}
非负数快读
template<typename T>void in(T &a)
{
T ans=0;
char c=getchar();
for(;c<'0'||c>'9';c=getchar());
for(;c>='0'&&c<='9';c=getchar())ans=ans*10+c-'0';
a=ans;
}
template<typename T,typename... Args>void in(T &a,Args&...args)
{
in(a),in(args...);
}
fread快读
char buf1[2097152],*ip1=buf1,*ip2=buf1;
inline int getc(){
return ip1==ip2&&(ip2=(ip1=buf1)+fread(buf1,1,2097152,stdin),ip1==ip2)?EOF:*ip1++;
}
template<typename T>void in(T &a)
{
T ans=0;
bool f=0;
char c=getc();
for(;c<'0'||c>'9';c=getc())if(c=='-')f=1;
for(;c>='0'&&c<='9';c=getc())ans=ans*10+c-'0';
a=(f?-ans:ans);
}
template<typename T,typename... Args>void in(T &a,Args&...args)
{
in(a),in(args...);
}
非负数fread快读
char buf1[2097152],*ip1=buf1,*ip2=buf1;
inline int getc(){
return ip1==ip2&&(ip2=(ip1=buf1)+fread(buf1,1,2097152,stdin),ip1==ip2)?EOF:*ip1++;
}
template<typename T>void in(T &a)
{
T ans=0;
char c=getc();
for(;c<'0'||c>'9';c=getc());
for(;c>='0'&&c<='9';c=getc())ans=ans*10+c-'0';
a=ans;
}
template<typename T,typename... Args>void in(T &a,Args&...args)
{
in(a),in(args...);
}
快写
template<typename T>void __out(T x)
{
(x>9)&&(__out(x/10),0),putchar(x%10+48);
}
template<typename T>void out(T x)
{
if(x<0)x=-x,putchar('-');
__out(x);
}
加速 cin 和 cout
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
手动吸氧
#pragma GCC optimize("Ofast,unroll-loops")
离散化
void rebuild(int n,int a[],int cnt=0){
int temp[n+5];
for(int i=1;i<=n;i++)temp[i]=a[i];
sort(temp+1,temp+n+1),cnt=unique(temp+1,temp+n+1)-temp-1;
for(int i=1;i<=n;i++)a[i]=lower_bound(temp+1,temp+cnt+1,a[i])-temp;
}
__gnu_pbds
//哈希表
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
__gnu_pbds::gp_hash_table<int,int>a;
//优先队列
#include<ext/pb_ds/priority_queue.hpp>
__gnu_pbds::priority_queue<int,greater<int>,__gnu_pbds::pairing_heap_tag>a;//配对堆
__gnu_pbds::priority_queue<int,greater<int>,__gnu_pbds::thin_heap_tag>b;//冗余计数二项堆
__gnu_pbds::priority_queue<int,greater<int>,__gnu_pbds::binomial_heap_tag>c;//二项堆
__gnu_pbds::priority_queue<int,greater<int>,__gnu_pbds::rc_binomial_heap_tag>d;//斐波那契堆
__gnu_pbds::priority_queue<int,greater<int>,__gnu_pbds::binary_heap_tag>e;//二叉堆
__gnu_pbds::priority_queue<int,greater<int>,__gnu_pbds::TAG>::point_iterator it;//迭代器
DLX
int head[n+5],sz[m+5],l[k+5],r[k+5],u[k+5],d[k+5],cnt,x[k+5],y[k+5],num,ans[n+5];
void build(int b){
cnt=b;
for(int i=0;i<=b;i++)l[i]=i?i-1:b,r[i]=i==b?0:i+1,u[i]=d[i]=i;
}
void insert(int a,int b){
sz[b]++,cnt++,x[cnt]=a,y[cnt]=b,u[cnt]=b,d[cnt]=d[b],u[d[cnt]]=d[u[cnt]]=cnt;
if(!head[a])head[a]=l[cnt]=r[cnt]=cnt;
else l[cnt]=head[a],r[cnt]=r[head[a]],l[r[cnt]]=r[l[cnt]]=cnt;
}
void remove(int a){
l[r[a]]=l[a],r[l[a]]=r[a];
for(int i=d[a];i!=a;i=d[i])for(int j=r[i];j!=i;j=r[j])u[d[j]]=u[j],d[u[j]]=d[j],sz[y[j]]--;
}
void recover(int a){
for(int i=u[a];i!=a;i=u[i])for(int j=l[i];j!=i;j=l[j])u[d[j]]=d[u[j]]=j,sz[y[j]]++;
l[r[a]]=a,r[l[a]]=a;
}
bool dance(){
int pos=r[0];
if(!pos)return 1;
for(int i=r[0];i;i=r[i])if(sz[i]<sz[pos])pos=i;
remove(pos),num++;
for(int i=d[pos];i!=pos;i=d[i]){
ans[num]=x[i];
for(int j=r[i];j!=i;j=r[j])remove(y[j]);
if(dance())return 1;
for(int j=l[i];j!=i;j=l[j])recover(y[j]);
}
return recover(pos),num--,0;
}