North American Invitational Programming Contest (NAIPC) 2019 intersectingrectangles

本文介绍了一种使用扫描线和树状数组检测二维平面上多个轴对齐矩形是否相交的算法。通过将每个矩形转换为两条线,并按Y坐标排序,算法能够高效地确定是否存在至少一对相交的矩形。

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

You are given a set of n axis-aligned rectangles in a 2D plane. For this problem, two rectangles are considered to intersect if their boundaries contain any common points (in particular, two nesting rectangles don’t count as intersecting). Determine if some pair of rectangles intersect.

在这里插入图片描述
In this example, only rectangles A and B intersect.

Input
Each test case will begin with a line with a single integer n (1≤n≤105), which is the number of rectangles.

Each of the next n lines will contain four space-separated integers:

x1 y1 x2 y2
(−109≤x1,y1,x2,y2≤109,x1<x2,y1<y2), which describe a rectangle, where (x1,y1) is the lower left corner and (x2,y2) is the upper right corner. All x values will be distinct. All y values will be distinct.

Output
Output a single integer, which is 1 if some pair of rectangles intersect, 0 if no pair of rectangles intersect.

Sample Input
3 0 0 2 2 1 1 3 4 5 7 6 8
Sample Output
1
Sample Input
4 0 0 20 20 1 1 3 4 2 10 9 12 11 3 19 18
Sample Output
0

题目大意:给n个矩形,问是否有相交的矩形,不存在重复的边,每个矩形用左下角和右上角两个点表示
思路:扫描线+树状数组或者线段树
之前做过一个求矩形面积的,用的是线段树维护,发现很麻烦,那个题用树状数组维护应该也是可以的
树状数组的话直接用一维的单点更新和区间查询就行
每个矩形看作两条线,按y排好序,从下往上扫描
遇见下边(flag==1的线)就判断一下这个区间是否有数字,也就是区间被加过,没有就更改两个端点,当作区间更新,否则break。
遇见上边(flag=0的线) 就两个端点都减1,减完后判断区间里面是否还有数字,有就break,说明中间夹杂着其他的线。
最后代码处理的话还要离散化一下数据即可,还有就是数组要开大,因为存的是线,是n的2倍
代码:

#include <bits/stdc++.h>

using namespace std;
const int maxn=1e5+100;
typedef long long ll;
int c[maxn<<2]; //树状数组
int len ;

struct line
{
    int l,r,h,flag;
}a[maxn<<2];
int lowbit(int x){
    return x&(-x);
}

void update(int i,int k){    //在i位置加上k
    while(i <= maxn*2){
        c[i] += k;
        i += lowbit(i);
    }
}

int getsum(int i){        //求A[1 - i]的和

    int res = 0;
    while(i > 0){
        res += c[i];
        i -= lowbit(i);
    }
    return res;
}
bool cmp(line l1,line l2)
{
    return l1.h<l2.h;
}
vector<int> v;
int getid(int x)
{
    return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
}
int main()
{
    int t;
    scanf("%d",&t);
    int x1,x2,y1,y2;
    int cnt=0;
    while(t--)
    {
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        v.push_back(x1);
        v.push_back(x2);
        a[cnt++]={x1,x2,y1,1};
        a[cnt++]={x1,x2,y2,-1};
    }
    sort(a,a+cnt,cmp);
    sort(v.begin(),v.end());
    v.erase( unique(v.begin(),v.end()) , v.end());//去重

    int l,r;
    int ff=0;
    for(int i=0;i<cnt;i++)
    {
        l=getid(a[i].l);
        r=getid(a[i].r);
        int sum;
        if(a[i].flag==1)
        {
            sum=getsum(r)-getsum(l-1);
            if(sum!=0)
            {
                ff=1;break;
            }
            update(l,1);
            update(r,1);

        }
        else
        {
            update(l,-1);
            update(r,-1);
            sum=getsum(r)-getsum(l-1);
            if(sum!=0)
            {
                ff=1;break;
            }

        }
    }
    printf("%d\n",ff);
    return 0;
}
/*
2
0 0 2 2
1 1 3 4

3
0 0 9 9
1 1  8 8
2 2  7 7

2
0 0 5 5
2 2 9 9

2
0 0 9 9
5 1 20 2



2
3 2 8 3
4 1 5 4

2
1 1 4 3
2 2 3 4

2
1 2 4 4

*/

<think>好的,用户想了解AIME24的具体含义和相关背景。首先,我需要确定这个术语的常见领域。AIME可能是一个缩写,常见的有数学竞赛,比如美国数学邀请赛(AIME)。24可能指的是年份或版本。 先搜索"AIME24"的定义。发现AIME通常指American Invitational Mathematics Examination,是AMC系列竞赛的下一阶段,通常AMC10/12优秀选手参加。而24可能指2024年的比赛,或者是第24届?不过AIME一般每年举办两次,分为AIME I和AIME II,所以24可能不是届数,而是题目编号或年份。 再查具体背景,AIME比赛题目编号通常用年份加罗马数字,比如AIME 2024的问题可能被标记为2024 AIME I Problem 1。但用户提到的AIME24可能指某个特定题目,比如第24题,或者是某个项目的名称。 另外,可能有其他领域使用AIME24,比如医学或工程,但根据常见性,数学竞赛更可能。需要确认是否有AIME第24题的相关内容,或者2024年的AIME比赛信息。 搜索结果可能显示,AIME24可能指2024年的AIME竞赛,或者是某个题目的编号。比如,在数学论坛上,用户可能讨论AIME II 2024的第24题。需要整合这些信息,说明AIME的基本背景,再解释24可能的含义,如年份、题号或版本。 还要注意避免引用堆砌,把信息分散到步骤中,每个方法或可能性分开说明。最后生成相关问题,比如如何准备AIME,AIME24题目的解法,或者AIME与其他竞赛的区别。</think>AIME是**美国数学邀请赛**(American Invitational Mathematics Examination)的缩写,由美国数学协会(MAA)组织,面向在AMC10/12竞赛中表现优异的学生。AIME24可能指**2024年AIME竞赛**或该竞赛中的**第24题**,需结合具体语境判断。 **竞赛背景** AIME是AMC系列竞赛的进阶赛事,每年举办两次(AIME I和AIME II),共15道证明题,满分15分。题目难度逐级递增,第24题通常为高难度题目,涉及组合数学、数论或几何等领域的综合应用。 **AIME24的可能含义** 1. **2024年AIME赛事**:如"AIME24"指代年份,可能涉及2024年的竞赛安排、题目特点或参赛数据。 2. **AIME第24题**:若为题目编号,通常出现在模拟题或历史真题中,例如: ```python # 示例:AIME 2024第24题可能涉及的几何问题 def calculate_area(radius): return 2 * radius**2 * (1 + math.sqrt(2)) ``` 3. **其他领域**:在医学或工程学中可能为缩写,但数学竞赛为最常见场景。 **典型题目特征** AIME高难度题常需构造性思维,例如: - 数论题可能涉及模运算与不定方程,如求解方程 $x^2 + y^2 = 2024$ 的整数解数量。 - 组合题可能要求用递推公式 $a_n = 3a_{n-1} - a_{n-2}$ 计算特定排列数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值