zoukankan      html  css  js  c++  java
  • Color the ball(差分数组)

    N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?

    Input每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。
    当N = 0,输入结束。Output每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。Sample Input

    3
    1 1
    2 2
    3 3
    3
    1 1
    1 2
    1 3
    0

    Sample Output

    1 1 1
    3 2 1
    差分数组和前缀和是互逆的
    #pragma GCC optimize(2)
    #include<bits/stdc++.h>
    using namespace std;
    inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;}
    typedef long long ll;
    const int maxn=1e6+10;
    const int M=1e7+10;
    const int INF=0x3f3f3f3f;
    int a[maxn];
    int b[maxn];
    int main()
    {
        int n;
        while(~scanf("%d",&n)){
            if(n==0){
                break;
            } 
            memset(a,0,sizeof(a));
            memset(b,0,sizeof(b));
            int x,y;
            for(int i=1;i<=n;i++){
                cin>>x>>y;
                b[x]++;
                b[y+1]--;
            }
            for(int i=1;i<=n;i++){
                a[i]=a[i-1]+b[i];
            }
            for(int i=1;i<n;i++){
                printf("%d ",a[i]);
            }
            printf("%d
    ",a[n]);
        }
        return 0;
    }
  • 相关阅读:
    dojo自定义Widget
    奇怪的JS
    Dojo Widget系统(转)
    JS 中Promise 模式
    Structs 原理图
    ArcGIS Engine Style文件操作
    dojo.hitch 原理
    Android:解决cannot find zipalign的问题
    Bootstrap:解决Bootstrap下拉框需要双击才能打开的问题
    Clojure:添加gzip功能
  • 原文地址:https://www.cnblogs.com/lipu123/p/12194492.html
Copyright © 2011-2022 走看看