Poj--2481(树状数组)
2014-09-08 21:53:10
思路:将x非降序排序,x相等时y非升序排序,然后在过程中要注意两头牛的S、E相等的情况,此时应该直接复制上头牛的答案并且将该牛Update,巨坑的是数组开到100005 WA,开到100010竟然就AC了 QAQAQAQAQAQAQAQAQAQAQ。
1 /************************************************************************* 2 > File Name: p2481.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Mon 08 Sep 2014 08:27:31 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <vector> 13 #include <queue> 14 #include <iostream> 15 #include <algorithm> 16 using namespace std; 17 typedef long long ll; 18 const int INF = 1 << 29; 19 const int maxn = 100010; 20 21 struct node{ 22 int s,e,p; 23 ll cnt; 24 }no[maxn]; 25 26 int N,c[maxn]; 27 28 int Lowbit(int x){return x & (-x);} 29 void Update(int x,int d){while(x <= maxn){c[x] += d,x += Lowbit(x);}} 30 int Getsum(int x){int res = 0;while(x){res += c[x],x -= Lowbit(x);}return res;} 31 32 33 bool cmp1(node a,node b){ 34 if(a.s == b.s) return a.e > b.e; 35 return a.s < b.s; 36 } 37 38 bool cmp2(node a,node b){ 39 return a.p < b.p; 40 } 41 42 int main(){ 43 while(scanf("%d",&N) != EOF && N){ 44 memset(c,0,sizeof(c)); 45 for(int i = 1; i <= N; ++i){ 46 scanf("%d%d",&no[i].s,&no[i].e); 47 no[i].p = i; 48 } 49 sort(no + 1,no + 1 + N,cmp1); 50 for(int i = 1; i <= N; ++i){ 51 if(i > 1 && no[i].e == no[i - 1].e && no[i].s == no[i - 1].s){ 52 no[i].cnt = no[i - 1].cnt; 53 } 54 else no[i].cnt = (ll)Getsum(maxn) - Getsum(no[i].e - 1); 55 Update(no[i].e,1); 56 } 57 sort(no + 1,no + 1 + N,cmp2); 58 printf("%lld",no[1].cnt); 59 for(int i = 2; i <= N; ++i) printf(" %lld",no[i].cnt); 60 puts(""); 61 } 62 return 0; 63 }