• `算法竞赛题解` Monitor


    题目链接

    题目描述

    Xiaoteng has a large area of land for growing crops, and the land can be seen as a rectangle of n×m.

    But recently Xiaoteng found that his crops were often stolen by a group of people, so he decided to install some monitors to find all the people and then negotiate with them.

    However, Xiao Teng bought bad monitors, each monitor can only monitor the crops inside a rectangle. There are p monitors installed by Xiaoteng, and the rectangle monitored by each monitor is known.

    Xiao Teng guess that the thieves would also steal q times of crops. he also guessed the range they were going to steal, which was also a rectangle. Xiao Teng wants to know if his monitors can see all the thieves at a time.

    Input
    There are mutiple test cases.

    Each case starts with a line containing two integers n , m ( 1 ≤ n , 1 ≤ m , n × m ≤ 1 0 7 ) n,m(1≤n,1≤m,n×m≤10^7) n,m(1n,1m,n×m107) which represent the area of the land.

    And the secend line contain a integer p ( 1 ≤ p ≤ 1 0 6 ) p(1≤p≤10^6) p(1p106) which represent the number of the monitor Xiaoteng has installed. This is followed by p lines each describing a rectangle. Each of these lines contains four intergers x 1 , y 1 , x 2 a n d y 2 ( 1 ≤ x 1 ≤ x 2 ≤ n , 1 ≤ y 1 ≤ y 2 ≤ m ) x1,y1,x2 and y2(1≤x1≤x2≤n,1≤y1≤y2≤m) x1,y1,x2andy2(1x1x2n,1y1y2m) ,meaning the lower left corner and upper right corner of the rectangle.

    Next line contain a integer q ( 1 ≤ q ≤ 1 0 6 ) q(1≤q≤10^6) q(1q106) which represent the number of times that thieves will steal the crops.This is followed by q lines each describing a rectangle. Each of these lines contains four intergers x 1 , y 1 , x 2 x1,y1,x2 x1,y1,x2 and y2 ( 1 ≤ x 1 ≤ x 2 ≤ n , 1 ≤ y 1 ≤ y 2 ≤ m ) (1≤x1≤x2≤n,1≤y1≤y2≤m) (1x1x2n,1y1y2m),meaning the lower left corner and upper right corner of the rectangle.

    Output
    For each case you should print q lines.

    Each line containing YES or NO mean the all thieves whether can be seen.

    Sample Input
    6 6
    3
    2 2 4 4
    3 3 5 6
    5 1 6 2
    2
    3 2 5 4
    1 5 6 5
     
    
    Sample Output
    YES
    NO
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    题解

    先二维差分, 来支持区间修改操作.
    然后对差分结果进行二维前缀和
    将该二维前缀和根据> 0得到Bool矩阵 (或者说是: 01矩阵), 对其进行二维前缀和, 支持区间查询操作.
    区间值 等于 区间面积, 说明该区域全是1


    由于差分和前缀和有很多会下标越界, 需要特判, 比较麻烦, 有一个Trick

    int & Sum_at( int _x, int _y){
        static int Error;
        if( IS_IN_RANGE_( _x, 0, N-1) && IS_IN_RANGE_( _y, 0, M-1)){
            return Sum[ _x][ _y];
        }
        return Error = 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    突破编程_C++_面试(STL 编程 vector )
    Pytorch 多卡并行(2)—— 使用 torchrun 进行容错处理
    Win10系统总是重复安装更新怎么办?
    QCN6274 VS QCN9274|QCN9074 VS QCN6122|Exploring the World of Wi-Fi 6E
    Ubuntu Linux adb shell
    C++入门-day02
    黑马瑞吉外卖之购物车功能开发(添加购物车和购物车数据展示)
    【docker】虚拟化和docker容器概念
    十月券商金工精选
    【目标检测】yolov3特征提取网络------Darknet53网络及pytorch实现
  • 原文地址:https://blog.csdn.net/weixin_42712593/article/details/125991596