2724: Cows
Posted 还是力量不够
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2724: Cows相关的知识,希望对你有一定的参考价值。
描述
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: cowi and cowj, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cowi is stronger than cowj.
For each cow, how many cows are stronger than her? Farmer John needs your help!
输入
The input contains multiple test cases.
For each test case, the first line is an integer N (1 <= N <= 105), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 105) 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.
输出
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 cowi.
样例输入
3
1 2
0 3
3 4
0
样例输出
1 0 0
题意
每头奶牛都有一个吃草区域[S,E];如果满足Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj,说明第i头牛比第j头牛强壮。对每头奶牛求比其强壮的奶牛数量。
代码
#include<bits/stdc++.h> using namespace std; const int N=1e5+5; struct node { int S,E,id; }e[N]; int n,maxn; int cmp(node x,node y) { return x.S<y.S||x.S==y.S&&x.E>y.E; } int f[N],num[N]; void update(int x,int data) { for(int i=x;i<N;i+=i&-i) { f[i]+=data; } } int Getsum(int x) { int result=0; for(int i=x;i>=1;i-=i&-i) { result+=f[i]; } return result; } int main() { while(~scanf("%d",&n),n) { for(int i=1;i<=n;i++) { scanf("%d%d",&e[i].S,&e[i].E); e[i].id=i; } sort(e+1,e+n+1,cmp); memset(f,0,sizeof(f)); num[e[1].id]=0; update(e[1].E,1); maxn=e[1].E; for(int i=2;i<=n;i++) { if(e[i].S==e[i-1].S&&e[i].E==e[i-1].E) num[i]=num[i-1]; num[e[i].id]=Getsum(maxn)-Getsum(e[i].E-1); update(e[i].E,1); maxn=max(maxn,e[i].E); } for(int i=1;i<=n;i++) { if(i!=1)printf(" "); printf("%d",num[i]); } printf(" "); } }
以上是关于2724: Cows的主要内容,如果未能解决你的问题,请参考以下文章
BZOJ 2724 2724: [Violet 6]蒲公英 (区间众数不带修改版本)
(节点:2724)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“第一个”