bzoj 1199 二分暴力

本文介绍了一种针对特定数据范围(25W)的问题解决方法,通过将所有点按x坐标排序后,利用upper_bound和lower_bound查找符合矩形和圆形边界条件的点,并进一步判断这些点是否完全位于指定形状内部。该方法巧妙运用了upper_bound和lower_bound函数,实现了高效的暴力枚举。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这道题的数据范围是25W...

直接暴力。。。

将所有点按照x排序之后。遍历所有的矩形和圆,用upper_bound和lower_bound寻找出符合图形x坐标的所有点,(位于区间[s,t]),然后暴力枚举刚才寻找的点判断是否在当前形状内。。

好黄好暴力。upper_bound和lower_bound的新用法~~

#include <algorithm>
#include <algorithm>
#include <iostream>
#include<string.h>
#include <fstream>
#include <math.h>
#include <vector>
#include <cstdio>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define exp 1e-8
#define fi first
#define se second
#define ll long long
#define INF 0x3f3f3f3f
#define lson l,mid,rt<<1
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define rson mid+1,r,(rt<<1)+1
#define all(a) a.begin(),a.end()
#define mm(a,b) memset(a,b,sizeof(a));
#define for1(a,b) for(int a=1;a<=b;a++)//1---(b)
#define rep(a,b,c) for(int a=b;a<=c;a++)//b---c
#define repp(a,b,c)for(int a=b;a>=c;a--)///
using namespace std;
void bug(string m="here"){cout<<m<<endl;}
template<typename __ll> inline void READ(__ll &m){__ll x=0,f=1;char ch=getchar();while(!(ch>='0'&&ch<='9')){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}m=x*f;}
template<typename __ll>inline void read(__ll &m){READ(m);}
template<typename __ll>inline void read(__ll &m,__ll &a){READ(m);READ(a);}
template<typename __ll>inline void read(__ll &m,__ll &a,__ll &b){READ(m);READ(a);READ(b);}
template<typename __ll>inline void read(__ll &m,__ll &a,__ll &b,__ll &c){READ(m);READ(a);READ(b);READ(c);}
template < class T > T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template < class T > T lcm(T a, T b) { return a / gcd(a, b) * b; }
template < class T > inline void rmin(T &a, const T &b) { if(a > b) a = b; }
template < class T > inline void rmax(T &a, const T &b) { if(a < b) a = b; }
template < class T > T pow(T a, T b) { T r = 1; while(b > 0) { if(b & 1) r = r * a; a = a * a; b /= 2; } return r; }
template < class T > T pow(T a, T b, T mod) { T r = 1; while(b > 0) { if(b & 1) r = r * a % mod; a = a * a % mod; b /= 2; } return r; }

const int maxn=250000+100;
int ans[maxn];
struct POINT
{
    double x,y;int idx;
    bool operator <(const POINT &a)const {
        return x<a.x;
    }
    POINT(){}
    POINT(double a,double b)
    {
        x=a,y=b;
    }
}point[maxn];int cnt_point=0;

struct square
{
    POINT p1,p2;
    void read()
    {
        scanf("%lf %lf %lf %lf",&p1.x,&p1.y,&p2.x,&p2.y);
    }
    bool insize(POINT tmp)
    {
        if(tmp.x>p1.x&&tmp.x<p2.x&&tmp.y>p1.y&&tmp.y<p2.y)
            return 1;
        return 0;
    }
    void cal()
    {
        int s=upper_bound(point,point+cnt_point,p1)-point;
        int t=lower_bound(point,point+cnt_point,p2)-point-1;
        for(int i=s;i<=t;i++)
            if(insize(point[i]))
                ans[point[i].idx]++;
    }
}squ[maxn];int cnt_squ=0;

struct circle
{
    POINT p;
    double r;
    void read()
    {
        scanf("%lf %lf %lf",&p.x,&p.y,&r);
    }
    double dist(POINT tmp)
    {
        return (tmp.x-p.x)*(tmp.x-p.x)+(tmp.y-p.y)*(tmp.y-p.y);
    }
    bool insize(POINT tmp)
    {
        if(dist(tmp)<r*r)
            return 1;
        return 0;
    }
    void cal()
    {
        int s=upper_bound(point,point+cnt_point,POINT(p.x-r,0.0))-point;
        int t=lower_bound(point,point+cnt_point,POINT(p.x+r,0.0))-point-1;
        for(int i=s;i<=t;i++)
            if(insize(point[i]))
                ans[point[i].idx]++;
    }
}cir[maxn];int cnt_cir=0;
int main()
{
    int n,m;
    read(n,m);
    cnt_point=m;
    char ch[200];
    while(n--)
    {
        scanf("%s",ch);
        if(ch[0]=='r')
            squ[cnt_squ++].read();
        else cir[cnt_cir++].read();
    }
    rep(i,0,m-1)scanf("%lf %lf",&point[i].x,&point[i].y),point[i].idx=i;
    sort(point,point+m);
    rep(i,0,cnt_squ-1)squ[i].cal();
    rep(i,0,cnt_cir-1)cir[i].cal();
    rep(i,0,m-1)printf("%d\n",ans[i]);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值