AT_agc044_c [AGC044C] Strange Dance 题解

题目链接:[AGC044C] Strange Dance

乡下人 × 2。这次是低位在上。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<bits/stdc++.h>
using namespace std;
const int mxn=5e6+6;
int ch[mxn][3],tag[mxn],n,root,cnt,val[mxn];
string s;
inline void build(int&x,int dep,int id,int ml){
if(!x)x=++cnt;
if(dep==n){
val[x]=id;
return;
}
build(ch[x][0],dep+1,id,ml*3);
build(ch[x][1],dep+1,id+ml,ml*3);
build(ch[x][2],dep+1,id+ml*2,ml*3);
}
inline void pushdown(int x){
if(tag[x]){
tag[ch[x][0]]^=tag[x];
tag[ch[x][1]]^=tag[x];
tag[ch[x][2]]^=tag[x];
tag[x]=0;
swap(ch[x][1],ch[x][2]);
}
}
inline void go(int x,int dep){
if(dep==n)return;
pushdown(x);
swap(ch[x][1],ch[x][2]),swap(ch[x][0],ch[x][1]);
go(ch[x][0],dep+1);
}
inline void S(){tag[root]^=1;}
inline void R(){go(root,0);}
int ans[mxn],cc;
inline void get(int x,int dep,int id,int ml){
if(dep==n){
++cc;
ans[val[x]]=id;
return;
}
pushdown(x);
get(ch[x][0],dep+1,id,ml*3);
get(ch[x][1],dep+1,id+ml,ml*3);
get(ch[x][2],dep+1,id+ml*2,ml*3);
}
int main(){
ios_base::sync_with_stdio(false);
cin>>n>>s;
build(root,0,0,1);
for(int i=0;i<s.size();++i){
if(s[i]=='S')S();
else R();
}
get(root,0,0,1);
for(int i=0;i<cc;++i)cout<<ans[i]<<' ';
cout<<'\n';
}