题意
思路
一、使用树状数组
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1e5+10;
struct Cow{
Cow(int l,int r,int p) :l(l),r(r),pos(p){}
Cow(){}
int l,r,pos;
}c[N];
bool cmp(Cow a,Cow b){
if(a.l==b.l) return a.r > b.r;
return a.l < b.l;
}
int ans[N];
int tree[N<<1];
int lowbit(int x){
return x&-x;
}
int query(int x){
int ret = 0;
while(x){
ret+=tree[x];
x-=lowbit(x);
}
return ret;
}
void update(int x,int maxn){
while(x<=maxn){
tree[x]++;
x+=lowbit(x);
}
}
int main()
{
//std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int n,l,r;
while(~scanf("%d",&n)&&n){
memset(tree,0,sizeof(tree));
int maxn = 0;
for(int i=1;i<=n;i++){
//cin >> l >> r;
scanf("%d%d",&l,&r);
l++,r++;//全部平移一格,因为树状数组从1开始
maxn = max(maxn,r);
c[i] = Cow(l,r,i);
}
sort(c+1,c+1+n,cmp);
for(int i=1;i<=n;i++){
if(i!=1&&c[i].l==c[i-1].l&&c[i].r==c[i-1].r){
ans[c[i].pos] = ans[c[i-1].pos];
}else{
ans[c[i].pos] = query(maxn) - query(c[i].r-1);
}
//当前右端点所属的区间都需要更新
update(c[i].r,maxn);
}
for(int i=1;i<=n;i++){
printf("%d ",ans[i]);
//cout << ans[i] << ' ';
}
putchar('\n');
//cout << '\n';
}
return 0;
}
二、使用线段树
见我的另一篇博客的问题三
三、使用zkw线段树
等待学习…