Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4165 Accepted Submission(s): 2241
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,輸入結束。
當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
#include <cstdio>
#include <string.h>
int ss[100005];
using namespace std;
int n;
int lowbit(int x){
return x&(-x);
}
void update(int x,int y){
while(x>0){
ss[x]+=y;
x-=lowbit(x);
}
}
int sum(int x){
int s=0;
while(x<=n){
s+=ss[x];
x+=lowbit(x);
}
return s;
}
int main(){
while(scanf("%d",&n)&&n){
memset(ss,0,sizeof(ss));
int a,b,m;
m=n;
while(m--){
scanf("%d%d",&a,&b);
update(a-1,-1);
update(b,1);
}
for(int i=1;i<n;++i){
printf("%d ",sum(i));
}
printf("%d\n",sum(n));
}
return 0;
}
解法2:(普通用技巧)
#include<stdio.h> #include <string.h> int f[100010]; int main() { int n; int i; while (scanf("%d",&n),n) { memset(f,0,sizeof(f)); for (i=0;i<n;++i) { int a,b; scanf("%d%d",&a,&b); ++f[a]; --f[b+1]; } int m=0; for (i=1;i<n;++i) { m+=f[i]; printf("%d ",m); } m+=f[i]; printf("%d\n",m); } return 0; }