POJ 3416 Crossing(树状数组)

转自:http://blog.csdn.net/acm_cxlove/article/details/8270539

自己的代码又错了..

#include

using namespace std;
#define N 500050
#define up 500050


int l[N+5],r[N+5];
int n,m;
struct node{
    int x,y;
    int id;
    bool friend operator<(node a,node b){
        if(a.x!=b.x)return a.xx;
        return a.y>b.y;
    }
}p[N],q[N];
void update(int *s,int x,int val){
    while(x<=up){
        s[x]+=val;
        x+=(x&(-x));
    };
}
int getsum(int *s,int x){
    int ret=0;
    while(x){
        ret+=s[x];
        x-=(x&(-x));
    }
    return ret;
}

int ans[N];
int main(){
    int T;scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;++i)
            scanf("%d%d",&p[i].x,&p[i].y);
        for(int i=1;i<=m;++i)
            scanf("%d%d",&q[i].x,&q[i].y),q[i].id=i;
        memset(l,0,sizeof(l));memset(r,0,sizeof(r));
        sort(p+1,p+1+n);
        sort(q+1,q+1+m);
        for(int i =1;i<=n;++i)
            update(r,p[i].y+1,1);
        int st=1,ed;
        for(int i=1;i<=m;++i){
            for(ed=st;ed<=n;++ed){
                if(p[ed].x>q[i].x)break;
            }
            for(st;sty+1,1);
                update(r,p[st].y+1,-1);
            }
            int a=getsum(r,up)-getsum(r,q[i].y+1)+getsum(l,q[i].y+1);
            int b=getsum(l,up)-getsum(l,q[i].y+1)+getsum(r,q[i].y+1);
            ans[q[i].id]=abs(a-b);
            //printf("%d\n",st);
        }
        for(int i=1;i<=m;++i)printf("%d\n",ans[i]);
        puts("");
    }
}

别人的代码:
using namespace std;
struct Node{
int x,y,id;
bool operator<(const Node n)const{
return x

你可能感兴趣的:(数据结构-树状数组)