poj 2481
阿新 • • 發佈:2018-07-28
為我 farmer end ace icu which ron border his
Language:
Cows
Description Farmer John‘s cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good.Farmer John has N cows (we number the cows from 1 to N). Each of Farmer John‘s N cows has a range of clover that she particularly likes (these ranges might overlap). The ranges are defined by a closed interval [S,E]. But some cows are strong and some are weak. Given two cows: cowi For each cow, how many cows are stronger than her? Farmer John needs your help! Input The input contains multiple test cases.For each test case, the first line is an integer N (1 <= N <= 105 The end of the input contains a single 0. Output Sample Input 3 1 2 0 3 3 4 0 Sample Output 1 0 0 Hint Huge input and output,scanf and printf is recommended.Source POJ Contest,Author:Mathematica@ZSU |
簡要說明題意:
給你一些區間,問你每個區間會被多少個區間完全覆蓋(並且不完全相同)。
首先根據左端點的位置排序,從小到大訪問,在最後把它對應的右端點位置的數+1,只求這個區間r到所有區間的最靠右位置的區間和就是答案,因為我們思考在它前面那些讓靠後位置加1的區間,因為排序的原因l一定是小於等於此處的l,那麽r也是滿足條件就可以計入了
註意相同區間還有 5,6包含 5,5
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=1e5+5; struct A { int l,r,id; bool operator<(const A &a)const { if(a.l!=l) return l<a.l; return r>a.r; } }a[N]; int ans[N],c[N],mx; void add(int x) { for(;x<=mx;x+=x&-x) ++c[x]; } int askk(int x) { int re=0; for(;x;x-=x&-x) re+=c[x]; return re; } int main() { while(1) { int n; mx=0; scanf("%d",&n); if(!n) break; for(int i=1;i<=n;++i) scanf("%d%d",&a[a[i].id=i].l,&a[i].r),mx=max(a[i].r,mx);//取最大右端 sort(a+1,a+n+1); memset(c,0,sizeof(c)); for(int i=1;i<=n;++i) { if(a[i].l==a[i-1].l&&a[i].r==a[i-1].r) ans[a[i].id]=ans[a[i-1].id];//與前一個相同則不需要計算 else ans[a[i].id]=askk(mx)-askk(a[i].r-1);//計算a[i].r到mx的區間和 add(a[i].r);//對應位置添加 } for(int i=1;i<=n;++i) printf("%d ",ans[i]); printf("\n"); } return 0; }
poj 2481