不知道为什么编译器老说错。。。。
int n,x,y,maxx;
struct point{
int l,r,h;
bool friend operator<(point a,point b){
return a.h<b.h;
}
};
point node[mxn];
int dp[mxn][2];
int dfs(int cur,int x,int time){
if(node[cur].h==0)return time;
if(dp[cur][0]!=inf||dp[cur][1]!=inf)return min(dp[cur][0],dp[cur][1]);
int up=lower_bound(node,node+1+n,node[cur].h)-node;
int down=lower_bound(node,node+1+n,node[cur].h-maxx)-node;
int flagl=0,flagr=0;
for(int i=up;i>=down;--i){
if(node[cur].h-node[i].h>maxx)continue;
if(node[cur].l<=node[i].l&&node[cur].l<=node[i].r&&!flagl){
dp[cur][0]=min(dp[cur][0],dfs(i,node[cur].l,time+node[cur].h-node[cur].h+x-node[cur].l)); flagl=1;
}
if(node[cur].r<=node[i].l&&node[cur].r<=node[i].r&&!flagr){
dp[cur][1]=min(dp[cur][1],dfs(i,node[cur].r,time+node[cur].h-node[cur].h+abs(x-node[cur].r)));flagr=1;
}
}
return min(dp[cur][0],dp[cur][1]);
}
int main(){
int T;sf("%d",&T);
while(T--){
sf("%d%d%d%d",&n,&x,&y,&maxx);
mem(dp,inf);
node[0].l=node[0].r=x;node[0].h=y;
rep(i,1,n){
sf("%d%d%d",&node[i].l,&node[i].r,&node[i].h);
}
node[n+1].l=0,node[n+1].r=0,node[n+1].h=0;
sort(node,node+1+n);
dfs(n+1,x,0);
pf("%d\n",dp[0][0]);
}
}