1 solutions
-
0
/* * __----~~~~~~~~~~~------___ * . . ~~//====...... __--~ ~~ * -. \_|// |||\\ ~~~~~~::::... /~ * ___-==_ _-~o~ \/ ||| \\ _/~~- * __---~~~.==~||\=_ -_--~/_-~|- |\\ \\ _/~ * _-~~ .=~ | \\-_ '-~7 /- / || \ / * .~ .~ | \\ -_ / /- / || \ / * / ____ / | \\ ~-_/ /|- _/ .|| \ / * |~~ ~~|--~~~~--_ \ ~==-/ | \~--===~~ .\ * ' ~-| /| |-~\~~ __--~~ * |-~~-_/ | | ~\_ _-~ /\ * / \ \__ \/~ \__ * _--~ _/ | .-~~____--~-/ ~~==. * ((->/~ '.|||' -_| ~~-/ , . _|| * -_ ~\ ~~---l__i__i__i--~~_/ * _-~-__ ~) \--______________--~~ * //.-~~~-~_--~- |-------~~~~~~~~ * //.-~~~--\ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * 神兽保佑 永无BUG */ /* * @Description: I want to be the weakest vegetable in the world! * @Author: I.Z. */ #include<bits/stdc++.h> using namespace std; #define MOD 998244353 #define speMOD 2933256077ll #define pii pair<int,int> #define all(v) v.begin(),v.end() #define pb push_back #define REP(i,b,e) for(int i=(b);i<(int)(e);++i) #define over {putchar('0');puts("");return;} #define lowbit(x) ((x)&(-(x))) #define cntbit(x) __builtin_popcount(x) #define rf(v) shuffle(all(v),sd); #define deal(v) sort(all(v));v.erase(unique(v.begin(),v.end()),v.end()) #define lbound(v,x) lower_bound(all(v),x)-v.begin() mt19937 sd(random_device{}()); int qpow(int a,int b,int m=MOD,int res=1){ a%=m; while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1; return res; } string YN(bool x,string Y="Yes",string N="No") { if(x){ return Y; } else { return N; } } int exgcd(int x,int y,int &a,int &b){ if(y==0){ a=1;b=0; return x; } int d=exgcd(y,x%y,a,b); int z=b; b=a-b*(x/y); a=z; return d; } int _x_,_y_; inline int inverse(int x,int y=MOD){ exgcd(x,y,_x_,_y_); return (_x_+y)%y; } int fac[2000005],inv[2000005]; void init(int n){ fac[0]=inv[0]=1; REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD; inv[n]=qpow(fac[n],MOD-2); for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD; } int binom(int x,int y){ return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD; } int read(){ int res=0;char c=getchar(); while(c<48||c>57)c=getchar(); do res=(res<<1)+(res<<3)+(c^48),c=getchar();while(c>=48&&c<=57); return res; } int n; int a[500005]; int fa[1000005]; int f[500005]; int findfa(int x){return fa[x]==x? x:fa[x]=findfa(fa[x]);} int findfa2(int x){return f[x]==x? x:f[x]=findfa2(f[x]);} bool merge(int x,int y,int op){ fa[findfa(y*2+op)]=findfa(x*2); fa[findfa(y*2+1-op)]=findfa(x*2+1); return findfa(x*2)==findfa(x*2+1); } struct segment{ int l,r,id; }c[500005]; int col[500005],tim; int s[2][500005]; void output(){ tim-=clock(); int tp[2]={0,0}; int cur=0,tot=0; REP(i,0,n*2){ if(tot<n&&col[tot]==0&&(!tp[0]||s[0][tp[0]-1]>a[tot])){ putchar('a'); s[0][tp[0]++]=a[tot++]; }else if(tp[0]&&s[0][tp[0]-1]==cur){ putchar('b');--tp[0];++cur; }else if(tot<n&&col[tot]==1&&(!tp[1]||s[1][tp[1]-1]>a[tot])){ putchar('c'); s[1][tp[1]++]=a[tot++]; }else{ putchar('d'); --tp[1];++cur; } putchar(' '); } puts("");tim+=clock(); } vector<int>seg[2000005]; void build(int l,int r,int p){ seg[p].clear(); if(l==r)return; int m=(l+r)>>1; build(l,m,p*2+1);build(m+1,r,p*2+2); } void update(int l,int r,int s,int t,int p,int val){ if(l<=s&&t<=r){ seg[p].pb(val); return; } int m=(s+t)>>1; if(m>=l)update(l,r,s,m,p*2+1,val); if(m<r)update(l,r,m+1,t,p*2+2,val); } vector<int>own; bitset<500005>vis; void query(int pos,int l,int r,int p){ vector<int>cur; for(auto i:seg[p]){if(!vis[findfa2(i)])vis[f[i]]=1,cur.pb(f[i]);} seg[p]=cur; for(auto i:seg[p])own.pb(i); if(l==r)return; int m=(l+r)>>1; if(m>=pos)query(pos,l,m,p*2+1); else query(pos,m+1,r,p*2+2); } void Main() { n=read(); REP(i,0,n)a[i]=read()-1; REP(i,0,n*2)fa[i]=i; REP(i,0,n)f[i]=i; vector<segment>v1,v2; int mn=a[n-1]; build(0,n-1,0); for(int i=n-2;i>=0;--i){ own.clear(); query(a[i],0,n-1,0); for(auto j:own)vis[j]=0; if(own.size()){ if(merge(i,c[own[0]].id,1))over; for(auto j:own)if(j!=own[0]){ if(merge(c[own[0]].id,c[j].id,0))over f[findfa2(j)]=findfa2(own[0]); } } c[i]={mn,a[i],i};update(mn,a[i],0,n-1,0,i); mn=min(mn,a[i]); } REP(i,0,n)if(findfa(i*2)==findfa(i*2+1))over REP(i,0,n)col[i]=-1; REP(i,0,n)if(col[i]==-1){ int x=findfa(i*2); if(x/2==i){ col[i]=0; continue; } if(x&1){ if(col[x>>1]==-1)col[x>>1]=1,col[i]=0; else col[i]=col[x>>1]^1; }else{ if(col[x>>1]==-1)col[x>>1]=col[i]=0; else col[i]=col[x>>1]; } } output(); } void TC() { int tc=read(); while(tc--){ Main(); cout.flush(); } } signed main() { // system("fc 1.out sort2.ans");return 0; freopen("sort.in","r",stdin); freopen("sort.out","w",stdout); return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0; } /* 1. CLEAR the arrays (ESPECIALLY multitests) 2. DELETE useless output */
- 1
Information
- ID
- 2943
- Time
- 2000ms
- Memory
- 1024MiB
- Difficulty
- 10
- Tags
- (None)
- # Submissions
- 1
- Accepted
- 1
- Uploaded By