• hdu 1556 Color the ball


    http://acm.hdu.edu.cn/showproblem.php?pid=1556

    Color the ball

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


    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
     
    Recommend
    LL
    初学线段树,,,照着写,弱爆了!!!!!!!!
    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3 int n;
     4 struct Node
     5 {
     6     int l,r;
     7     int sum;
     8 }node[300001];
     9 void create_tree(int ll,int rr,int u)
    10 {
    11     node[u].l=ll;
    12     node[u].r=rr;
    13     node[u].sum=0;
    14     if(ll==rr) return;
    15     create_tree(ll,(ll+rr)/2,u*2);
    16     create_tree((ll+rr)/2+1,rr,u*2+1);
    17 }
    18 
    19 void update(int a,int b,int u)
    20 {
    21     
    22     if(node[u].l==a&&b==node[u].r)
    23     {
    24         node[u].sum++;
    25         return;
    26     }
    27     int mid=(node[u].l+node[u].r)/2;
    28     if(b<=mid) update(a,b,2*u);
    29     else if(a>=mid+1) update(a,b,2*u+1);
    30     else{
    31         
    32     update(a,mid,2*u);
    33     update(mid+1,b,2*u+1);
    34     }
    35     
    36 }
    37 void print(int u,int sum)
    38 {
    39 
    40     if (node[u].l==node[u].r) {printf("%d",sum+node[u].sum);
    41     if (node[u].l!=n) printf(" ");return;}  
    42     print(2*u,sum+node[u].sum);  
    43     print(2*u+1,sum+node[u].sum);   
    44 
    45 }
    46 int main()
    47 {
    48     
    49     int aa,bb;
    50     while(~scanf("%d",&n),n)
    51     {
    52         create_tree(1,n,1);
    53         for(int i=1;i<=n;i++)
    54         {
    55             scanf("%d%d",&aa,&bb);
    56             update(aa,bb,1);
    57         }
    58         print(1,0);
    59         printf("\n");
    60     }
    61 }
    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3 int a[100001],b[1000001],c[1000001];
     4 int main()
     5 {
     6     int n,x,y,i;
     7     while(~scanf("%d",&n),n)
     8     {int f=0;
     9         memset(a,0,sizeof(a));
    10         memset(b,0,sizeof(b));
    11         for(i=0;i<n;i++)
    12         {
    13             scanf("%d%d",&x,&y);
    14             a[x]++;//开始点等于x的个数 
    15             b[y]++;//终点为y的个数 
    16             
    17         }
    18         c[n]=b[n];//c[i]为要求的 
    19         for(i=n-1;i>=0;i--)
    20         c[i]=b[i]+c[i+1]-a[i+1];
    21         /*
    22          c[6]=b[6]+c[7]-a[7]
    23          6被涂的次数等于超过终点超过6且起始点不超过六加上b[6]的和 
    24         */ 
    25         for(i=1;i<=n;i++)
    26         {
    27             if(f) printf(" ");
    28             printf("%d",c[i]);
    29             f=1;
    30         }
    31         printf("\n");
    32         
    33     }
    34 }
  • 相关阅读:
    BUAA2020软工作业(三)——个人项目
    BUAA2020软工作业(二)——对软件工程的初步理解
    BUAA2020软工作业(一)——谈谈我和计算机的缘分
    OO第四单元总结与课程总结
    OO第三单元总结——JML
    面向对象第二单元总结——魔鬼电梯
    面向对象设计与构造——第一单元总结
    提问回顾与个人总结
    【技术博客】Flutter—使用网络请求的页面搭建流程、State生命周期、一些组件的应用
    软件案例分析
  • 原文地址:https://www.cnblogs.com/1114250779boke/p/2658934.html
Copyright © 2020-2023  润新知