Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
30464 | LYLAKIOI | 【S】T3 | C++ | 运行超时 | 54 | 1389 MS | 335004 KB | 1785 | 2024-07-19 15:51:14 |
#include<bits/stdc++.h> #define up(i,l,r) for(int i=(l);i<=(r);++i) #define down(i,l,r) for(int i=(l);i>=(r);--i) #define pi pair<int,int> #define p1 first #define p2 second #define m_p make_pair #define p_b push_back typedef long long ll; using namespace std; const int maxn=3e6+10,mod=1e9+7; inline ll read(){ ll x=0;short t=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')t=-1;ch=getchar();} while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar(); return x*t; } int n,a[maxn],ss[maxn];bool s[maxn]; int dfn[maxn],low[maxn],bel[maxn],scc,ct,stk[maxn],instk[maxn],tp; vector<int>v[maxn]; vector<int>st[maxn]; int nbel[maxn]; int pos[maxn]; int g(int x){ int res=0;up(i,0,19)if(!((x>>i)&1))res|=(1<<i); return res; } void dfs(int u){ dfn[u]=low[u]=++ct,instk[u]=1,stk[++tp]=u; for(int x:v[u]){ if(!dfn[x])dfs(x),low[u]=min(low[u],low[x]); else if(instk[x])low[u]=min(low[u],dfn[x]); } if(dfn[u]==low[u]){ ++scc; while(tp){ int x=stk[tp--];bel[x]=scc;instk[x]=0; ss[scc]+=s[x]; if(x==u)break; } } } void slv(){ n=read();up(i,1,n)a[i]=read(),s[a[i]]=1; up(i,0,(1<<20)-1)if(s[i])v[i].p_b(g(i)); up(i,1,(1<<20)-1){ down(j,19,0){ if(!((i>>j)&1))continue; int nxt=i^(1<<j); v[i].p_b(nxt); } } up(i,0,(1<<20)-1)if(!dfn[i])dfs(i); up(i,1,n)if(ss[bel[a[i]]]==1)nbel[i]=++scc;else nbel[i]=bel[a[i]]; up(i,1,n)st[nbel[i]].p_b(a[i]); up(i,1,scc)sort(st[i].begin(),st[i].end()); up(i,1,n){ printf("%d ",st[nbel[i]][pos[nbel[i]]++]); } }int main(){ slv(); fclose(stdin); fclose(stdout); return 0; }