zoukankan      html  css  js  c++  java
  • hdu 1556 Color the ball(线段树区间维护+单点求值)

    传送门:Color the ball

    Color the ball

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


    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

    题解:

    线段树区间维护(水水水)

    代码:

    #include <stdio.h>
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    #include <deque>
    #include <iomanip>
    #include <iostream>
    #include <list>
    #include <map>
    #include <queue>
    #include <set>
    #include <utility>
    #include <vector>
    #define mem(arr, num) memset(arr, 0, sizeof(arr))
    #define _for(i, a, b) for (int i = a; i <= b; i++)
    #define __for(i, a, b) for (int i = a; i >= b; i--)
    #define IO                     
      ios::sync_with_stdio(false); 
      cin.tie(0);                  
      cout.tie(0);
    using namespace std;
    typedef long long ll;
    const ll inf = 0x3f3f3f3f;
    const double EPS = 1e-10;
    const ll mod = 1000000007LL;
    const int N = 1 << 19;
    int dat[N],n;
    void update(int a,int b,int k,int l,int r) {
      if(a<=l && b >= r) {
        dat[k]++;
      } else if(a <= r && b >=l) {
        update(a,b,k<<1,l,(l+r)/2);
        update(a,b,k<<1|1,(l+r)/2+1,r);
      }
    }
    void down(int l,int r,int k) {
      if(l==r) {
        l==n?printf("%d
    ",dat[k]):printf("%d ",dat[k]);
      }
      else {
        dat[k<<1] += dat[k];
        dat[k<<1|1] += dat[k];
        down(l,(l+r)/2,k<<1);
        down((l+r)/2+1,r,k<<1|1);
      }
    }
    int main() {
      int a,b;
      while(scanf("%d",&n),n) {
        mem(dat,0);
        _for(i,1,n) {
          scanf("%d%d",&a,&b);
          update(a,b,1,1,n);
        }
        down(1,n,1);
      }
      return 0;
    }
    宝剑锋从磨砺出 梅花香自苦寒来
  • 相关阅读:
    【转】Java并发编程:synchronized
    【转】Java并发编程:Thread类的使用
    【转】Java并发编程:如何创建线程?
    【计算机二级C语言】卷005
    【计算机二级C语言】卷004
    【计算机二级C语言】卷003
    【计算机二级C语言】卷002
    【计算机二级C语言】卷001
    汇编窥探Swift String的底层
    【KakaJSON手册】08_其他用法
  • 原文地址:https://www.cnblogs.com/GHzcx/p/8921727.html
Copyright © 2011-2022 走看看