您的位置:首页 > 其它

poj2481 树状数组

2015-10-08 15:39 260 查看
树状数组模板题,如果有牛的区域能全覆盖目前牛的区域,那么该牛比目前牛强壮

首先按e从大到小排,如果一样按s从小到大排

a数组记录的是ans,及到x为止的前缀和

注意两个区域相等时结果也相等而不算全覆盖

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
#define FOR(i, l, r) for(int i = l; i <= r; i++)
#define REP(i, r, l) for(int i = r; i >= l; i--)
typedef long long ll;
double eps = 1e-6;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
struct coww
{
int s,e,id;
friend bool operator <(const coww &nd1,const coww &nd2)
{
if(nd1.e == nd2.e)
return nd1.s < nd2.s; //如果e相等,s从小到大排
return nd1.e > nd2.e;  //e从大到小排
}
}cow[100010];
int c[100010];
int a[100010];
int lowbit(int x)
{
return x & (-x);
}
void modify(int x)    //x位置修改(+1)后c[x]的修改
{
while(x <= 100010)
{
c[x] ++ ;
x += lowbit(x);
}
}
int sum(int x)   //x的前缀和
{
int ret=0;
while(x > 0)
{
ret += c[x];
x-=lowbit(x);
}
return ret;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
if(n== 0) break;
memset(a,0,sizeof(a));
memset(c,0,sizeof(c));
FOR(i,1,n)
{
scanf("%d%d", &cow[i].s, &cow[i].e);
cow[i].s++ , cow[i].e++;
cow[i].id=i;
}
sort( cow+1 , cow+n+1);
FOR(i,1,n)
{
if(cow[i].s == cow[i-1].s &&cow[i].e == cow[i-1].e)
a[cow[i].id]=a[cow[i-1].id];
else
a[cow[i].id]=sum(cow[i].s);
modify(cow[i].s);
}
FOR(i,1,n-1)
printf("%d ",a[i]);
printf("%d\n",a
);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: