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,那么它的编 ...
随机推荐
- HDU 2253 Longest Common Subsequence Again
其实这个题我还不会,学长给了一个代码交上去过了,据说用到了一种叫做位压缩的技术,先贴代码吧,以后看懂了再来写 #include <stdio.h> #include <string. ...
- Spring Tool Suit安装virgo server插件、virgo的下载
virgo-tomcat原先是Spring DM Server,后来转eclipse社区维护 安装教程:http://osgi.com.cn/article/7289514 virgo-tomcat各 ...
- c#+ArcGIS Engine-获取矢量图层的空间参考
转自原文c#+ArcGIS Engine-获取矢量图层的空间参考 介绍一种简单的获取矢量图层空间参考的方法: 首先打开Shp文件获得FeatureClass,代码如下: string pPath=&q ...
- Java NIO和IO的主要差别
我应该何时使用IO,何时使用NIO呢?在本文中,我会尽量清晰地解析Java NIO和IO的差异.它们的使用场景.以及它们怎样影响您的代码设计. Java NIO和IO的主要差别 下表总结了Java N ...
- HDU2452 Navy maneuvers 记忆化搜索
这题目意思能忍?读了半年,乱七八糟的 记忆化搜索 拖拖的,dp[i][0]代表以获得最小值为目标的船以i为起点.dp[i][1]代表以获得最大值为目标的船以i为起点.接下来暴力枚举入度为0的点为起点, ...
- 概率编程语言(Probabilistic Programming Languages)库 —— edward
注意:tensorflow api 在 1.1.0 以后迎来重大变化,edward 的稳定版依赖于 tensorflow 1.1.0. edward是一个支持概率建模.推断的 Python 第三方库, ...
- 19.volatile
volatile 编译器会自动优化,而volatile起到的作用是禁止优化,每次读内存
- excel操作小技巧
excel拼接sql语句时,时间格式问题 问题:若直接插入时间的单元格 :="insert into t_entity_car (create_time,name,age) value (' ...
- R学习:《机器学习与数据科学基于R的统计学习方法》中文PDF+代码
当前,机器学习和数据科学都是很重要和热门的相关学科,需要深入地研究学习才能精通. <机器学习与数据科学基于R的统计学习方法>试图指导读者掌握如何完成涉及机器学习的数据科学项目.为数据科学家 ...
- make---GNU编译工具
make命令是GNU的工程化编译工具,用于编译众多相互关联的源代码问价,以实现工程化的管理,提高开发效率. 知识扩展 无论是在linux 还是在Unix环境 中,make都是一个非常重要的编译命令.不 ...