poj2481 树状数组

树状数组模板题,如果有牛的区域能全覆盖目前牛的区域,那么该牛比目前牛强壮

首先按e从大到小排,如果一样按s从小到大排

a数组记录的是ans,及到x为止的前缀和

注意两个区域相等时结果也相等而不算全覆盖

树状数组可以解决的:点修改,查询前缀和,每次修改和查询前缀和都是logn

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
#define FOR(i, l, r) for(int i = l; i <= r; i++)
#define REP(i, r, l) for(int i = r; i >= l; i--)
typedef long long ll;
double eps = 1e-6;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
struct coww
{
    int s,e,id;
    friend bool operator <(const coww &nd1,const coww &nd2)
    {
        if(nd1.e == nd2.e)
            return nd1.s < nd2.s; //如果e相等,s从小到大排
        return nd1.e > nd2.e;  //e从大到小排
    }
}cow[100010];
int c[100010];
int a[100010];
int lowbit(int x)
{
    return x & (-x);
}
void modify(int x)    //x位置修改(+1)后c[x]的修改
{
    while(x <= 100010)
    {
        c[x] ++ ;
        x += lowbit(x);
    }
}
int sum(int x)   //x的前缀和
{
    int ret=0;
    while(x > 0)
    {
        ret += c[x];
        x-=lowbit(x);
    }
    return ret;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        if(n== 0) break;
        memset(a,0,sizeof(a));
        memset(c,0,sizeof(c));
        FOR(i,1,n)
        {
            scanf("%d%d", &cow[i].s, &cow[i].e);
            cow[i].s++ , cow[i].e++;
            cow[i].id=i;
        }
        sort( cow+1 , cow+n+1);
        FOR(i,1,n)
        {
            if(cow[i].s == cow[i-1].s &&cow[i].e == cow[i-1].e)
                a[cow[i].id]=a[cow[i-1].id];
            else
                a[cow[i].id]=sum(cow[i].s);
            modify(cow[i].s);
        }
        FOR(i,1,n-1)
            printf("%d ",a[i]);
        printf("%d\n",a[n]);
    }
    
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值