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-1032 The 3n+1 problem 模拟问题(水题)
题目链接:https://cn.vjudge.net/problem/HDU-1032 水题 代码 #include <cstdio> #include <algorithm> ...
- JDBC连接ORACLE无法登陆java.sql.SQLException: ORA-01017: invalid username/password; logon denied
当用jdbc连接Oracle数据库的时候 private Connection getConnection() throws SQLException { OracleDataSource ods = ...
- docker容器存放目录磁盘空间满了,转移数据修改Docker默认存储位置
原文:docker容器存放目录磁盘空间满了,转移数据修改Docker默认存储位置 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_3767485 ...
- Maven 工程 POM.XML文件最全详解
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- script指定src后内部代码无效
/********** 无效 ***************/ <script type="text/javascript" src=""> fun ...
- 解析XML文件之使用DOM解析器
在前面的文章中.介绍了使用SAX解析器对XML文件进行解析.SAX解析器的长处就是占用内存小.这篇文章主要介绍使用DOM解析器对XML文件进行解析. DOM解析器的长处可能是理解起来比較的直观,当然, ...
- traits的介绍
traits基本利用了上一篇文章的思想,通过局部特化,来返回针对特定类型的信息. 可以自己实现traits,约定俗成了,返回特性信息. 也可以利用iterator的traits信息. iterator ...
- linux下u盘检測程序
获得U盘的插入或者拔取得信息的传统方法是在内核级执行hotplug程序.相关參数通过环境变量传递过来,再由hotplug通知其它关注hotplug的应用程序,可是效率比較低. ...
- 独立python环境之virtualenv和virtualenvwrapper
介绍 如果有一台測试机,多个人使用,有多个项目,不同项目可能python版本号不一样.须要的库不一样. 我们须要一个独立干净的python环境,互相隔离,互不影响. virtualenv能够帮我们解决 ...
- wcf rest系列文章
http://www.cnblogs.com/artech/archive/2012/02/15/wcf-rest.html 需要注意的是,发布的服务,可以在web behavior中指定显示help ...