将e按从大到小排序,统计前i-1个中比

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct Item
{
int s;
int e;
int x;
};
int n;
Item item[500010];
int a[500010],b[500010];
int lowbit(int x)
{
return x&(-x);
}
int sum(int x)
{
int ret=0;
while(x>0)
{
ret=ret+a[x];
x-=lowbit(x);
}
return ret;
}
void add(int x,int d)
{
while(x<500001)
{
a[x]+=d;
x+=lowbit(x);
}
}
bool cmp(Item a,Item b)
{
return a.e>b.e||((a.e==b.e)&&(a.s<b.s));
}
int main()
{
while(scanf("%d",&n)&&n)
{
memset(a,0,sizeof(a));
for(int i=0;i<n;i++)
{
scanf("%d%d",&item[i].s,&item[i].e);
item[i].s++,item[i].e++;
item[i].x=i;
}
sort(item,item+n,cmp);
b[item[0].x]=sum(item[0].s);
add(item[0].s,1);
for(int i=1;i<n;i++)
{
if(item[i].e==item[i-1].e&&item[i].s==item[i-1].s)
{
b[item[i].x]=b[item[i-1].x];
add(item[i].s,1);
}
else{
b[item[i].x]=sum(item[i].s);
add(item[i].s,1);
}
}
for(int i=0;i<n;i++)
cout<<b[i]<<" ";
cout<<endl;
}
return 0;
}

e小的个数,注意相等的情况

poj_2481,Cows,树状数组的更多相关文章

  1. POJ2481:Cows(树状数组)

    Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ( ...

  2. POJ 2481:Cows 树状数组

    Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 14906   Accepted: 4941 Description ...

  3. poj 2481 - Cows(树状数组)

    看的人家的思路,没有理解清楚,,, 结果一直改一直交,,wa了4次才交上,,, 注意: 为了使用树状数组,我们要按照e从大到小排序.但s要从小到大.(我开始的时候错在这里了) 代码如下: #inclu ...

  4. poj2481 Cows 树状数组

    题目链接:http://poj.org/problem?id=2481 解题思路: 这道题对每组数据进行查询,是树状数组的应用.对于二维的树状数组, 首先想到排序.现在对输入的数据按右值从大到小排序, ...

  5. 【POJ2182】Lost Cows 树状数组+二分

    题中给出了第 i 头牛前面有多少比它矮,如果正着分析比较难找到规律.因此,采用倒着分析的方法(最后一头牛的rank可以直接得出),对于第 i 头牛来说,它的rank值为没有被占用的rank集合中的第A ...

  6. poj2182Lost Cows——树状数组快速查找

    题目:http://poj.org/problem?id=2182 从后往前确定,自己位置之前没有被确定的且比自己编号小的个数+1即为自己的编号: 利用树状数组快速查找,可另外开一个b数组,角标为编号 ...

  7. POJ2182 Lost Cows 树状数组

    题意:有编号1~n乱序排列的奶牛,给出了每一个奶牛前小于自己编号的奶牛数目 维护一个树状数组,下标是编号,值为$0/1$标识是否存在,很显然最后一个牛的编号是知道的,我们在树状数组上二分出前缀和为小于 ...

  8. POJ 2182 Lost Cows (树状数组 && 二分查找)

    题意:给出数n, 代表有多少头牛, 这些牛的编号为1~n, 再给出含有n-1个数的序列, 每个序列的数 ai 代表前面还有多少头比 ai 编号要小的牛, 叫你根据上述信息还原出原始的牛的编号序列 分析 ...

  9. POJ 2481 Cows(树状数组)

                                                                      Cows Time Limit: 3000MS   Memory L ...

随机推荐

  1. ubuntu 14.04服务器上使用nginx搭建wordpress博客详解

    过程详解 1.更新apt-get sudo apt-get update 2.安装nginx sudo apt-get install nginx 3.启动nginx sudo service ngi ...

  2. jquery-常用插件集合

    001.弹出消息插件toastr https://github.com/CodeSeven/toastr 002.弹出页面全屏插件 https://github.com/sindresorhus/sc ...

  3. C++11时间具体解释

    转载请注明出处:http://blog.csdn.net/luotuo44/article/details/46854229 C++ 11添加了三个与时间相关的类型:时间段.时钟.时间点. 以史为鉴 ...

  4. SQL语句将某字段查询出以逗号隔开

    MySQL的sql语句有好多能够省去server端的复杂处理 1.group_concat 这玩意儿能够实现  将一个字段如id查询出来   成为这种格式:121,122,123,124,125,12 ...

  5. 从头认识java-16.4 nio的读与写(ByteBuffer的使用)

    这一章节我们来讨论一下nio的读与写. 1.nio的读 package com.ray.ch16; import java.io.IOException; import java.io.RandomA ...

  6. iOS 常见小问题

    1. iOS 编译后上下有黑边 ? 缺少启动图片 2.Failed to instantiate the default view controller for UIMainStoryboardFil ...

  7. silverlight wpf DataTemplate Command binding

    <Grid x:Name="LayoutRoot" Background="White"> <CommonControl:NoapDataGr ...

  8. kentico version history and upgrade

    Version history Kentico 10: November 30, 2016 Kentico 9: November 24, 2015 Kentico 8.2: January 6, 2 ...

  9. zzuoj--1001--汽水瓶(简单数学)

    1001: 汽水瓶 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 194  Solved: 77 [Submit][Status][Web Board ...

  10. Service Mesh(服务网格)

    Service Mesh(服务网格) 什么是Service Mesh(服务网格)Service mesh 又译作 "服务网格",作为服务间通信的基础设施层.Buoyant 公司的 ...