#include <iostream> #include <stdio.h> #include <string> #include <string.h> #include <algorithm> #include <math.h> #include <fstream> #include <vector> #include <map> #include <queue> #include <stack> #include <math.h> #include <stdlib.h> using namespace std ; const int maxn = 100005; int n; struct cow{ int id; int l,r; friend bool operator<(const cow &a,const cow &b){ if(a.r == b.r) return a.l < b.l; return a.r > b.r; } }cow[maxn]; int c[maxn]; int lowbit(int x){ return x&(-x); } void update(int id,int val,int up){ for(int i = id;i <= up;i+=lowbit(i)) c[i] += val; } int query(int id){ int ans = 0; for(int i = id;i > 0;i-=lowbit(i)) ans += c[i]; return ans; } int out[maxn]; int main(){ while(scanf("%d",&n)&&n){ int up = -1; for(int i = 1;i <= n;i++){ scanf("%d%d",&cow[i].l,&cow[i].r); cow[i].l++; cow[i].r++; cow[i].id = i; if(up < cow[i].l) up = cow[i].l; } sort(cow+1,cow+n+1); //for(int i = 1;i <= n;i++) // printf("%d %d\n",cow[i].l,cow[i].r); memset(c,0,sizeof(c)) ; out[cow[1].id]=0; update(cow[1].l,1,up); for(int i = 2;i <= n;i++){ if(cow[i].l == cow[i-1].l && cow[i].r == cow[i-1].r) out[cow[i].id] = out[cow[i-1].id]; else out[cow[i].id] = query(cow[i].l); update(cow[i].l,1,up); } printf("%d",out[1]) ; for(int i = 2;i <= n;i++) printf(" %d",out[i]); printf("\n"); } return 0 ; }
POJ(2481)Cows 树状数组
最新推荐文章于 2021-11-11 18:31:11 发布