POJ 2352
树状数组水题。
开始的时候WA的不明所以,后来看到只是Y是非降序排好的,X是乱序。
所以要把X也排序后再做,或者对每一个相同的Y,添加所有X后再进行sum计算。
还要注意update和sum函数还要注意X=0的情况。
上代码:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<math.h> 4 #include<algorithm> 5 6 #define range 32001 7 8 int a[range],total[range]; 9 int x[range],y[range]; 10 11 int lowbit(int i); 12 int sum(int loc); 13 void update(int loc); 14 15 int main() 16 { 17 int n; scanf("%d",&n); 18 for(int i=0; i!=n; ++i) 19 scanf("%d%d",&x[i],&y[i]); 20 x[n]=y[n]=0x3f3f3f3f; 21 22 memset(a,0,sizeof(a)); 23 memset(total,0,sizeof(total)); 24 update(x[0]); 25 int head=0; 26 27 int tmp; 28 for(int i=1; i<=n; ++i) 29 { 30 if(y[i] != y[i-1]) 31 { 32 for(int j=head; j!=i; ++j) 33 { tmp = sum(x[j]) ; 34 //printf("%d\n",tmp); 35 ++total[tmp]; } 36 head=i; 37 } 38 39 update(x[i]); 40 } 41 42 for(int i=0; i!=n; ++i) 43 printf("%d\n",total[i]); 44 //while(1); 45 return 0; 46 } 47 48 int lowbit(int i) 49 { 50 return i&(-i); 51 } 52 53 int sum(int loc) 54 { 55 int all = 0; 56 while(loc > 0) 57 { 58 all += a[loc]; 59 loc -= lowbit(loc); 60 } 61 all += a[0]-1; 62 return all; 63 } 64 65 void update(int loc) 66 { 67 if(loc == 0) 68 { ++a[0]; return; } 69 70 while(loc < range) 71 { 72 a[loc] += 1; 73 loc += lowbit(loc); 74 } 75 return ; 76 }
To Be The Best Of Yourself