rt,测试的时候显示栈溢出,而提交的时候是TLE,有没有大佬帮忙看看为什么会这样?
#include<bits/stdc++.h>
using namespace std;
const int maxn=5e6+10;
int m,q,x,cnt;
string s;
struct tree{
int opt,x,ls,rs;
}t[maxn<<2];
int build(int c){
cnt++;
if(s[c]!='x'){
t[cnt].opt=0;
while(c<s.length()&&s[c]<='9'&&s[c]>='0')
t[cnt].x=t[cnt].x*10+s[c++]-'0';
return c;
}
c++;
if(s[c]=='>')
t[cnt].opt=1;
else t[cnt].opt=2;
c++;
while(c<s.length()&&s[c]<='9'&&s[c]>='0')
t[cnt].x=t[cnt].x*10+s[c++]-'0';
c++;
int x=cnt;
t[x].ls=cnt+1;
int h=build(c);
t[x].rs=cnt+1;
return build(h+1);
}
int query(int p,int x){
if(!t[p].opt)
return t[p].x;
else if(t[p].opt==1){
if(x>t[p].x)
return query(t[p].ls,x);
else return query(t[p].rs,x);
}
else{
if(x<t[p].x)
return query(t[p].ls,x);
else return query(t[p].rs,x);
}
}
int main(){
//freopen("expr.in","r",stdin);
//freopen("expr.out","w",stdout);
cin>>m>>q>>s;
//cout<<s.length()<<"\n";
build(0);
while(q--){
cin>>x;
cout<<query(1,x)<<"\n";
}
return 0;
}