POJ - 2481 Cows (单点更新,区间查询(求和)的树状数组)

POJ - 2481 Cows (单点更新,区间查询(求和)的树状数组)

【vj链接】


题目

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: cow i and cow j, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cow i is stronger than cow j.

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 <= 10 5), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 10 5) specifying the start end location respectively of a range preferred by some cow. Locations are given as distance from the start of the ridge.

The end of the input contains a single 0.
Output
For each test case, output one line containing n space-separated integers, the i-th of which specifying the number of cows that are stronger than cow i.
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.


题目大意与解题思路

多组输入以0结束
第一行n表示有n头牛
接下来n行是每头牛的S和E ( S < E )
求每头牛比它自己强壮的有多少
i比j强壮 当且仅当 S[i]<=S[j] && E[i]>=E[j] && E[i]-S[i]>E[j]-S[j] ;
解题思路
按S从小到大把牛排序后对于每头牛查询之前E>=当前牛的E的数量并减去相同牛的数量。
用线段树解决这类题很方便,但是线段树写起来太长了,而且要注意的细节很多,用树状数组会比较快,代码更简洁一点。
ps.
注意不管是线段树还是树状数组都是从1开始的


AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#define LL long long
using namespace std;

const int maxm=1e5+100;///树状数组大小
const int maxn=1e5+100;
const int INF=0x3f3f3f3f;

#define lowbit(x) ((x)&(-x))

int m;///树状数组中最大数
int c[maxm];
void init_clear()
{
    for(int i=0;i<=m;i++)
        c[i]=0;
}
void update(int *c,int x,int v)
{
    for(int i=x;i<=m;i+=lowbit(i))
        c[i]+=v;
}
int get_sum(int *c,int x)
{
    int ans=0;
    for(int i=x;i>0;i-=lowbit(i))//i要大于0
        ans+=c[i];
    return ans;
}
struct node
{
    int x,y;
    int pos;
}a[maxn];
bool cmp(node a,node b)
{
    if(a.x!=b.x) return a.x<b.x;
    return a.y>b.y;
}
int ans[maxn];
int n;
int main()
{
    int i,j,k,x,y,l,r,cnt;
    while(scanf("%d",&n))
    {
        if(n==0) break;
        m=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
            a[i].x++;
            a[i].y++;
            a[i].pos=i;
            m=max(m,a[i].y);
        }
        init_clear();
        sort(a+1,a+1+n,cmp);
        l=r=1;
        cnt=0;
        while(r<=n)
        {
            ans[a[r].pos]=get_sum(c,m)-get_sum(c,a[r].y-1);
            //printf("(%d,%d,%d) ",a[r].x,a[r].y,ans[a[r].pos]);
            if(a[r].x==a[r-1].x&&a[r].y==a[r-1].y) ans[a[r].pos]-=cnt;
            else cnt=0;
            //printf("(%d,%d,%d)\n",a[r].x,a[r].y,ans[a[r].pos]);
            update(c,a[r].y,1);
            cnt++;
            r++;
        }
        for(i=1;i<n;i++)
        {
            printf("%d ",ans[i]);
        }
        printf("%d\n",ans[i]);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值