POJ--2481--Cows【线段树】

链接 :http://poj.org/problem?id=2481

题意:一条直线上长满了三叶草,有n头牛,每头牛都有喜欢的一段三叶草区间 [ si , ei ] ,如果一头牛喜欢的区间包含了另一头牛喜欢的区间,则说明前者比后者强壮,问对于每头牛各有多少头牛比他强壮。

这道题排序之后就是线段树区间查询的裸题,排序:对区间初始位置从小到大排,对区间结束位置从大到小排,然后依次开始遍历,因为现在找的这头牛的起始位置肯定大于等于找过的牛,所以只用看它的结束位置,从它的结束位置查询到线段树末尾n,看比它的结束位置还大的结束位置有几个,就知道几头牛比它强壮,由于只用比较结束位置,更新的时候也更新结束位置就行了。

#include<cstring>
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<ctime>
#include<cstdlib>
#include<functional>
#include<cmath>
using namespace std;
#define PI acos(-1.0)
#define MAXN 100100
#define eps 1e-7
#define INF 0x7FFFFFFF
#define long long ll;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

struct node{
    int s,e,num;
}a[MAXN];
int ans[MAXN];
int sum[MAXN<<2];
bool cmp(node x,node y){
    if(x.s==y.s)    return x.e>y.e;
    return x.s<y.s;
}
void pushup(int rt){
    sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void build(int l,int r,int rt){
    sum[rt] = 0;
    if(l==r)    return ;
    int m = (l+r)>>1;
    build(lson);
    build(rson);
}
void update(int s,int l,int r,int rt){
    if(l==r){
        sum[rt]++;
        return ;
    }
    int m = (l+r)>>1;
    if(s<=m)    update(s,lson);
    else     update(s,rson);
    pushup(rt);
}
int query(int s,int e,int l,int r,int rt){
    if(s<=l&&r<=e){
        return sum[rt];
    }
    int m = (l+r)>>1;
    int cnt = 0;
    if(s<=m)    cnt+=query(s,e,lson);
    if(e>m)     cnt+=query(s,e,rson);
    return cnt;
}
int main(){
    int n,i;
    while(scanf("%d",&n),n){
        for(i=0;i<n;i++){
            scanf("%d%d",&a[i].s,&a[i].e);
            a[i].num = i;
        }
        sort(a,a+n,cmp);
//        for(i=0;i<n;i++){
//            cout<<a[i].s<<" "<<a[i].e<<endl;
//        }
        build(0,n,1);
        for(i=0;i<n;i++){
            if(i&&a[i].s==a[i-1].s&&a[i].e==a[i-1].e)   ans[a[i].num] = ans[a[i-1].num];
            else    ans[a[i].num] = query(a[i].e,n,1,n,1);
            update(a[i].e,1,n,1);
        }
        for(i=0;i<n-1;i++)  printf("%d ",ans[i]);
        printf("%d\n",ans[i]);
    }
    return 0;
}

POJ--2481--Cows【线段树】

时间: 2024-11-06 23:07:12

POJ--2481--Cows【线段树】的相关文章

POJ 2481 Cows (线段树)

Cows 题目:http://poj.org/problem?id=2481 题意:有N头牛,每只牛有一个值[S,E],如果对于牛i和牛j来说,它们的值满足下面的条件则证明牛i比牛j强壮:Si <=Sjand Ej <= Ei and Ei - Si > Ej - Sj.现在已知每一头牛的测验值,要求输出每头牛有几头牛比其强壮. 思路:将牛按照S从小到大排序,S相同按照E从大到小排序,这就保证了排在后面的牛一定不比前面的牛强壮.再按照E值(离散化后)建立一颗线段树(这里最值只有1e5,所

POJ 2481 Cows 简单树状数组区间覆盖

点击打开链接 Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13334   Accepted: 4413 Description 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

POJ - 2481 - Cows (树状数组+排序!!)

Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13304   Accepted: 4407 Description 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 hi

POJ 2481 Cows(树状数组)

http://poj.org/problem?id=2481 题意: 有n头牛,每头牛有一个区间[S,E],求每头牛比它区间大的牛的个数. 思路: 先对数据进行一下排序,先按右坐标按降序排列,那么接下来我们只需要比较左坐标的数值大小就可以了. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<vector> 6

poj 2481 Cows(树状数组)又是john和他的母牛那点不为人知的故事

Description 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). Ea

POJ 2481 Cows【树状数组】

题意:给出n头牛的s,e 如果有两头牛,现在si <= sj && ei >= ej 那么称牛i比牛j强壮 然后问每头牛都有几头牛比它强壮 先按照s从小到大排序,然后用e来当做树状数组里面那个a数组,对于每头牛求出前面比他大的e有多少个 还有就是注意有两头牛的s和e相等的情况,就只需要更新值, 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <

POJ 2182 Lost Cows.(线段树)

~~~~ 数据太水,暴力无压力,不过还是用线段树写了下,表现了楼主对线段树的无限热爱之情. ~~~~ 题目连接:http://poj.org/problem?id=2182 大致题意;牛牛因XXXXXX原因乱序成一排,现已知每头牛前面有多少头牛比它的编号小(第一头牛前面没有当然就不列粗来了),求每头牛的编号. 思路:从后往前扫描,遇到a,则说明它是剩余序列的第a+1头牛. ~~~~ 暴力代码:(157ms) #include<cstdio> #include<algorithm>

POJ 2481 Cows(树状数组)

Description 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). Ea

poj 2182 Lost Cows(线段树经典题)

题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9152   Accepted: 5879 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, th

POJ 2481 Cows &amp;&amp; POJ 2352 Stars(树状数组妙用)

题目链接:POJ 2481 Cows POJ 2352 Stars 发现这两个题目都跟求逆序数有着异曲同工之妙,通过向树状数组中插入点的位置,赋值为1,或者++,然后通过求和来判断比当前 点 "小" 的有多少点. Cows需要自己排序, Stars题目已经给排好序. POJ 2352 Stars 题目大意为在二维坐标上给出一些星星的坐标,求某一个星星左方,下方,左下方的星星个数.题目已经把星星按照Y坐标从小到大,X从小到大排序.因此,在每次对一个星星进行统计时,之前出现过的星星,只要X