POJ 2481-Cows(BIT)
题意:
n个牛,每个牛对应一个区间,对于每个牛求n个区间有几个包含该牛的区间。
分析:
先 区间右边界从大到小排序,相同时左边界小到大,统计第i头牛即左边界在前i-1头左边界的正序数。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
#define N 100010
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
int bit[N],tmp[N],n;
struct node{
int s,e,index;
}a[N];
bool cmp(node x,node y){
if(y.e==x.e)
return x.s<y.s;
else
return x.e>y.e;
}
int sum(int x){
int num=;
while(x>){
num+=bit[x];
x-=(x&(-x));
}
return num;
}
void add(int x,int d){
while(x<=N){
bit[x]+=d;
x+=(x&(-x));
}
}
void solve(){
memset(bit,,sizeof(bit));
sort(a,a+n,cmp);
tmp[a[].index]=;
add(a[].s,);
for(int i=;i<n;++i){
if(a[i].s==a[i-].s&&a[i].e==a[i-].e){
tmp[a[i].index]=tmp[a[i-].index];
}
else{
// cout<<i<<" "<<sum(a[i].e)<<endl;
tmp[a[i].index]=sum(a[i].s);
}
add(a[i].s,);
}
for(int i=;i<n;++i){
if(i==n-)
printf("%d\n",tmp[i]);
else
printf("%d ",tmp[i]);
}
}
int main()
{
while(~scanf("%d",&n)){
if(n==)break;
for(int i=;i<n;++i){
scanf("%d%d",&a[i].s,&a[i].e);
a[i].s++;
a[i].e++;
a[i].index=i;
}
solve();
}
return ;
}
POJ 2481-Cows(BIT)的更多相关文章
- 2018.07.08 POJ 2481 Cows(线段树)
Cows Time Limit: 3000MS Memory Limit: 65536K Description Farmer John's cows have discovered that the ...
- poj 2481 Cows(数状数组 或 线段树)
题意:对于两个区间,[si,ei] 和 [sj,ej],若 si <= sj and ei >= ej and ei - si > ej - sj 则说明区间 [si,ei] 比 [ ...
- POJ 2481 Cows(树状数组)
Cows Time Limit: 3000MS Memory L ...
- poj 2481 Cows(树状数组)题解
Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ( ...
- 2018.07.03 POJ 3348 Cows(凸包)
Cows Time Limit: 2000MS Memory Limit: 65536K Description Your friend to the south is interested in b ...
- POJ 2431 Expedition(探险)
POJ 2431 Expedition(探险) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] A group of co ...
- POJ 3281 Dining (网络流)
POJ 3281 Dining (网络流) Description Cows are such finicky eaters. Each cow has a preference for certai ...
- POJ 3414 Pots(罐子)
POJ 3414 Pots(罐子) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 You are given two po ...
- POJ 1847 Tram (最短路径)
POJ 1847 Tram (最短路径) Description Tram network in Zagreb consists of a number of intersections and ra ...
- 树状数组 POJ 2481 Cows
题目传送门 #include <cstdio> #include <cstring> #include <algorithm> using namespace st ...
随机推荐
- 在DataTable中执行DataTable.Select("条件")
.在DataTable中执行DataTable.Select("条件")返回DataTable: // <summary> // 执行DataTable中的查询返回 ...
- linux 安装firefox
从火狐官网下载的firefox-9.0.1.tar.bz2解压后,进入firefox文件夹,执行./firefox会提示缺少库,故采用yum安装Firefox9.1.切换到root用户 su - 2. ...
- 通过PLSQL Developer导入SQL文件
1.点击“工具” 2.选中其中的“导入表(L)”,在按下图所示操作: PLSQL Developer会自动运行相关命令,在Tables中刷新即可看到新导入的表.
- PAT-乙级-1034. 有理数四则运算(20)
1034. 有理数四则运算(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求编写程序,计算2个有理 ...
- 谈 DevOps 自动化时,也应该考虑到 SOX 等法案
[编者按]作者 Aaron Volkmann 是 CERT Division 高级研究员,在本文中,他对 DevOps 自动化违反 SOX 法案进行了阐述.同时,也简单的提出了如何通过 CI 来避免这 ...
- Linux---Ls命令 初级实现
By xxx0624Done: ls ls -a ls -l ls /tmp ls -R ls -t FileName color FileName o ...
- HDU4647+贪心
/* 贪心. 题意:给定一些点 一些边 点和边都有价值.现在A B 选点.求A-B的maxVal 思路:分割边.边的1/2分给两个端点. 如果这两个点被同一个人取,则ok:否则 做减法也行,对题意无影 ...
- JNDI学习总结(二)——Tomcat下使用C3P0配置JNDI数据源
一.C3P0下载 C3P0下载地址:http://sourceforge.net/projects/c3p0/files/?source=navbar
- C中如何调用C++函数、类内函数 \混编\链接
在C中如何调用C++函数的问题,简单回答是将函数用extern "C"声明,当被问及如何将类内成员函数声明时,一时语塞,后来网上查了下,网上有一翻译C++之父的文章可以作为解答,遂 ...
- JVM内存管理(一)
方法区: 方法区存放了要加载的类的信息(名称.修饰符等).类的静态变量.类中定义为final类型的常量.类中的field信息.类中的方法信息.当开发人员在程序中通过Class对象的getName.is ...