我们给出了一个(轴对齐的)矩形列表 rectangles 。 对于 rectangle[i] = [x1, y1, x2, y2],其中(x1,y1)是矩形 i 左下角的坐标,(x2,y2)是该矩形右上角的坐标。

找出平面中所有矩形叠加覆盖后的总面积。 由于答案可能太大,请返回它对  ^  +  取模的结果。

示例 :

输入:[[,,,],[,,,],[,,,]]
输出:
解释:如图所示。
示例 : 输入:[[,,,]]
输出:
解释:答案是 ^ 对 (^ + ) 取模的结果, 即 (^)^ → (-)^ = 。
提示: <= rectangles.length <=
rectanges[i].length =
<= rectangles[i][j] <= ^
矩形叠加覆盖后的总面积不会超越 ^ - ,这意味着可以用一个 位有符号整数来保存面积结果。

解题算法:

class Solution {
public: static bool cmp(vector<int> a,vector<int> b){
return a[] < b[];
} int getDeep(vector<vector<int>>& rectangles,int x1,int x2){
vector<pair<int,int>> deep;
int length = ;
int start;
int end; for(int i = ;i < rectangles.size(); ++i){
if(x1 >= rectangles[i][] && x2 <= rectangles[i][]){
if(deep.empty()){
deep.push_back(make_pair(rectangles[i][],rectangles[i][]));
}else{
end = deep[deep.size()-].second;
if(rectangles[i][] <= end){
start = deep[deep.size()-].first;
end = max(end,rectangles[i][]);
deep.pop_back();
deep.push_back(make_pair(start,end));
}else{
deep.push_back(make_pair(rectangles[i][],rectangles[i][]));
}
}
}
} for(int i = ;i < deep.size(); ++i){
length += deep[i].second - deep[i].first;
} return length;
} int rectangleArea(vector<vector<int>>& rectangles) {
long long mod = + ;
long long area = ;
vector<long long> X; for(int i = ;i < rectangles.size(); ++i){
X.push_back(rectangles[i][]);
X.push_back(rectangles[i][]);
} sort(X.begin(),X.end());
sort(rectangles.begin(),rectangles.end(),cmp);
vector<long long>::iterator end_unique = unique(X.begin(), X.end());
X.erase(end_unique, X.end()); for(int i = ;i < X.size(); ++i){
long long height = getDeep(rectangles,X[i-],X[i]);
long long width = X[i]-X[i-];
area += (width*height)%mod;
area = area%mod;
} return area;
}
};

850. 矩形面积 II的更多相关文章

  1. [Swift]LeetCode850. 矩形面积 II | Rectangle Area II

    We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, ...

  2. [LeetCode] 850. Rectangle Area II 矩形面积之二

    We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, ...

  3. [LeetCode] Rectangle Area 矩形面积

    Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...

  4. 2019计蒜之道初赛4 B. 腾讯益智小游戏—矩形面积交(简单)(矩形交集)

    B. 腾讯益智小游戏—矩形面积交(简单) 1000ms 262144K   腾讯游戏开发了一款全新的编程类益智小游戏,最新推出的一个小游戏题目是关于矩形面积交的.聪明的你能解出来吗?看下面的题目接招吧 ...

  5. [LeetCode] 223. Rectangle Area 矩形面积

    Find the total area covered by two rectilinearrectangles in a 2D plane. Each rectangle is defined by ...

  6. POJ 1151 Atlantis(线段树-扫描线,矩形面积并)

    题目链接:http://poj.org/problem?id=1151 题目大意:坐标轴上给你n个矩形, 问这n个矩形覆盖的面积 题目思路:矩形面积并. 代码如下: #include<stdio ...

  7. 25.按要求编写一个Java应用程序: (1)编写一个矩形类Rect,包含: 两个属性:矩形的宽width;矩形的高height。 两个构造方法: 1.一个带有两个参数的构造方法,用于将width和height属性初化; 2.一个不带参数的构造方法,将矩形初始化为宽和高都为10。 两个方法: 求矩形面积的方法area() 求矩形周长的方法perimeter() (2)通过继承Rect类编写一个具有

    package zhongqiuzuoye; //自己写的方法 public class Rect { public double width; public double height; Rect( ...

  8. 扫描线 + 线段树 : 求矩形面积的并 ---- hnu : 12884 Area Coverage

    Area Coverage Time Limit: 10000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit user ...

  9. 【HDU 1542】Atlantis(线段树+离散化,矩形面积并)

    求矩形面积并,离散化加线段树. 扫描线法: 用平行x轴的直线扫,每次ans+=(下一个高度-当前高度)*当前覆盖的宽度. #include<algorithm> #include<c ...

随机推荐

  1. getField()与getDeclaredField()的区别

    Java的反射机制中,用Class的getField(String name)或getDelaredField(String name)可以得到目标类的指定属性,返回类型是Field. 但这两个是有区 ...

  2. AutoResetEvent 方法名称设计缺陷

    这个类和方法,让人乍一读是读不明白的.不能通过方法名称明白其含义.所以它的方法名称设计是欠考虑. 应该类似于这样: public static class MyAutoResetEvent { pub ...

  3. nginx模块学习

    rewrite模块的语法 链接

  4. 面向对象OO第15次作业总结

    面向对象OO第15次作业总结 1.论述测试与正确性论证的效果差异,比较其优缺点测试通过大量测试数据来覆盖测试代码,比较直观,优点在于知道测的是啥,特别直观,缺点在于很难覆盖所有情况.正确性论证从逻辑关 ...

  5. 实验1 熟悉Linux开发环境 实验报告

    参见http://www.cnblogs.com/lhc-java/p/4970269.html

  6. Hibernate 与 mybatis 区别

    JAVA面试中问及HIBERNATE与 MYBATIS的对比,在这里做一下总结   我是一名java开发人员,hibernate以及mybatis都有过学习,在java面试中也被提及问道过,在项目实践 ...

  7. 【图论】POJ-3255 次短路径

    一.题目 Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her ...

  8. 《软件工程和Python》第0周作业1

    写在前面的话 欢迎大家开始一段新的课程学习!从开博客开始吧.每次博客作业都会有评分,计入总成绩哦. 1.   截止日期 本次作业的提交截止时间:见老师要求 2.   作业要求 (1)建立个人技术博客和 ...

  9. 404 Note Found 团队会议纪要

    目录 团队会议 会议纪要1 会议纪要2 会议纪要3 会议纪要4 会议纪要5 会议纪要6 团队会议 会议纪要1 会议纪要2 会议纪要3 会议纪要4 会议纪要5 会议纪要6

  10. oracle 简单的sysTimeStamp类型转date 类型

    oracle  简单的SYSTIMESTAMP 类型转date 类型 SELECT SYSTIMESTAMP , SYSTIMESTAMP+0 FROM dual; SAMPLE_TIME ----- ...