• `算法竞赛题解` 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
  • 相关阅读:
    Java使用javah命令:‘javah‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    建信金融科技--软开笔试题1.答案--java--9.12
    MASA MAUI iOS如何绑定微信
    软件测试学习(一)基础概念、实质、说明书测试、分类、动态黑盒测试
    计算机应用基础练习题题(史上最全)
    BI系统的分布式部署原理和技术实现
    protobuf c语言库 Nanopb的使用方法
    pnpm、npm、yarn的区别
    离散数学 学习 之 递推方程和生成函数
    bp神经网络优化算法对比,bp神经网络的优化算法
  • 原文地址:https://blog.csdn.net/weixin_42712593/article/details/125991596