QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#449482 | #8523. Puzzle II | Fesdrer | ML | 1ms | 7824kb | C++14 | 3.1kb | 2024-06-21 12:16:39 | 2024-06-21 12:16:40 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=300005;
int n,L,op,blacknum;
char oria[N],orib[N];
class FHQTreap{
public:
struct TreeNode{
int ls,rs,val,cnt,randnum,siz;
}tr[N<<2];
int root,tot;
FHQTreap(){
root=tot=0;
srand(0);
}
inline int New(int val){
tr[++tot]={0,0,val,val,rand(),1};
return tot;
}
inline void pushup(int p){
tr[p].cnt=tr[tr[p].ls].cnt+tr[tr[p].rs].cnt+tr[p].val;
tr[p].siz=tr[tr[p].ls].siz+tr[tr[p].rs].siz+1;
}
inline void split(int p,int key,int &pr,int &qr){
if(!p){
pr=qr=0;
return;
}
if(tr[tr[p].ls].siz+1<=key) pr=p,split(tr[p].rs,key-tr[tr[p].ls].siz-1,tr[p].rs,qr);
else qr=p,split(tr[p].ls,key,pr,tr[p].ls);
pushup(p);
}
inline int merge(int p,int q){
if(!p||!q) return p|q;
if(tr[p].randnum>tr[q].randnum){
tr[p].rs=merge(tr[p].rs,q);
pushup(p);
return p;
}
else{
tr[q].ls=merge(p,tr[q].ls);
pushup(q);
return q;
}
}
inline void find(int p,int &pr,int &qr){
// cout<<p<<endl;
if(!p){
pr=qr=0;
}
if(tr[p].val) pr=p,qr=tr[p].rs,tr[p].rs=0;
else if(tr[tr[p].ls].cnt) qr=p,find(tr[p].ls,pr,tr[p].ls);
else pr=p,find(tr[p].rs,tr[p].rs,qr);
pushup(p);
}
// void dfs(int x){
// cout<<x<<"("<<tr[x].val<<"):"<<tr[x].ls<<" "<<tr[x].rs<<endl;
// if(tr[x].ls) dfs(tr[x].ls);
// if(tr[x].rs) dfs(tr[x].rs);
// }
inline void turn_pre(int p,int q){
if(tr[p].siz>=L){
int l,r;
split(p,tr[p].siz-L,l,r);
root=merge(merge(l,New(0)),merge(r,q));
}
else{
int l,mid,r;
split(q,tr[q].siz-1,l,r);
split(l,tr[l].siz-L+tr[p].siz+1,l,mid);
root=merge(merge(merge(r,p),l),merge(New(0),mid));
// cout<<"-----------"<<endl;
// dfs(root);
// cout<<"-----------"<<endl;
}
}
inline void turn_nxt(int p,int q){
if(tr[q].siz>=L-1){
int l,r;
split(q,L-1,l,r);
root=merge(merge(p,l),merge(New(0),r));
}
else{
int l,mid,r;
split(p,L-1-tr[q].siz,l,r);
split(l,1,l,mid);
root=merge(merge(merge(mid,New(0)),r),merge(p,l));
}
}
}a,b;
void check_if_swap(){
int cnt=0;
for(int i=1;i<=n;i++) if(oria[i]=='C') cnt++;
if(cnt*2>n){
for(int i=1;i<=n;i++) swap(oria[i],orib[i]);
op=1;
}
}
void in_treap(){
a.root=a.New((oria[1]=='C')),b.root=b.New((orib[1]=='B')),blacknum=0;
for(int i=1;i<=n;i++) if(oria[i]=='C') blacknum++;
for(int i=2;i<=n;i++) a.root=a.merge(a.root,a.New((oria[i]=='C')));
for(int i=2;i<=n;i++) b.root=b.merge(b.root,b.New((orib[i]=='B')));
}
void solve(){
int al,amid,ar,bl,bmid,br;
a.find(a.root,al,ar),b.find(b.root,bl,br);
int x=a.tr[al].siz,y=b.tr[bl].siz;
// cout<<x<<" "<<y<<endl;
x-=L;
if(x<=0) x+=n;
if(op) cout<<y<<" "<<x<<endl;
else cout<<x<<" "<<y<<endl;
x++;
if(x>n) x-=n;
if(op) cout<<y<<" "<<x<<endl;
else cout<<x<<" "<<y<<endl;
a.split(al,a.tr[al].siz-1,al,amid);
b.split(bl,b.tr[bl].siz-1,bl,bmid);
a.turn_pre(al,ar);
b.turn_nxt(bl,br);
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>L>>(oria+1)>>(orib+1);
check_if_swap();
in_treap();
cout<<blacknum*2<<endl;
while(blacknum--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 7676kb
input:
6 3 BCCBCC BBCBBC
output:
4 1 6 1 1 4 4 4 5
result:
ok moves = 4
Test #2:
score: 0
Accepted
time: 1ms
memory: 5784kb
input:
2 1 BC BC
output:
2 1 1 2 1
result:
ok moves = 2
Test #3:
score: 0
Accepted
time: 1ms
memory: 5776kb
input:
2 1 BB CC
output:
0
result:
ok moves = 0
Test #4:
score: 0
Accepted
time: 1ms
memory: 7680kb
input:
2 1 CC BB
output:
0
result:
ok moves = 0
Test #5:
score: 0
Accepted
time: 1ms
memory: 7732kb
input:
3 1 CCC BBB
output:
0
result:
ok moves = 0
Test #6:
score: 0
Accepted
time: 1ms
memory: 7740kb
input:
3 1 CBC BCB
output:
2 2 1 2 2
result:
ok moves = 2
Test #7:
score: 0
Accepted
time: 1ms
memory: 7824kb
input:
3 2 BBB CCC
output:
0
result:
ok moves = 0
Test #8:
score: 0
Accepted
time: 1ms
memory: 5712kb
input:
3 2 BCB BCC
output:
2 3 1 1 1
result:
ok moves = 2
Test #9:
score: -100
Memory Limit Exceeded
input:
4 2 CCCB BBCB
output:
2 4 1 4 2