题意:
多组数据,给定n个区间,求每个区间被多少区间完全覆盖的总和
Sample Input
3
1 2
0 3
3 4
0
Sample Output
1 0 0
思路:
以左端点排升序,如果相同排降序
因为我们先把不可能被包围的区间操作完,添加它的右端点到树状数组,剩下的区间就看它的右端点小于多少已添加的了
code
#include<stdio.h> #include<string.h> #include<algorithm> #define lowbit(x) x&-x using namespace std; const int mxn=1e5+10; struct node { int l,r,id; }a[mxn]; bool operator <(const node &x,const node &y) { if(x.l!=y.l) return x.l<y.l; return x.r>y.r; } int n,mx,c[mxn],ans[mxn]; void add(int x) { while(x<=mx) { c[x]++; x+=lowbit(x); } } int ask(int x) { int re=0; while(x) { re+=c[x]; x-=lowbit(x); } return re; } int main() { while(scanf("%d",&n) && n) { mx=0; memset(a,0,sizeof(a)); memset(c,0,sizeof(c)); for(int i=1;i<=n;++i) { scanf("%d%d",&a[a[i].id=i].l,&a[i].r); mx=max(mx,a[i].r); } sort(a+1,a+1+n); 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]=ask(mx)-ask(a[i].r-1); add(a[i].r); } for(int i=1;i<=n;++i) { printf("%d ",ans[i]); } puts(""); } return 0; }