bzoj 1935 园丁的烦恼

1935: [Shoi2007]Tree 园丁的烦恼

Time Limit: 15 Sec  Memory Limit: 357 MB
Submit: 1846  Solved: 838
[Submit][Status][Discuss]

Description

很久很久以前,在遥远的大陆上有一个美丽的国家。统治着这个美丽国家的国王是一个园艺爱好者,在他的皇家花园里种植着各种奇花异草。有一天国王漫步在花园里,若有所思,他问一个园丁道: “最近我在思索一个问题,如果我们把花坛摆成六个六角形,那么……” “那么本质上它是一个深度优先搜索,陛下”,园丁深深地向国王鞠了一躬。 “嗯……我听说有一种怪物叫九头蛇,它非常贪吃苹果树……” “是的,显然这是一道经典的动态规划题,早在N元4002年我们就已经发现了其中的奥秘了,陛下”。 “该死的,你究竟是什么来头?” “陛下息怒,干我们的这行经常莫名其妙地被问到和OI有关的题目,我也是为了预防万一啊!” 王者的尊严受到了伤害,这是不可容忍的。看来一般的难题是难不倒这位园丁的,国王最后打算用车轮战来消耗他的实力: “年轻人,在我的花园里的每一棵树可以用一个整数坐标来表示,一会儿,我的骑士们会来轮番询问你某一个矩阵内有多少树,如果你不能立即答对,你就准备走人吧!”说完,国王气呼呼地先走了。 这下轮到园丁傻眼了,他没有准备过这样的问题。所幸的是,作为“全国园丁保护联盟”的会长——你,可以成为他的最后一根救命稻草。

Input

第一行有两个整数n,m(0≤n≤500000,1≤m≤500000)。n代表皇家花园的树木的总数,m代表骑士们询问的次数。 文件接下来的n行,每行都有两个整数xi,yi,代表第i棵树的坐标(0≤xi,yi≤10000000)。 文件的最后m行,每行都有四个整数aj,bj,cj,dj,表示第j次询问,其中所问的矩形以(aj,bj)为左下坐标,以(cj,dj)为右上坐标。

Output

共输出m行,每行一个整数,即回答国王以(aj,bj)和(cj,dj)为界的矩形里有多少棵树。

Sample Input

3 1
0 0
0 1
1 0
0 0 1 1

Sample Output

3

HINT

 

Source

思路: 1. 离线+ 树状数组

2 cdq分治

其实是一样的,cdq慢一点点。。

对于时间序是一定的,所以在分治的时候我们对x进行排序,然后对y进行维护(树状数组)

代码:

/*
#include<bits/stdc++.h>

using namespace std;

struct node
{
    int x,y;
    int id;
    int op;
}a[2500005];
int maxy;
int n,m;
int ans[500005];
int c[10000005];

bool cmp(node a,node b)
{
    if(a.x==b.x) return abs(a.op)<abs(b.op);
    return a.x<b.x;
}

int lowbit(int x)
{
    return x&(-x);
}

void update(int x,int val)
{
    for(;x<=maxy;x+=lowbit(x)) c[x]+=val;
}

int query(int x,int op)
{
    int sum=0;
    for(;x>0;x-=lowbit(x)) sum+=c[x];
    return sum*op;
}

int main()
{
    int x1,yy1,x2,y2;
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d %d",&a[i].x,&a[i].y);
        a[i].op=0; a[i].x++; a[i].y++;
        maxy=max(maxy,a[i].y);
        a[i].id=0;
    }
    int tot=n;
    for(int i=1;i<=m;i++){
        scanf("%d %d %d %d",&x1,&yy1,&x2,&y2);
        x1++; yy1++; x2++; y2++;
        maxy=max(maxy,y2);
        a[++tot].x=x2; a[tot].y=y2; a[tot].id=i; a[tot].op=1;
        a[++tot].x=x1-1; a[tot].y=yy1-1; a[tot].id=i; a[tot].op=1;
        a[++tot].x=x1-1; a[tot].y=y2; a[tot].id=i; a[tot].op=-1;
        a[++tot].x=x2; a[tot].y=yy1-1; a[tot].id=i; a[tot].op=-1;
    }

    sort(a+1,a+tot+1,cmp);

    for(int i=1;i<=tot;i++){
        if(a[i].id==0){
            update(a[i].y,1);
        }
        else{
            ans[a[i].id]+=query(a[i].y,a[i].op);
        }
    }

    for(int i=1;i<=m;i++){
        printf("%d\n",ans[i]);
    }

    return 0;
}
*/

#include<bits/stdc++.h>

using namespace std;


template <class T>
bool scan_d(T &ret)
{
    char c;
    int sgn;
    T bit = 0.1;
    if (c=getchar(), c==EOF)
    {
        return 0;
    }
    while (c!='-'&& c!='.'&& (c<'0'||c>'9'))
    {
        c = getchar();
    }
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0' && c <= '9')
    {
        ret = ret * 10 + (c - '0');
    }
    if (c == ' ' || c == '\n')
    {
        ret *= sgn;
        return 1;
    }
    while (c = getchar(), c >= '0' && c <= '9')
    {
        ret += (c - '0') * bit, bit /= 10;
    }
    ret *= sgn;
    return 1;
}

template <class T>
inline void print_d(T& x)
{
    if (x > 9)
    {
        print_d(x/10);
    }
    putchar(x % 10 + '0');
}


struct node
{
    int x,y;
    int id;
    int op;
}a[2500005],tmp[2500005];

int c[10000005];
int ans[500005];
int n,m,maxy;
int tot;

bool cmp(node a,node b)
{
    if(a.x==b.x) return abs(a.op)<abs(b.op);
    return a.x<b.x;
}

int lowbit(int x)
{
    return x&(-x);
}

void update(int x)
{
    for(;x<=maxy;x+=lowbit(x)) c[x]++;
}

int query(int x,int op)
{
    int sum=0;
    for(;x>0;x-=lowbit(x)) sum+=c[x];
    return sum*op;
}

void clearr(int x)
{
    for(;x<=maxy;x+=lowbit(x)){
        if(c[x]==0) break;
        c[x]=0;
    }
}

void cdq(int l,int r)
{
    //cout<<"*** l "<<l<<" "<<r<<endl;
    if(l>=r) return ;
    int mid=(l+r)>>1;
    cdq(l,mid);
    cdq(mid+1,r);
    int p,q; int id=l;
    p=l; q=mid+1;
    while(p<=mid&&q<=r){
        if(cmp(a[p],a[q])){
            if(a[p].op==0){
                update(a[p].y);
            }
            tmp[id++]=a[p++];
        }
        else{
            if(a[q].op!=0){
                ans[a[q].id]+=query(a[q].y,a[q].op);
            }
            tmp[id++]=a[q++];
        }
    }
    while(p<=mid){
        tmp[id++]=a[p++];
    }
    while(q<=r){
        if(a[q].op!=0){
            ans[a[q].id]+=query(a[q].y,a[q].op);
        }
        tmp[id++]=a[q++];
    }
    for(int i=l;i<=r;i++){
        clearr(a[i].y);
        a[i]=tmp[i];
    }
}

int main()
{
    int x1,yy1,x2,y2;
    scan_d(n); scan_d(m);
    for(int i=1;i<=n;i++){
        scan_d(a[i].x); scan_d(a[i].y);
        a[i].op=0; a[i].x++; a[i].y++;
        maxy=max(maxy,a[i].y);
        a[i].id=0;
    }
    tot=n;
    for(int i=1;i<=m;i++){
        scan_d(x1); scan_d(yy1); scan_d(x2); scan_d(y2);
        x1++; yy1++; x2++; y2++;
        maxy=max(maxy,y2);
        a[++tot].x=x2; a[tot].y=y2; a[tot].id=i; a[tot].op=1;
        a[++tot].x=x1-1; a[tot].y=yy1-1; a[tot].id=i; a[tot].op=1;
        a[++tot].x=x1-1; a[tot].y=y2; a[tot].id=i; a[tot].op=-1;
        a[++tot].x=x2; a[tot].y=yy1-1; a[tot].id=i; a[tot].op=-1;
    }

    cdq(1,tot);
    /*
    for(int i=1;i<=tot;i++){
        printf("%d %d %d %d\n",a[i].id,a[i].x,a[i].y,a[i].op);
    }
    */

    for(int i=1;i<=m;i++){
        printf("%d\n",ans[i]);
    }

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值