QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#449499#8523. Puzzle IIFesdrerML 0ms3916kbC++142.9kb2024-06-21 12:30:002024-06-21 12:30:00

Judging History

This is the latest submission verdict.

  • [2024-06-21 12:30:00]
  • Judged
  • Verdict: ML
  • Time: 0ms
  • Memory: 3916kb
  • [2024-06-21 12:30:00]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
const int N=300005;
bool st;
int n,L,op,blacknum;
char oria[N],orib[N];
class FHQTreap{
public:
	struct TreeNode{
		int ls,rs,val,cnt,randnum,siz;
	};
	int root;
	vector<TreeNode> tr;
	FHQTreap(){
		root=0;
		tr.assign(1,(TreeNode){0,0,0,0,0,0});
		srand((unsigned int)time(0));
	}
	inline int New(int val){
		tr.push_back((TreeNode){0,0,val,val,rand(),1});
		return int(tr.size())-1;
	}
	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){
		if(!p){
			pr=qr=0;
			return;
		}
		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);
	}
	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));
		}
	}
	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;
bool ed;
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;
	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: 3912kb

input:

6 3
BCCBCC
BBCBBC

output:

4
1 3
1 4
4 1
4 2

result:

ok moves = 4

Test #2:

score: 0
Accepted
time: 0ms
memory: 3708kb

input:

2 1
BC
BC

output:

2
1 1
2 1

result:

ok moves = 2

Test #3:

score: 0
Accepted
time: 0ms
memory: 3700kb

input:

2 1
BB
CC

output:

0

result:

ok moves = 0

Test #4:

score: 0
Accepted
time: 0ms
memory: 3700kb

input:

2 1
CC
BB

output:

0

result:

ok moves = 0

Test #5:

score: 0
Accepted
time: 0ms
memory: 3636kb

input:

3 1
CCC
BBB

output:

0

result:

ok moves = 0

Test #6:

score: 0
Accepted
time: 0ms
memory: 3916kb

input:

3 1
CBC
BCB

output:

2
2 1
2 2

result:

ok moves = 2

Test #7:

score: 0
Accepted
time: 0ms
memory: 3628kb

input:

3 2
BBB
CCC

output:

0

result:

ok moves = 0

Test #8:

score: 0
Accepted
time: 0ms
memory: 3636kb

input:

3 2
BCB
BCC

output:

2
3 1
1 1

result:

ok moves = 2

Test #9:

score: 0
Accepted
time: 0ms
memory: 3708kb

input:

4 2
CCCB
BBCB

output:

2
4 1
4 2

result:

ok moves = 2

Test #10:

score: -100
Memory Limit Exceeded

input:

9 6
CCCBCCCBB
BBBCBBBCC

output:

6
4 7
4 8
7 4
7 5
18 4
18 5

result: