inline void build(){
for(int i=1;i<=n;i++){
int pos=top;
while(top&&a[st[pos]]>a[i])
--pos;
if(pos<top) ls[i]=rs[st[pos]];
if(pos) rs[st[pos]]=i;
st[++pos]=i;
top=pos;
}
}
inline void build(){
for(int i=1;i<=n;i++){
int pos=top;
while(top&&a[st[pos]]>a[i])
--pos;
if(pos<top) ls[i]=st[pos+1];
if(pos) rs[st[pos]]=i;
st[++pos]=i;
top=pos;
}
}
st数组为维护右子树链的单调栈,那为什这两种写法中写法一会错?
我在过程中输出了 st[pos+1] 和 rs[st[pos]] 他们的值是一样的啊。
inline void build(){
for(int i=1;i<=n;i++){
int pos=top;
while(top&&a[st[pos]]>a[i])
--pos;
printf("+1:%d rs:%d\n",st[pos+1],rs[st[pos]]);
if(pos<top) ls[i]=rs[st[pos]];
if(pos) rs[st[pos]]=i;
st[++pos]=i;
top=pos;
}
}
实在搞不懂了,感谢各位解惑