题解的大佬全是平衡树、线段树,其实这题暴力就能过。
代码如下:
#include<bits/stdc++.h>
using namespace std;
int a[50005],n,m;
stack<int>t;//需要修复的房子
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)a[i]=1;//不需要用scanf
for(int i=1;i<=m;i++){
char c;
int b;
cin>>c;
if(c=='R'){
if(!t.empty()){//如果有需要修复的房子就修复
a[t.top()]=1;
t.pop();
}
}
else{
cin>>b;
if(c=='D'){
a[b]=0;
t.push(b);//房子设为摧毁,加入栈
}
else{//为"Q"时
if(a[b]==0){
cout<<0<<endl;
continue;//没有房子可去
}
else{
int s=1;
for(int i=b-1;i>=1;i--){
if(a[i]==0)break;
s++;
}
for(int i=b+1;i<=n;i++){
if(a[i]==0)break;
s++;
}//将左右和本身可到达的相加输出
cout<<s<<endl;
}
}
}
}
return 0;
}