bzoj 1935: [Shoi2007]Tree 园丁的烦恼 ( 树状数组 )

题目:

就是求矩形内点的个数

这个没有修改,但是数据范围很大,所以要离散化,

用树状数组就好了,没有必要用 CDQ .

求ans 的时候,分成求 四个点到原点的矩阵和,加加减减就好了

#include<bits/stdc++.h>
#define low(x) (x & (-x))
using namespace std;
const int N = 1e6+10;
struct node{
    int id,qid,x,y,c;
}f[N*4],g[4*N];
int c[N*4],tim,qtim;
int ans[N],n,m,a[N*4],tot;
bool cmp(node A, node B){
    if (A.x != B.x) return A.x < B.x;
    if (A.y != B.y) return A.y < B.y;
    return A.id < B.id;
}
void add(int x, int y){
    for(; x < N*4; x += low(x)) c[x] += y;
}
int sum(int x){
    int ans = 0;
    for (; x; x -= low(x)) ans += c[x];
    return ans;
}
int main(){
    int x,y,xx,yy;
    scanf("%d%d",&n,&m);
    for (int i = 0; i < n; ++i){
        scanf("%d%d",&x,&y); 
        f[++tim] = (node){tim,0,x,y,1};
        a[tot++] = x; a[tot++] = y;
    }
    for (int i = 0; i < m; ++i){
        scanf("%d%d%d%d",&x,&y,&xx,&yy);
        a[tot++] = x-1; a[tot++] = y-1; a[tot++] = xx; a[tot++] = yy;

        f[++tim] = (node){tim,++qtim,xx,yy,1};
        f[++tim] = (node){tim,qtim,x-1,yy,-1};
        f[++tim] = (node){tim,qtim,xx,y-1,-1};
        f[++tim] = (node){tim,qtim,x-1,y-1,1};
    }
    sort(a,a+tot);
    tot = unique(a,a+tot) - a;
    for (int i = 1; i <= tim; ++i){
        f[i].x = lower_bound(a,a+tot,f[i].x) - a + 1;
        f[i].y = lower_bound(a,a+tot,f[i].y) - a + 1;
    }
    sort(f+1,f+tim+1,cmp);
    for (int i = 1; i <= tim; ++i){
        if (f[i].qid == 0){
            add(f[i].y,1);
        } else{
            ans[f[i].qid] += f[i].c * sum(f[i].y);
        }
    }
    for (int i = 1; i <= qtim; ++i)
        printf("%d\n",ans[i]);
    return 0;
}
/*
3 1
0 0 
0 1
1 0
0 0 1 1

*/

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值