Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 29602 | Accepted: 12924 |
Descriptionios
Input數組
Outputide
Sample Inputthis
5 1 1 5 1 7 1 3 3 5 5
Sample Outputspa
1 2 1 1 0
Hintcode
Sourceorm
樹狀數組計數問題,因爲這裏全部星星都是按從Y值從小到大給出的,所以只要使用一維樹狀數組便可blog
因爲題目中的數據範圍是從0開始的,爲了把0去掉,要將全部數據的X值加1three
按輸入數據的順序,每組在樹狀樹組中先求c[1]+…+c[x+1],以後再在c[x+1]上加1ip
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define MAX 35000 5 6 using namespace std; 7 8 int c[MAX]; 9 int total[MAX]; 10 11 int lowbit(int x) 12 { 13 return x&(-x); 14 } 15 16 int sum(int x) 17 { 18 int ret=0; 19 20 while(x>0) 21 { 22 ret+=c[x]; 23 x-=lowbit(x); 24 } 25 26 return ret; 27 } 28 29 void add(int x,int t) 30 { 31 while(x<=MAX) 32 { 33 c[x]+=t; 34 x+=lowbit(x); 35 } 36 } 37 38 int main() 39 { 40 int n; 41 42 while(scanf("%d",&n)==1) 43 { 44 memset(c,0,sizeof(c)); 45 memset(total,0,sizeof(total)); 46 47 int x,y; 48 for(int i=1;i<=n;i++) 49 { 50 scanf("%d %d",&x,&y); 51 total[sum(x+1)]++; 52 add(x+1,1); 53 } 54 55 for(int i=0;i<n;i++) 56 printf("%d\n",total[i]); 57 } 58 59 return 0; 60 }