博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj2481
阅读量:5098 次
发布时间:2019-06-13

本文共 3323 字,大约阅读时间需要 11 分钟。

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 10785   Accepted: 3572

Description

Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good.
Farmer John has N cows (we number the cows from 1 to N). Each of Farmer John's N cows has a range of clover that she particularly likes (these ranges might overlap). The ranges are defined by a closed interval [S,E].
But some cows are strong and some are weak. Given two cows: cow
i and cow
j, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cow
i is stronger than cow
j.
For each cow, how many cows are stronger than her? Farmer John needs your help!

Input

The input contains multiple test cases.
For each test case, the first line is an integer N (1 <= N <= 10
5), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 10
5) specifying the start end location respectively of a range preferred by some cow. Locations are given as distance from the start of the ridge.
The end of the input contains a single 0.

Output

For each test case, output one line containing n space-separated integers, the i-th of which specifying the number of cows that are stronger than cow
i.

Sample Input

31 20 33 40

Sample Output

1 0 0
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #define llt long long int11 #define ml(x, y) ((x + y) >> 1)12 #define mr(x, y) (((x + y) >> 1) + 1)13 #define pl(p) (p << 1)14 #define pr(p) ((p << 1) | 1)15 #define N 10000516 using namespace std;17 struct node18 {19 int s, e, pos;20 bool operator < (const node& a)const//按照先左边小,再右边大进行排序21 {22 return s == a.s ? e > a.e : s < a.s;23 }24 }a[N];25 int segtree[N << 2], ans[N];26 void build(int l, int r, int p)27 {28 segtree[p] = 0;29 if (l < r)30 {31 build(l, ml(l, r), pl(p));32 build(mr(l, r), r, pr(p));33 }34 }35 void update(int l, int r, int p, int v)36 {37 segtree[p]++;38 if (l != r)39 if (ml(l, r) >= v)40 update(l, ml(l, r), pl(p), v);41 else42 update(mr(l, r), r, pr(p), v);43 }44 int query(int l, int r, int p, int v)//查询区间[l, imax]的个数45 {46 if (v <= l)47 return segtree[p];48 int ans = 0;49 if (ml(l, r) >= v)//等于[l,mid]+[mid+1,imax] 50 ans = segtree[pr(p)] + query(l, ml(l, r), pl(p), v);51 else//[mid+1,imax] 52 ans = query(mr(l, r), r, pr(p), v);53 return ans;54 }55 int main()56 {57 int n, i, maxe;//maxe记录一个最大值58 while (scanf("%d", &n), n)59 {60 maxe = 0;61 for (i = 0; i < n; i++)62 {63 scanf("%d%d", &a[i].s, &a[i].e);64 a[i].pos = i;65 if (maxe < a[i].e)66 maxe = a[i].e;67 }68 sort(a, a + n);//排序 69 build(0, maxe, 1);70 for (i = 0; i < n; i++)71 {72 if (i != 0 && a[i].e == a[i - 1].e && a[i].s == a[i - 1].s)//处理重复的73 ans[a[i].pos] = ans[a[i - 1].pos];//74 else75 ans[a[i].pos] = query(0, maxe, 1, a[i].e);//查询只需要得要a[i].e到imax有多少个数就行了76 update(0, maxe, 1, a[i].e);//因为s小的先处理,所以只需要根据e来更新,记录该区间e的个数77 }78 printf("%d", ans[0]);79 for (i = 1; i < n; i++)80 printf(" %d", ans[i]);81 putchar('\n');82 }83 return 0;84 }

 

转载于:https://www.cnblogs.com/jecyhw/p/3454837.html

你可能感兴趣的文章
python第九天课程:遇到了金角大王
查看>>
字符串处理
查看>>
ECharts(Enterprise Charts 商业产品图表库)初识
查看>>
LeetCode Factorial Trailing Zeroes (阶乘后缀零)
查看>>
hdu 5402 Travelling Salesman Problem (技巧,未写完)
查看>>
[AIR] 获取U盘,打开U盘
查看>>
HtmlUnitDriver 网页内容动态抓取
查看>>
ad logon hour
查看>>
获得进程可执行文件的路径: GetModuleFileNameEx, GetProcessImageFileName, QueryFullProcessImageName...
查看>>
证件照(1寸2寸)拍摄处理知识汇总
查看>>
罗马数字与阿拉伯数字转换
查看>>
Eclipse 反编译之 JadClipse
查看>>
asp.net 获取IP地理位置的几个主要接口
查看>>
Python入门-函数
查看>>
[HDU5727]Necklace(二分图最大匹配,枚举)
查看>>
距离公式汇总以及Python实现
查看>>
设计模式之装饰者模式
查看>>
一道不知道哪里来的容斥题
查看>>
Blender Python UV 学习
查看>>
window添加右键菜单
查看>>