c++

                                                      淘汰赛车

        赛车比赛在潘多拉星球变得越来越流行了。但是他们的比赛跟我们平常的不太一样:n 辆赛车在一条长长的直道上展开同台竞技。每辆赛车的速度都为 1m/s,整条赛道在每一米都有坐标标记。

在比赛的赛车中,赛车 i 从 0 秒开始由 ai 向 bi 移动。到达 bi 之后转而返回由 bi 向 ai 移动。循环往复。

又是蒜头菌!原来这是蒜头菌正在玩的一个手机小游戏。蒜头菌可以在某些位置放下 TNT 炸毁某些赛车。因为他有 m 个问题。其中,问题 j 是:在 tj 时刻,在 xi 到 yi 之间共有几辆赛车?

你的任务就是回答萌蒜头的问题。

输入

输入的第一行包含两个数字 n 和 m(1 ≤ n, m ≤ 1000),分别代表正在比赛的赛车数量和蒜头的问题数。

接下来的 n 行中,每行包含 2 个整数 ai、bi(0 ≤ ai, bi ≤ 109, ai != bi),分别代表赛车 i 的起点和终点。

再接下来的 m 行中,每行包含 3 个整数 xj,yj,tj(0 ≤ xj ≤ yj ≤ 109, 0 ≤ tj ≤ 109),分别代表问题 j 的左右坐标边界和询问的时间。

输出

输出共有 m 行,每行各有一个整数,分别代表对应的 m 个问题的答案。

样例1

输入:


0 1
0 2
2 3
3 5
4 5
0 5 0
0 1 2
0 2 1
2 5 2
2 5 3

输出:

5
1
2
4
3
 
#include <iostream>
using namespace std;
const int N = 1001;  
struct  racing 
{  
    int start,end,t,flag;  
}node[N]; 


struct compare{
	int left,right,time;
}; 


int main(int argc, char** argv) {
	 int n,m,ans;
	 compare com;
	 cin>>n>>m;  
    while(n>0&&m>0){  
        for(int i=0; i<n; i++){  
            cin>>node[i].start<<node[i].end;
            if(node[i].end<=node[i].start)  
                node[i].flag=0;  
            else{  
                node[i].flag=1;  
                int tmp=node[i].start;  
                node[i].start=node[i].end;  
                node[i].end=tmp;    
            }  
            node[i].t=node[i].end-node[i].start;  
        }  
        while(m--){  
            cin>>com.left>>com.right>>com.time;
            ans=0;  
            for(int i=0; i<n; i++){  
                int tmp=com.time/node[i].t+node[i].flag;  
                int res=com.time%node[i].t;  
                int loc;  
                if(tmp&2)  
                    loc=node[i].end-res;  
                else  
                    loc=node[i].start+res;  
                if(loc>=com.left&&loc<=com.right)  
                    ans++;  
            }  
            cout<<ans<<endl;  
        }  
    }  
	return 0;
}
分析题目:
      我们从题目中分析出对象即:赛车和问题集。接下来对每个赛车确定位置是最关键的一步,通过分析可以知道:询问的时间时赛车完成了往返的一个来回的整数倍时有local=node.start+res;否则就为local=node[i].end-res;从而判断是否在询问区间内。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值