codeforces 101 B. Buses

这个题目不错,但是一开始算数的时候竟然少算了一种情况导致,思路上跑偏了啊。

题目大意:给你n,m。m代表的是你有m中bus,每种bus只能从si走到ti,问你从0点到达n点可以有多少种方案可以选择。注意人只能坐车不可以走。

对于点ti来说,所有的方案就是前面的si到ti-1方案的和。这样就很简单了啊,但是数据很大需要离散化之后用线段树去维护这个区间的更新,就是需要求前面的和。

对于线段树竟然如此的生疏了,我也是醉了啊,以后得多写啊。

B. 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.

#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
#define M 1000100
#define LL long long
//#define LL long long
#define INF 0x3f3f3f
#define PI 3.1415926535898
#define mod 1000000007

const int maxn = 1000010;

using namespace std;

struct node
{
    int l, r;
    LL num;
}f[maxn];

struct node1
{
    LL x, y;
}p[maxn];
LL dx[maxn];
LL dy[maxn];
LL Max;
int ans;
int xans;

bool cmp(node1 a, node1 b)
{
    return a.y < b.y;
}

int Find(LL x)
{
    int l = 0;
    int r = xans-1;
    while(l <= r)
    {
        int mid = (l+r)>>1;
        if(dy[mid] == x) return mid;
        else if(dy[mid] > x) r = mid-1;
        else l = mid+1;
    }
    return -1;
}

void Bulid(int l, int r, int site)
{
    f[site].l = l;
    f[site].r = r;
    f[site].num = 0LL;
    if(l == r)
    {
        ///if(l == 0) f[site].num = 1LL;
        return;
    }
    int mid = (l+r)>>1;
    Bulid(l, mid, site<<1);
    Bulid(mid+1, r, site<<1|1);
}

LL Query(int l, int r, int site)
{
    if(f[site].l == l && f[site].r == r) return f[site].num;
    int mid = (f[site].l+f[site].r)>>1;
    if(r <= mid) return Query(l, r, site<<1);
    else if(l > mid) return Query(l, r, site<<1|1);
    else
    {
        LL x1 = Query(l, mid, site<<1)%mod;
        LL x2 =  Query(mid+1, r, site<<1|1)%mod;
        return (x1+x2)%mod;
    }
}

void Updata(int site, int m, LL d)
{
    if(f[site].l == f[site].r && f[site].l == m)
    {
        f[site].num += d;
        return;
    }
    int mid = (f[site].l+f[site].r)>>1;
    if(m <= mid) Updata(site<<1, m, d);
    else Updata(site<<1|1, m, d);
    f[site].num += d;
}

void cha(int x, int site)
{
    if(f[site].l == f[site].r && f[site].l == x)
    {
        Max = f[site].num;
        return;
    }
    int mid = (f[site].l+f[site].r)>>1;
    if(x <= mid) cha(x, site<<1);
    else cha(x, site<<1|1);
}

int main()
{
    int n, m;
    while(~scanf("%d %d",&n, &m))
    {
        ans = 0;
        for(int i = 0; i < m; i++)
        {
            scanf("%I64d %I64d",&p[i].x, &p[i].y);
            dx[ans++] = p[i].x;
            dx[ans++] = p[i].y;
        }
        dx[ans++] = n;
        sort(dx, dx+ans);
        sort(p, p+m, cmp);
        xans = 0;
        for(int i = 0; i < ans; i++)
            if(xans == 0 || dy[xans-1] != dx[i]) dy[xans++] = dx[i];
        if(dy[0] != 0)
        {
            cout<<0<<endl;
            continue;
        }
        Bulid(0, xans-1, 1);
        Updata(1, 0, 1);
        for(int i = 0; i < m; i++)
        {
            int x = Find(p[i].x);
            int y = Find(p[i].y);
            LL xx = Query(x, y-1, 1);
            ///cout<<"xx = "<<xx<<endl;
            xx %= mod;
            Updata(1, y, xx);
        }
        int nx = Find(n);
        cha(nx, 1);
        cout<<Max%mod<<endl;
    }
    return 0;
}
时间: 2024-10-08 01:26:56

codeforces 101 B. Buses的相关文章

codeforces 459C - Pashmak and Buses 【构造题】

题目:codeforces 459C - Pashmak and Buses 题意:给出n个人,然后k辆车,d天时间,然后每天让n个人选择坐一辆车去上学,要去d天不能有任意两个人乘同一辆车,不能的话输出 -1 分类:数学,构造 分析:这个题目首先得分析,我开始想到的是首先用相同的放在一起,比如 7 2 3 这样构造 1 1 1 1 2 2 2 1 1 1 2 2 2 1 1 1 2 2 2 1 1 1 2 2 2 1 1 1 就是需要的天数跟每一行出现次数最多的数的出现次数相等,但是发现还有更优

Codeforces Beta Round #79 (Div. 1 Only) B. Buses 树状数组

http://codeforces.com/contest/101/problem/B 给定一个数n,起点是0  终点是n,有m两车,每辆车是从s开去t的,我们只能从[s,s+1,s+2....t-1]处上车,从t处下车., 问能否去到点n,求方案数 设L[x]表示有多少辆车能够到达x处. 只能从t处下车:说明只能单点更新,对于没辆车x,在区间[s,s+1,s+2....t-1]内上车是可以得,那么有多少辆车呢?明显就是∑区间里能到达的点.然后单点更新t即可 数据大,明显不能到达的点是没用的,离

codeforces 459C Pashmak and Buses(模拟,组合数A)

题目 跑个案例看看结果就知道了:8 2 3 题目给的数据是 n,k,d 相当于高中数学题:k个人中选择d个人排成一列,有多少种不同的方案数,列出其中n中就可以了. #include<iostream> #include<algorithm> #include<string> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h>

Codeforces Round #261 (Div. 2)——Pashmak and Buses

题目链接 题意: n个人,k个车,d天.每一个人每天能够坐随意一个车.输出一种情况保证:不存在两个人,每天都在同一辆车上 (1?≤?n,?d?≤?1000; 1?≤?k?≤?109). 分析: 比赛中用的方法麻烦至极...基本想法是均分,这样答案肯定比較优.第一天分到同一辆车上的人在第二天再均分,一直到结束就可以 学习了别人的方法:一个人在所有d天中每天坐哪辆车,能够表示为d位k进制数x. 那么2个人每天都在同一辆车上,就是两个人的x相等.所以我们仅仅要构造出n个不同的d位k进制数即可 这种方法

Codeforces 101B Buses 排序+树状数组

题目链接:点击打开链接 当转移[l,r] 区间时, 若[0, r-1] 这里的区间都已经转移完毕时是最优的,所以按右端点升序,同理右端点相同时左端点升序,然后树状数组维护一下前缀和. #include <vector> #include <iostream> #include <algorithm> #include <string.h> #include <stdio.h> using namespace std; #define N 1000

Codeforces 459C Pashmak and Buses 机智数学题

这个题目说的是有n个人,有k辆巴士,有m天,每天都要安排n个人坐巴士(可以有巴士为空),为了使得这n个人不会成为朋友,只要每两个人在这m天里坐的巴士至少一天不相同即可. 要你求是否有这样的安排方法,如果有,输出具体的安排方案,每个人每天坐那辆车. 挺不错的题目,我压根没想到..真的,虽然知道之后惊呼原来如此简单,但一开始确实想岔了.现在一看这题目,很清晰,每个学生在这m天中坐的车辆,就会形成一个由m个数字组成的序列(数字为1-k代表巴士编号),按照题目要求,只需要学生的那个序列是独一无二的即可.

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store

Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题是线段树成段更新,但是不能直接更新,不然只能一个数一个数更新.这样只能把每个数存到一个数组中,长度大概是20吧,然后模拟二进制的位操作.仔细一点就行了. 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath>

Codeforces Gym 100269 Dwarf Tower (最短路)

题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game named "Dwarf Tower". In this game there are n different items,which you can put on your dwarf character. Items are numbered from 1 to n. Vasya want