1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| #include <cstdio> #include <iostream> #include <algorithm> #include <cstring>
using namespace std; const int maxn = 1e5 + 10;
int c[maxn]; int num[maxn]; int revise[maxn];
struct pp{ int l; int r; int id; }a[maxn];
bool cmp(struct pp a,struct pp b){ if(a.r != b.r) return a.r > b.r; else return a.l < b.l; }
int lowbit(int x){ return x&(-x); }
void update(int x,int y){ while(x <= y){ c[x] += 1; x += lowbit(x); } }
int getsum(int x){ int ans = 0; while(x >= 1){ ans += c[x]; x -= lowbit(x); } return ans; }
int main(){ int n; int cnt ; int z; int maxx ; while(scanf("%d",&n) && n){ maxx = -1; memset(c,0,sizeof c); memset(num,0,sizeof num); memset(revise,0, sizeof revise);
for(int i = 1 ; i <= n ; i++){ scanf("%d %d",&a[i].l,&a[i].r); a[i].l += 1; a[i].r += 1; a[i].id = i; if(maxx < a[i].r){ maxx = a[i].r; } } sort(a+1,a+1+n,cmp); for(int i = 1 ; i <= n ; i++){ if(a[i].l == a[i-1].l && a[i].r == a[i-1].r){ if(a[i-1].l == a[i-2].l && a[i-1].r == a[i-2].r){ revise[i] = revise[i-1] + 1; } else{ revise[i]++; } } }
for(int i = 1 ; i <= n ; i++){
update(a[i].l,maxx);
z = getsum(a[i].l);
num[a[i].id] = z-1 - revise[i]; }
for(int i = 1 ; i <= n ; i++){ printf(i != n+1?"%d ":"%d",num[i]); } cout<<"\n"; } return 0; }
|