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,那么它的编 ...
随机推荐
- 今日SGU 5.28
SGU 121 题意:给你一张图,问你每个顶点必须有黑白两条边(如果它的边数>=2),问你怎么染色,不行就输出no 收获:你会发现不行的情况只有一个单纯的奇数环的时候,反之我们交替染色即可 #i ...
- openVswitch(OVS)源码分析之工作流程(哈希桶结构体的解释)
这篇blog是专门解决前篇openVswitch(OVS)源码分析之工作流程(哈希桶结构体的疑惑)中提到的哈希桶结构flex_array结构体成员变量含义的问题. 引用下前篇blog中分析讨论得到的f ...
- swust oj 2516 教练我想学算术 dp+组合计数
#include<stdio.h> #include<string.h> #include<iostream> #include<string> #in ...
- rgba
正反两面展示效果 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head& ...
- Vsftp问题及解决办法汇总(持续增加中)
1.VsFTP出现500 OOPS: cannot change directory的解决办法 在安装完vsftp服务后登陆时可能遇到cannot change directory后面是登陆者的目录的 ...
- NET Native
起因源自于微软在 MSDN 博客上宣布了 .NET Native 的开发者预览版..NET Native 可以将 C# 代码编译成本地机器码.有了它,开发者将不仅能享受 C# 的高生产力,而且能拥有 ...
- 分享一个tom大叔的js 深入理解系列 (有助于提升)
http://www.cnblogs.com/TomXu/archive/2011/12/15/2288411.html#3620172
- javascript“类”与继承总结
http://haorooms.com/post/js_jc_lei2
- xfce4 + docky ,docky 上面那透明的一条黑色横线去掉方法
在安装完Debian 9 + xfce4桌面后 ,添加docky启动后,会在docky 上面有一条黑色横线看起来非常不舒服. 去掉方法:设置管理器->窗口管理器微调->合成器->取消 ...
- 二:2.1 字符串与循环中的 while
字符串:字符串是以单引号或双引号括起来的任意文本 创建字符串: str1 = "sunck is a good man!" str3 = "sunckis a nice ...