zoukankan      html  css  js  c++  java
  • HDUOJ -----Color the ball

    Color the ball

    Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 7497    Accepted Submission(s): 3865


    Problem Description
    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
     
    Author
    8600
     
    Source
     
    简单的树状数组。
    依旧树状数组的原理,如果直接采用ope(a,b),显然会超时,而且若是在树状数组中划定界限来统计,图画的个数,这样这样明显是统计不到的,
    我们知道,树状数组是一个向上修改的的过程,然后向下统计求sum , 若果我们要夹出这个数值,那么我们必须要进行两次的修改,来最终确定有多少
     
    代码:
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 #define maxn  100000
     5 int aa[maxn+5];
     6 int nn;
     7 int lowbit(int x)
     8 {
     9   return x&(-x);
    10 }
    11 void ope(int x,int val)
    12 {
    13     while(x<=nn)
    14     {
    15         aa[x]+=val;
    16         x+=lowbit(x);
    17     }
    18 }
    19 int sum(int x)
    20 {
    21     int ans=0;
    22     while(x>0)
    23     {
    24         ans+=aa[x];
    25         x-=lowbit(x);
    26     }
    27     return ans;
    28 }
    29 int main()
    30 {
    31   int i,a,b;
    32   while(scanf("%d",&nn),nn)
    33   {
    34       memset(aa,0,sizeof(aa));
    35       for(i=0;i<nn;i++)
    36       {
    37           scanf("%d%d",&a,&b);
    38           ope(a,1);
    39           ope(b+1,-1);
    40       }
    41         printf("%d",sum(1));
    42       for(i=2;i<=nn;i++)
    43         printf(" %d",sum(i));
    44         putchar(10);
    45   }
    46     return 0;
    47 }
    View Code
  • 相关阅读:
    centos 下PATH变量配置错误补救办法 Alex
    基于php模块方式安装LAMP和常见LAMP应用 Alex
    php配置 Alex
    php测试小代码 Alex
    PHP简介 Alex
    2.7.JavaScriptnull与undefined
    2.9.JavaScript内置对象
    2.8.JavaScript不同数据类型转换
    2.2.javascript变量作用域
    2.6.Javascript数值型
  • 原文地址:https://www.cnblogs.com/gongxijun/p/3668401.html
Copyright © 2011-2022 走看看