题意:有一群骑士比赛 每次给定一个序列从a到b决斗 其中w赢了 最后问打败每个骑士的是哪个骑士
思路:初看这题就想起了从盆子中倒水的题
用num数组储存打败每个骑士的骑士编号 用pre数组储存下一个没被打败的骑士的下标
#include <iostream>
#include <cstdio>
#include <cmath>
#define max 333333
using namespace std;
int num[max],pre[max];
int change(int a,int b,int w)
{
if(a>b||a==-1);
else if(num[a]==0)
{
if(w!=a)
num[a]=w;
change(pre[a],b,w);
return a;
}
else
return pre[a]=change(pre[a],b,w);
}
int main()
{
int n,m,i;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
pre[i]=i+1;
pre[n]=-1;
while(m--)
{
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
change(a,b,w);
}
for(i=1;i<=n;i++)
printf("%d ",num[i]);
return 0;
}