POJ2481

题意

在这里插入图片描述
在这里插入图片描述

思路

在这里插入图片描述

一、使用树状数组

#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线段树

等待学习…

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值