您的位置:首页 > 其它

HDU 1556 Color the ball

2015-07-12 00:34 447 查看
You can solve it by tree array;

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

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>

using namespace std;

const int M = 100005;
int Bit[M << 1] ;

void Insert(int x,int value) {
for(int i = x ; i < M ; i += i & (-i)) {
Bit[i] += value;
}
}

int Query(int x){
int ret = 0;
for(int i = x ; i  ; i -= i & (-i)) {
ret += Bit[i];
}
return ret;
}

void Deal_with(){
int n,Left,Right;
while(scanf("%d",&n),n) {
memset(Bit,0,sizeof(Bit));
for(int i = 0 ; i < n ; i ++ ) {
scanf("%d %d",&Left,&Right);
Insert(Left,1);
Insert(Right+1,-1);
}
for(int i = 1 ; i <= n ;i ++){
printf("%d",Query(i));
printf(i == n ? "\n" : " ");
}
}
}

int main(void){
//freopen("a.in","r",stdin);
Deal_with();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: