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,所以不用离散化也行),遍历每一头牛,在它之前的,E值大于它的牛的数目即是答案(要注意两者相同的情况)。

其实,上面的线段树就是排序好之后的E值序列的中每一个数的逆序对数目。

代码:

#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<fstream>
#include<cstring>
#include<ctype.h>
#include<iostream>
#include<algorithm>
#define INF (1<<30)
#define PI acos(-1.0)
#define mem(a, b) memset(a, b, sizeof(a))
#define rep(i, n) for (int i = 0; i < n; i++)
#define debug puts("===============")
typedef long long ll;
using namespace std;
const int maxn = 100100;
int n;
struct node {
    int x, y, id;
}e[maxn];
bool cmp(node s, node v) {
    if (s.x == v.x) return s.y > v.y;
    return s.x < v.x;
}
int x[maxn], index[maxn], dis[maxn];
int discrete(int x[], int index[], int dis[], int n) {
    int cpy[n];
    for (int i = 0; i < n; i++) {
        x[i] = e[i].y;
        cpy[i] = x[i];
    }
    sort(cpy, cpy + n);
    int tot = unique(cpy, cpy + n) - cpy;
    for (int i = 0; i < n; i++) {
        dis[i] = lower_bound(cpy, cpy + tot, x[i]) - cpy;
        index[dis[i]] = i;
    }
    return tot;
}
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
int has[maxn];
int sum[maxn << 2];
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 add(int pos, int x, int l, int r, int rt) {
    if (l == r) {
        sum[rt] += x;
        return ;
    }
    int m = (l + r) >> 1;
    if (pos <= m) add(pos, x, lson);
    else add(pos, x, rson);
    pushup(rt);
}
int query(int L, int R, int l, int r, int rt) {
    if (L <= l && r <= R) return sum[rt];
    int m = (l + r) >> 1;
    int res = 0;
    if (L <= m) res += query(L, R, lson);
    if (R > m) res += query(L, R, rson);
    return res;
}
int main () {
    while(~scanf("%d", &n), n) {
        for (int i = 0; i < n; i++) {
            scanf("%d%d", &e[i].x, &e[i].y);
            e[i].id = i;
        }
        sort(e, e + n, cmp);
        int m = discrete(x, index, dis, n);
        //rep(i, n) cout<<e[i].x<<" "<<e[i].y<<"-------->"<<e[i].id<<" "<<dis[i]<<endl;
        int nowx = -1, nowy = -1, ans = 0, cnt = 1;
        build(0, m - 1, 1);
        for (int i = 0; i < n; i++) {
            if (e[i].x == nowx && e[i].y == nowy) {
                has[e[i].id] = ans;
            } else {
                ans = query(dis[i], m - 1, 0, m - 1, 1);
                has[e[i].id] = ans;
                nowx = e[i].x, nowy = e[i].y;
            }
            add(dis[i], 1, 0, m - 1, 1);
        }
        for (int i = 0; i < n; i++) printf("%d%c", has[i], i == n - 1 ? '\n' : ' ');
    }
    return 0;
}

POJ 2481 Cows (线段树),布布扣,bubuko.com

时间: 2024-10-15 08:41:57

POJ 2481 Cows (线段树)的相关文章

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