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,那么它的编 ...
随机推荐
- LVS+keepalived均衡nginx配置
如果单台LVS发生突发情况,例如宕机.发生不可恢复现象,会导致用户无法访问后端所有的应用程序.避免这种问题可以使用HA故障切换,也就是有一台备用的LVS,主LVS 宕机,LVS VIP自动切换到从,可 ...
- 紫书 习题 10-7 UVa 10539(long long + 素数筛)
注意要开long long 如果int * int会炸 那么久改成long long * int #include<cstdio> #include<vector> #incl ...
- [Poi] Build and Analyze Your JavaScript Bundles with Poi
Ever wonder where those extra KB in your bundle are coming from? This lesson walks you through runni ...
- F - Humidex(1.4.2)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Descr ...
- oracle 10g/11g RAC 启停归档模式
oracle 10g rac 启停归档模式 假设Oracle数据库执行在归档模式,当进行数据库维护时,可能须要暂停数据库的归档,在完毕维护后,再又一次启动归档模式. 通过下面步骤能够从归档 ...
- 用motion实现家庭视频监控
需求?当然不是为了艳照.你们这些猥琐的人类! 毕竟家里总会有没人的时候,出门走到半路忘记煤气灶是不是关了,还得回去看看. 在这个科技以人为本的时代,当然应该是拿出智能手机联网看看啦.还有万一有人闯空门 ...
- HackingTeam重磅炸弹: 估值超1000万美金带有军火交易性质的木马病毒以及远控源代码泄露
[简单介绍] 经常使用网名: 猪头三 出生日期: 1981.XX.XX 个人站点: http://www.x86asm.com QQ交流: 643439947 编程生涯: 2001年~至今[共14年] ...
- 离散化求RECT1
本文转载至点击打开链接 #include<stdio.h> struct node{ int x1,y1,x2,y2,c; }; struct node s[1010]; int px[2 ...
- hdoj--2682--Tree()
Tree Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- 《Android编程权威指南》PhotoGallery应用梳理
PhotoGalley是<Android编程权威指南>书中另外一个重要的应用.