CF round #79 div2 D bues

本文深入探讨了信息技术领域的关键概念和技术,包括前端开发、后端开发、移动开发、游戏开发、大数据开发、开发工具等多个细分领域。文章通过具体实例阐述了每个领域的核心技术和实践方法,旨在帮助读者全面理解信息技术的复杂性和多样性。

 http://codeforces.com/contest/102/problem/D

                                                                                                                                                                                            D. Buses
                                                                                                                                                                                     time limit per test
                                                                                                                                                                                          2 seconds
                                                                                                                                                                                   memory limit per test
                                                                                                                                                                                        265 megabytes
                                                                                                                                                                                             input
                                                                                                                                                                                     standard input
                                                                                                                                                                                              output
                                                                                                                                                                                     standard output

Little boy Gerald studies at school which is quite far from his house. That's why he has to go there by bus every day. The way from home to school is represented by a segment of a straight line; the segment contains exactly n + 1 bus stops. All of them are numbered with integers from 0 to n in the order in which they follow from Gerald's home. The bus stop by Gerald's home has number 0 and the bus stop by the school has number n.

There are m buses running between the house and the school: the i-th bus goes from stop si to ti (si < ti), visiting all the intermediate stops in the order in which they follow on the segment. Besides, Gerald's no idiot and he wouldn't get off the bus until it is still possible to ride on it closer to the school (obviously, getting off would be completely pointless). In other words, Gerald can get on the i-th bus on any stop numbered from si to ti - 1 inclusive, but he can get off the i-th bus only on the bus stop ti.

Gerald can't walk between the bus stops and he also can't move in the direction from the school to the house.

Gerald wants to know how many ways he has to get from home to school. Tell him this number. Two ways are considered different if Gerald crosses some segment between the stops on different buses. As the number of ways can be too much, find the remainder of a division of this number by 1000000007 (109 + 7).

Input

The first line contains two space-separated integers: n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 105). Then follow m lines each containing two integers si, ti. They are the numbers of starting stops and end stops of the buses (0 ≤ si < ti ≤ n).

Output

Print the only number — the number of ways to get to the school modulo 1000000007 (109 + 7).

Sample test(s)
Input
2 2
0 1
1 2
Output
1
Input
3 2
0 1
1 2
Output
0
Input
5 5
0 1
0 2
0 3
0 4
0 5
Output
16
Note

The first test has the only variant to get to school: first on bus number one to the bus stop number one; then on bus number two to the bus stop number two.

In the second test no bus goes to the third bus stop, where the school is positioned. Thus, the correct answer is 0.

In the third test Gerald can either get or not on any of the first four buses to get closer to the school. Thus, the correct answer is 24 = 16.

题意:有m条公交路线,问你有多少中方案从0到n,每条公交路线的描述为s,t:s为起点,t为终点,可以在除终点外的任意站上车即[s,t-1]间的站,但只能在终点下车。

分析:树状数组+DP,f[t]表示到达t站的方案数,按t对公交路线排序,对于当前的公交车,假设起点站和终点站分别为s,t,那么对于区间[s,t-1]站内上车的都可以到达t,那么查询[s,t-1]之间有的所有方案数的和可以用树状数组求得并维护。由于n>>m所以离散化。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define M 100050
#define mod 1000000007
struct node
{
	int s,t;
}a[M];
int j,pos[M+M],f[M+M],c[M+M];
int lowbit(int x)
{
	return x&(-x);
}
void update(int i,int val)
{
	while(i<=j-1)
	{
		c[i]=(c[i]+val)%mod;
		i+=lowbit(i);
	}
}
int query(int i)
{
	int sum=0;
	while(i>0)
	{
		sum=(sum+c[i])%mod;
		i-=lowbit(i);
	}
	return sum;
}
int bin(int left,int right,int key)
{
	while(left<=right)
	{
		int mid=(left+right)>>1;
		if(pos[mid]==key)
			return mid;
		else if(pos[mid]<key)
			left=mid+1;
		else
			right=mid-1;
	}
	return -1;
}
bool cmp(node a,node b)
{
	return a.t<b.t;
}
int main()
{
	int n,m,i,k,s,t,l,r;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		memset(c,0,sizeof(c));
		memset(f,0,sizeof(f));
		for(i=0,k=0;i<m;i++)
		{
			scanf("%d%d",&a[i].s,&a[i].t);
			pos[k++]=a[i].s;
			pos[k++]=a[i].t;
		}
		sort(pos,pos+k);
		sort(a,a+m,cmp);
		for(i=0,j=0;i<k;i++)
			if(i==0||pos[i]!=pos[i-1])
				pos[j++]=pos[i];
		for(i=0;i<m;i++)
		{
			s=a[i].s;t=a[i].t;
			l=bin(0,j-1,s);
			r=bin(0,j-1,t);
			int temp=((query(r-1)-query(l-1)+mod)%mod)%mod;
			if(s==0) temp=(temp+1)%mod;
			update(r,temp);
			f[r]=(f[r]+temp)%mod;
		}
		t=bin(0,j-1,n);
		if(t==-1)
			printf("0\n");
		else
			printf("%d\n",f[t]);
	}
	return 0;
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值