Points in Rectangle (II) LightOJ - 1267

传送门

题目大意:平面上给定n个点,有m个矩形,每次询问矩形内(含边界)点的数量

思路:扫描线(树状数组)+离散化

离线处理

把每个矩形拆成两条扫描线(上底和下底),则对于每个矩形询问,我们设矩形左下顶点为x1,y1,右上顶点为x2,y2,f(x)为高度小于等于x的点的数量,g(x)为高度小于x的点数量,答案则为f(y2) - g(y1) ,其中[x1,x2],因为算点的时候含边界,对于上底要算高度小于等于它的点,下底只要小于即可

那我们如何考虑矩形左右端呢——用树状数组维护区间。

先将所有扫描线从低到高排序,如果当前是上底,则把高度小于等于它的点加入树状数组中;如果当前是下底,则把高度小于它的点加入。对于[x1,x2]内数的数量,用一下前缀和作差就好了。

注意:

离散数组要开3倍,n个点,2m条扫描线

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
using namespace std;
#define _for(i,a,b) for(int i=(a) ;i<=(b) ;i++)
#define _rep(i,a,b) for(int i=(a) ;i>=(b) ;i--)
#define mst(v,s) memset(v,s,sizeof(v))
#define pb push_back
#define IOS ios::sync_with_stdio(false)
#define int long long
#define inf 0x3f3f3f3f
#define lson p<<1,l,mid
#define rson p<<1|1,mid+1,r
#define ls p<<1
#define rs p<<1|1
#define endl "\n"
#define sf scanf
typedef long long ll;
const int N = 3*(5e4 + 10);
int n,q;
int xx[N],tot;//x离散数组
struct point//输入的点
{
    int x, y;
    bool operator < (const point& b) const//按照y从小到大排序
    {
        return y < b.y;
    }
}p[N];
struct Q//扫描线左右端,高度,编号,原矩形的上底or下底
{
    int x1, x2, y, id,f;//f 1下 -1上
    int res;
}qu[N<<1];
bool cmp1(Q a, Q b)//按照扫描线高度排序
{
    if (a.y != b.y) return a.y < b.y;
    return a.f > b.f;
}
bool cmp2(Q a, Q b)//按照输入顺序排序,因为最后答案要按顺序输出
{
    if (a.id != b.id) return a.id < b.id;
    return a.f > b.f;
}
int c[N];
int lowbit(int x) {return x & (-x);}
int sum(int x)
{
    int ans = 0;
    while ( x>0 )
    {
        ans += c[x];
        x -= lowbit(x);
    }
    return ans;
}
void add(int i, int val)
{
    while ( i<=tot )
    {
        c[i] += val;
        i += lowbit(i);
    }
}
signed main()
{
    ///!!!
    //freopen("data.txt","r",stdin);
    //!!!
    IOS;
    int T; cin >> T;
    int TT = 0;
    while (T--)
    {
        mst(xx, 0);
        mst(c, 0);
        mst(qu, 0);
        mst(p, 0);
        tot = 0;

        cin >> n >> q;
        //读入所有点,并离散化
        _for(i, 1, n)
        {
            int x, y; cin >> x >> y; x++, y++;
            p[i] = { x,y};
            xx[++tot] = x;
        }
        xx[++tot] = 0;//最前面加个0
        _for(i, 1, q)
        {
            int a, b, c, d; cin >> a >> b >> c >> d;
            a++, b++, c++, d++;
            qu[i] = { a,c,b,i,1,0 };
            qu[i + q] = { a,c,d,i,-1,0 };
            xx[++tot] = a, xx[++tot] = c;//扫描线的左右端也要加入离散数组
        }
        sort(xx + 1, xx + 1 + tot);//所有x排序
        sort(p + 1, p + 1 + n);//对所有点升序排

        tot = unique(xx + 1, xx + 1 + tot) - (xx + 1);//对x数组去重,为了离散
        sort(qu + 1, qu + 1 + 2 * q, cmp1);//对所有询问按照高度升序排
        int j = 1;
        _for(i, 1, 2 * q)
        {
            if (qu[i].f == 1)//如果当前扫描线是原矩形下底,加入所有 小于 扫描线高度的点
            {
                while (p[j].y < qu[i].y && j <= n)
                {
                    int k = lower_bound(xx + 1, xx + 1 + tot, p[j].x) - xx;
                    add(k, 1);
                    j++;
                }
            }
            else//如果当前扫描线是原矩形上底,加入所有 小于等于 扫描线高度的点
            {
                while (p[j].y <= qu[i].y && j <= n)
                {
                    int k = lower_bound(xx + 1, xx + 1 + tot, p[j].x) - xx;
                    add(k, 1);
                    j++;
                }
            }
            int x1 = lower_bound(xx + 1, xx + 1 + tot, qu[i].x1) - (xx);
            int x2 = lower_bound(xx + 1, xx + 1 + tot, qu[i].x2) - (xx);
            qu[i].res = sum(x2) - sum(x1 - 1);//每个扫描线存小于/小于等于它高度的点的数量
        }
        sort(qu + 1, qu + 1 + 2 * q, cmp2);//将扫描线按照读入顺序排序
        TT++;
        cout << "Case " << TT << ":" << endl;
        _for(i, 1, q)
        {
            //矩形上底存的res - 矩形下底存的res
            cout << qu[2 * i - 1 + 1].res - qu[2 * i - 1].res << endl;
        }
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值