zoukankan      html  css  js  c++  java
  • HRBUST 1909——理工门外的树——————【离线处理,差分前缀和】

    理工门外的树

    Time Limit: 1000 MS Memory Limit: 32768 KB

    64-bit integer IO format: %lld , %llu Java class name: Main

    [Submit] [Status] [Discuss]

    Description

    哈尔滨修地铁了~理工门口外长度为N的马路上有一排树,已知两棵树之间的距离都是1m。现在把马路看成是一个数轴,马路的一端在数轴0的位置,另一端在N的位置;数轴上的每个整数点,即0,1,2,……,L,都种有一棵树。马路上有一些区域要用来建地铁,这些区域用它们在数轴上的起始点和终止点表示。已知任一区域的起始点和终止点的坐标都是整数,区域之间可能有重合的部分。现在要把这些区域中的树(包括区域端点处的两棵树)移走。你的任务是计算将这些树都移走后,马路上还有多少棵树。 

    Input

    输入的第一行有两个整数N(1 <= N <= 1,000,000)和M(1 <= M <= 10,000),N代表马路的长度,M代表区域的数目,N和M之间用一个空格隔开。接下来的M行每行包含两个不同整数,用一个空格隔开,表示一个区域的起始点和终止点的坐标。 

    Output

    输出包括一行,这一行只包含一个整数,表示马路上剩余的树的数目。 

    Sample Input

    500 3

    150 300

    100 200

    470 471

    Sample Output

    298

    解题思路:差分前缀和,sum数组就是表示所有树的情况。然后遍历sum数组,如果小于0,说明该位置的树被拔掉。

    #include<stdio.h>
    #include<vector>
    #include<queue>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    const int maxn = 1e6+20;
    const int INF = 0x3f3f3f3f;
    const int mod = 1e9+7;
    int sum[maxn],a[maxn];
    int main(){
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF){
            memset(sum,0,sizeof(sum));
            memset(a,0,sizeof(a));
            int l,r;
            for(int i = 1; i <=m; i++){
                scanf("%d%d",&l,&r);
                if(l > r) swap(l,r);
                a[l+1] -= 1;
                a[r+1+1] += 1;
            }
            int ans = 0;
            for(int i = 1; i <= n+1; i++){
                sum[i] = sum[i-1] + a[i];
            }
            for(int i = 1; i <= n+1; i++){
                if(sum[i] < 0){
                    ans++;
                }
            }
            printf("%d
    ",n+1-ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    python-历史
    10-函数命名空间,作用域,嵌套,闭包
    centos7 搭建dns服务器
    centos7 搭建dhcp服务器
    Nginx 启用 gzip 压缩
    Eclipse 个人手册
    Nginx 命令
    定时任务
    系统设计
    根据 xsd 生成 jaxb java 类
  • 原文地址:https://www.cnblogs.com/chengsheng/p/5336731.html
Copyright © 2011-2022 走看看