标签:
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 15301 | Accepted: 5095 |
Description
Input
Output
Sample Input
3 1 2 0 3 3 4 0
Sample Output
1 0 0
Hint
思路:初看好像挺复杂的。其实可以把区间[S, E]看成点(S, E),这样题目就转化为hdu 1541 Stars。只是这里是求该点左上方的点的个数。
虽然如此,我还是WA了不少,有一些细节没注意到。给点排序时是先按y由大到小排序,再按x由小到大排序。而不能先按x排序。比如n=3, [1,5], [1,4], [3,5]的例子。另外还要注意对相同点的处理。
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 using namespace std; 5 6 const int MAX = 100010; 7 8 struct Node{ 9 int x, y, id, ans; 10 }seq[MAX]; 11 int sum[MAX], n; 12 13 int cmp1(Node a,Node b){ 14 if(a.y==b.y) return a.x<b.x; 15 return a.y>b.y; 16 } 17 int cmp2(Node a,Node b){ 18 return a.id<b.id; 19 } 20 21 int lowbit(int x){ 22 return x & (-x); 23 } 24 void add(int pos, int val){ 25 while(pos < MAX){ 26 sum[pos]+=val; 27 pos+=lowbit(pos); 28 } 29 } 30 int getsum(int pos){ 31 int res = 0; 32 while(pos>0){ 33 res+=sum[pos]; 34 pos-=lowbit(pos); 35 } 36 return res; 37 } 38 39 int main() 40 { 41 freopen("in.txt","r",stdin); 42 int i,j; 43 while(scanf("%d", &n) && n){ 44 for(i=1;i<=n;i++){ 45 scanf("%d%d", &seq[i].x, &seq[i].y); 46 seq[i].x++, seq[i].y++; 47 seq[i].id = i; 48 } 49 sort(seq+1,seq+n+1,cmp1); 50 memset(sum, 0, sizeof(sum)); 51 seq[1].ans = 0; 52 add(seq[1].x, 1); 53 int fa = 1; 54 for(i=2;i<=n;i++){ 55 if(seq[i].x == seq[fa].x && seq[i].y == seq[fa].y){ 56 seq[i].ans = seq[fa].ans; 57 }else{ 58 fa = i; 59 seq[i].ans = getsum(seq[i].x); 60 } 61 62 add(seq[i].x, 1); 63 } 64 sort(seq+1,seq+n+1,cmp2); 65 printf("%d", seq[1].ans); 66 for(i=2;i<=n;i++) printf(" %d", seq[i].ans); 67 printf("\n"); 68 } 69 return 0; 70 }
标签:
原文地址:http://www.cnblogs.com/a1225234/p/5038618.html