POJ 2481 Cows (线段树)
Cows
如今已知每一头牛的測验值,要求输出每头牛有几头牛比其强壮。
再依照E值(离散化后)建立一颗线段树(这里最值仅仅有1e5,所以不用离散化也行)。遍历每一头牛,在它之前的。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 (线段树)的更多相关文章
- POJ 2481 Cows(树状数组)
		Cows Time Limit: 3000MS Memory L ... 
- POJ 2481 Cows 【树状数组】
		<题目链接> 题目大意: 就是给出N个区间,问这个区间是多少个区间的真子集. 解题分析: 本题与stars类似,只要巧妙的将线段的起点和终点分别看成 二维坐标系中的x,y坐标,就会发现,其 ... 
- poj 2481 Cows(树状数组)题解
		Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ( ... 
- POJ 2481  Cows【树状数组】
		题意:给出n头牛的s,e 如果有两头牛,现在si <= sj && ei >= ej 那么称牛i比牛j强壮 然后问每头牛都有几头牛比它强壮 先按照s从小到大排序,然后用e来 ... 
- 树状数组 POJ 2481 Cows
		题目传送门 #include <cstdio> #include <cstring> #include <algorithm> using namespace st ... 
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
		POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ... 
- Buy Tickets POJ - 2828 思维+线段树
		Buy Tickets POJ - 2828 思维+线段树 题意 是说有n个人买票,但是呢这n个人都会去插队,问最后的队列是什么情况.插队的输入是两个数,第一个是前面有多少人,第二个是这个人的编号,最 ... 
- poj 2481 Cows(数状数组 或 线段树)
		题意:对于两个区间,[si,ei] 和 [sj,ej],若 si <= sj and ei >= ej and ei - si > ej - sj 则说明区间 [si,ei] 比 [ ... 
- POJ  2182 Lost Cows (线段树)
		题目大意: 有 n 头牛,编号为 1 - n 乱序排成一列,现已知每头牛前面有多少头牛比它的编号小,从前往后输出每头牛的编号. 思路: 从后往前推,假如排在最后的一头牛比他编号小的数量为a,那么它的编 ... 
随机推荐
- [JLOI2011]飞行路线(分层图)
			[JLOI2011]飞行路线 题目描述 Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在 n 个城市设有业务,设这些城市分别标记为 0 到 n−1 ,一共有 m ... 
- Spring学习总结(13)——Spring+Log4j+ActiveMQ实现远程记录日志
			应用场景 随着项目的逐渐扩大,日志的增加也变得更快.Log4j是常用的日志记录工具,在有些时候,我们可能需要将Log4j的日志发送到专门用于记录日志的远程服务器,特别是对于稍微大一点的应用.这么做的优 ... 
- Spring Cloud学习笔记【六】Hystrix 监控数据聚合 Turbine
			上一篇我们介绍了使用 Hystrix Dashboard 来展示 Hystrix 用于熔断的各项度量指标.通过 Hystrix Dashboard,我们可以方便的查看服务实例的综合情况,比如:服务调用 ... 
- 洛谷——P1965 转圈游戏
			https://www.luogu.org/problem/show?pid=1965 题目描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n- ... 
- jsbrige
			http://www.cnblogs.com/zhangqie/p/6724252.html 
- Canny边缘检测及C++实现
			Canny边缘检测算法是澳大利亚科学家John F. Canny在1986年提出来的,不得不提一下的是当年John Canny本人才28岁!到今天已经30年过去了,Canny算法仍然是图像边缘检测算法 ... 
- ADO.Net数据库帮助类
			public interface IDBHelper { /// <summary> /// 执行sql语句 /// </summary> /// <param name ... 
- MVC异常过滤器
			MVC过滤器 一般的过滤器执行顺序 IAuthorizationFilter->OnAuthorization(授权) IActionFilter ->OnActionE ... 
- angularjs之手机输入法回车变搜索,并触发事件,兼容pc回车事件
			一.效果:回车按钮变搜索 之前的输入法: 之后的输入法: 二.功能实现 <input type="search" id="search_input" pl ... 
- tensorflow学习之路-----MNIST数据
			''' 神经网络的过程:1.准备相应的数据库 2.定义输入成 3.定义输出层 4.定义隐藏层 5.训练(根据误差进行训练) 6.对结果进行精确度评估 ''' import tensorflow as ... 
