BZOJ 1935

来源:互联网 发布:win7开机优化加速 编辑:程序博客网 时间:2024/06/08 19:28

【题目分析】
树状数组+离线。


【代码】

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;struct event{    int opt;//0 add: 1 que    int x;// position    int y;// position    int ans;//belongs to    int tmp;//+ or -}t[3000001];int top=0,xx,yy,x1,y1,x2,y2,n,m;int ans[500001];int tr[10000001];int rank[3000001];inline bool cmp(int a,int b){return (t[a].x==t[b].x)?t[a].opt<t[b].opt:t[a].x<t[b].x;}inline void add(int k){for (int i=k;k<=10000000;k+=k&(-k)) tr[k]++;}inline int gs(int k){    int ret=0;    for (int i=k;i;i-=i&(-i)) ret+=tr[i];    return ret; }int main(){    scanf("%d%d",&n,&m);    for (int i=1;i<=n;++i)    {        scanf("%d%d",&xx,&yy);        xx++;yy++;        ++top;        rank[top]=top;        t[top].opt=0;        t[top].x=xx;        t[top].y=yy;        t[top].ans=0;        t[top].tmp=0;    }    for (int i=1;i<=m;++i)    {        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);        x1++;x2++;y1++;y2++;        ++top;        rank[top]=top;        t[top].opt=1;        t[top].x=x1-1;        t[top].y=y1-1;        t[top].ans=i;        t[top].tmp=1;        ++top;        rank[top]=top;        t[top].opt=1;        t[top].x=x2;        t[top].y=y2;        t[top].ans=i;        t[top].tmp=1;        ++top;        rank[top]=top;        t[top].opt=1;        t[top].x=x2;        t[top].y=y1-1;        t[top].ans=i;        t[top].tmp=-1;        ++top;        rank[top]=top;        t[top].opt=1;        t[top].x=x1-1;        t[top].y=y2;        t[top].ans=i;        t[top].tmp=-1;    }    sort (rank+1,rank+top+1,cmp);    for (int i=1;i<=top;++i)    {        if (!t[rank[i]].opt) add(t[rank[i]].y);        else        {            ans[t[rank[i]].ans]+=t[rank[i]].tmp*gs(t[rank[i]].y);        }    }    for (int i=1;i<=m;++i)    printf("%d\n",ans[i]);}
0 0
原创粉丝点击