码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1556 - Color the ball

时间:2014-07-26 17:05:31      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   2014   for   

原理就是先利用两个数间的差分,因为把一段连续的数做标记相当于头差分+1,尾差分-1,然后做前缀和即可。

可以想到,相比segment tree这种个只适合于查询操作不多但修改操作很多的情况(如果保存前缀和修改又会降速),否则计算前缀和会很费时。

/*
ID:esxgx1
LANG:C++
PROG:hdu1556
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define NN    100000

int a1[NN], a2[NN];

int main(void)
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif

    int N;

    while(scanf("%d", &N) > 0) {
        if (!N) break;
        for(int i=0; i<N; ++i) {
            int l, r;
            scanf("%d%d", &l, &r);
            a1[l-1]++, a1[r]--;
        }
        int sum = a1[0];
        a1[0] = 0;
        for(int i=1; i<=N; ++i) {
            if (i > 1) putchar( );
            printf("%d", sum);
            sum += a1[i];
            a1[i] = 0;
        }
        putchar(\n);
    }
    return 0;
}

 

2014-07-26 15:58:25 Accepted 1556 578MS 688K 634 B G++

HDU 1556 - Color the ball,布布扣,bubuko.com

HDU 1556 - Color the ball

标签:style   blog   http   color   os   io   2014   for   

原文地址:http://www.cnblogs.com/e0e1e/p/hdu_1556.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!