poj2481 树状数组

来源:互联网 发布:开淘宝网店步骤 编辑:程序博客网 时间:2024/06/06 16:31

树状数组模板题,如果有牛的区域能全覆盖目前牛的区域,那么该牛比目前牛强壮

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

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

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

树状数组可以解决的:点修改,查询前缀和,每次修改和查询前缀和都是logn

#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[n]);    }        return 0;}


0 0