标签:
COWsDescription
Input
Output
Sample Input
3 1 2 0 3 3 4 0
Sample Output
1 0 0
Analyse:
s从小到大排,e从大到小。然后依次modify,求>=c[i].e的sum;
注意几个坑点:
3
1 3
1 3
1 3
1 2
0 0 0 1
3
1 2
1 2
1 2
0 0 0
注意!!!!坑死!
CODE:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<string> #include<queue> #include<deque> #include<stack> #include<map> #include<set> #define INF 0x7fffffff #define SUP 0x80000000 #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long LL; const int N=100007; struct Tree{ int le,ri; int sum; }t[N<<2]; struct Cow{ int s,e,id; bool operator <(Cow tt) const{ if(s==tt.s) return e>tt.e; return s<tt.s; } }c[N]; int ans[N]; void build(int ro,int le,int ri) { t[ro].le=le; t[ro].ri=ri; t[ro].sum=0; if(le==ri) return; int mid=le+(ri-le)/2; build(ro<<1,le,mid); build(ro<<1|1,mid+1,ri); } void modify(int ro,int le,int ri,int x) { if(le==ri){ t[ro].sum+=1; return; } int mid=le+(ri-le)/2; if(x<=mid) modify(ro<<1,le,mid,x); else modify(ro<<1|1,mid+1,ri,x); t[ro].sum=t[ro<<1].sum+t[ro<<1|1].sum; } int query(int ro,int le,int ri,int L,int R) { if(L<=le&&ri<=R){ return t[ro].sum; } int mid=le+(ri-le)/2; int ret=0; if(L<=mid) ret+=query(ro<<1,le,mid,L,R); if(R>mid) ret+=query(ro<<1|1,mid+1,ri,L,R); return ret; } int main() { int n; while(scanf("%d",&n)==1,n){ int maxx=0; for(int i=0;i<n;i++){ scanf("%d%d",&c[i].s,&c[i].e); c[i].id=i; maxx=max(c[i].e,maxx); } sort(c,c+n); build(1,0,maxx+10); modify(1,0,maxx+10,c[0].e); ans[c[0].id]=0; for(int i=1;i<n;i++) { if(c[i].s==c[i-1].s&&c[i].e==c[i-1].e){ ans[c[i].id]=ans[c[i-1].id]; modify(1,0,maxx+10,c[i].e); } else{ ans[c[i].id]=query(1,0,maxx+10,c[i].e,maxx+10); modify(1,0,maxx+10,c[i].e); } } for(int i=0;i<n;i++) printf("%d%c",ans[i],i<n-1?' ':'\n'); } return 0; }
标签:
原文地址:http://blog.csdn.net/code_or_code/article/details/44835201