POJ2481 Cows 树状数组的简单应用

题意给了你N头牛,每头牛的强壮值是一个区间[s,e],如果第 i 头牛比第 j 头牛强壮那么必须满足 Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj;

为了满足这三个条件我们进行排序,先以e降序排为先决条件,若e相等则让s升序排列,如此即可满足三个条件,这道题目任意两头牛的强壮值区间有可能完全一样,这样就不需要重新用树状数组求一次和了,直接赋值即可,这样可以省很多时间,因为已经排序处理了,所以即便区间相等的  肯定就是相邻的,所以直接扫一遍即可,若不想等则用树状数组求和,往树状数组里添加的时候,以牛的s为序号,以1为value来加进去,这样处理以后就跟POJ2352简直是一模一样了

#include<iostream>
#include<cstdio>
#include<list>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
#include<map>
#include<vector>
#include<cmath>
#include<memory.h>
#include<set>
#include<cctype>

#define ll long long
#define LL __int64
#define eps 1e-8

//const ll INF=9999999999999;

#define inf 0xfffffff

using namespace std;

//vector<pair<int,int> > G;
//typedef pair<int,int> P;
//vector<pair<int,int>> ::iterator iter;
//
//map<ll,int>mp;
//map<ll,int>::iterator p;

typedef struct Node {
	int s,e;
	int id;
};

Node node[100000 + 5];

int n;
int c[100000 + 5];
int ans[100000 + 5];

void clear() {
	memset(node,0,sizeof(node));
	memset(c,0,sizeof(c));
	memset(ans,0,sizeof(ans));
}

bool cmp(Node x,Node y) {
	if(x.e == y.e)
		return x.s < y.s;
	return x.e > y.e;
}

int lowbit(int x) {
	return x&(-x);
}

void add(int i,int value) {
	while(i <= n) {
		c[i] += value;
		i += lowbit(i);
	}
}

int get_sum(int i) {
	int sum = 0;
	while(i > 0) {
		sum += c[i];
		i -= lowbit(i);
	}
	return sum ;
}

int main() {
	while(scanf("%d",&n),n) {
		clear();
		for(int i=1;i<=n;i++) {
			scanf("%d %d",&node[i].s,&node[i].e);
			node[i].s++;
			node[i].e++;//题目给的区间有从0开始
			node[i].id = i;
		}
		sort(node + 1,node + n + 1,cmp);
		for(int i=1;i<=n;i++) {
			if(i > 1 && node[i].s == node[i-1].s && node[i].e == node[i-1].e)//相等直接诶赋值
				ans[node[i].id] = ans[node[i-1].id];
			else
				ans[node[i].id] = get_sum(node[i].s);//否则树状数组求和
			add(node[i].s,1);
		}
		for(int i=1;i<n;i++)
			printf("%d ",ans[i]);
		printf("%d\n",ans[n]);
	}
	return 0;
}

POJ2481 Cows 树状数组的简单应用,码迷,mamicode.com

时间: 2024-10-12 16:56:31

POJ2481 Cows 树状数组的简单应用的相关文章

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

POJ2481: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 2481 Cows 树状数组解法,详细解析。

Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13445   Accepted: 4448 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

Cows(树状数组)

Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 19673   Accepted: 6699 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 2182 Lost Cows (树状数组)

Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9660   Accepted: 6219 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, they visited the neighborhood 'waterin

树状数组的简单运用

树状数组是一个比较优秀的数据结构,可以在O(log n)的情况下完成一些对数列的维护~~ 而且代码简单易懂,所以树状数组在OI竞赛中对于解决区间问题是十分常用的数据结构 接下来是一些例题: A.校门外的树 题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,--,L,都种有一棵树. 由于马路上有一些区域要用来建地铁.这些区域用它们在数轴上的起始点和终止点表示.已知

soj 1088. Cows(树状数组)

可以用树状数组解决. 先按左端点递增排序,左端点相等的按右端点降序排列. 然后从左往有扫,更新答案同时更新sum数组. 对于一只Cow i,ans[i]为f(i)-g(i). f(i)为满足p[j].s<=p[i].s&&p[j].e>=p[i].e(0<=j<n,j!=i)的Cow只数. g(i)为满足p[j].s==p[i].s&&p[j].e==p[i].e(0<=j<n,j!=i)的Cow只数. 开个d数组维护一下,d[i]即为g

POJ 2481 COWS(树状数组)

题目大意: 说给你n个线段的,告诉你每个线段的起始点S_i,和终止点E_i, 问这n条线段里有多少线段是相互包含的,如果两个端点重合不算包含. 解题思路: 用树状数组搞就可以了,这道题是star的变形题,star是让我们求出有多个星星在这个星星的左下角,而这道题是让我们求出有多少个星星在这个星星的左上角. 这样分析,对于(S_i,E_i)我们把他封装成为一个点的坐标,那么(S_j,E_j)同样也可以被封装成为一个点的坐标,而题目中说的S_i<S_j&&E_j<E_i&&

POJ 2182 Lost Cows (树状数组 &amp;&amp; 二分查找)

题意:给出数n, 代表有多少头牛, 这些牛的编号为1~n, 再给出含有n-1个数的序列, 每个序列的数 ai 代表前面还有多少头比 ai 编号要小的牛, 叫你根据上述信息还原出原始的牛的编号序列 分析:如果倒着看这个序列的话, 那序列的最后一个元素就能够确定一个编号.举个例子:如果序列的最后一个元素为0, 那就说明这头牛前面再也没有比它编号更小的牛了, 所以这头牛的编号肯定是最大的, 我们只要给它所在的编号加个标记, 然后继续根据倒数第二个.第三个--来依次确定便可还原整个序列, 这里可以使用树