FZU2187 回家种地(矩形面积并)
矩形面积并(只覆盖一次的面积)的裸题。好久没写代码debug了我太久,太辛酸了。
#pragma warning(disable:4996)
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std; #define ll long long
#define maxn 200005
#define y1 y111 int lf[maxn << 2], rf[maxn << 2];
int sum[maxn << 2];
int a[maxn];
int add[maxn << 2];
int mi[maxn << 2];
int ma[maxn << 2]; int n, nSize; void pushUp(int i)
{
mi[i] = min(mi[i << 1], mi[i << 1 | 1]);
ma[i] = max(ma[i << 1], ma[i << 1 | 1]);
} void pushDown(int i)
{
if (add[i] != 0){
if (lf[i] != rf[i]){
add[i << 1] += add[i];
add[i << 1 | 1] += add[i];
mi[i << 1] += add[i];
ma[i << 1] += add[i];
mi[i << 1 | 1] += add[i];
ma[i << 1 | 1] += add[i];
add[i] = 0;
}
}
} void build(int i, int L, int R)
{
lf[i] = L; rf[i] = R; add[i] = mi[i] = ma[i] = 0;
if (L == R){
sum[i] = a[L];
return;
}
int M = (L + R) >> 1;
build(i << 1, L, M);
build(i << 1 | 1, M + 1, R);
sum[i] = sum[i << 1] + sum[i << 1 | 1];
} void upd(int i, int L, int R,int v)
{
if (L == lf[i] && R == rf[i]){
add[i] += v;
mi[i] += v;
ma[i] += v;
return;
}
pushDown(i);
int M = (lf[i] + rf[i]) >> 1;
if (R <= M){
upd(i << 1, L, R,v);
}
else if (L > M){
upd(i << 1 | 1, L, R, v);
}
else{
upd(i << 1, L, M, v);
upd(i << 1 | 1, M + 1, R, v);
}
pushUp(i);
} ll query(int i)
{
if (ma[i] <= 0) return 0;
if (mi[i] > 1) return 0;
if (ma[i] == mi[i] && ma[i] == 1){
return sum[i];
}
pushDown(i);
return query(i << 1) + query(i << 1 | 1);
} struct Node
{
ll x;
ll bg, ed;
int v;
Node(ll xi, ll bgi, ll edi,int vi) :x(xi), bg(bgi), ed(edi),v(vi){}
bool operator < (const Node &b)const{
return x == b.x ? v>b.v : x < b.x;
}
};
vector<Node> vec;
vector<ll> dis; int main()
{
int T; cin >> T; int ca = 0;
while (T--)
{
scanf("%d", &n);
ll x1, x2, y1, y2;
vec.clear();
vec.reserve(2 * n + 100);
dis.clear();
for (int i = 0; i < n; ++i){
scanf("%I64d%I64d%I64d%I64d", &x1, &y1, &x2, &y2);
vec.push_back(Node(x1, y1, y2,1));
vec.push_back(Node(x2, y1, y2,-1));
dis.push_back(y1);
dis.push_back(y2);
}
sort(dis.begin(), dis.end());
nSize = unique(dis.begin(), dis.end()) - dis.begin();
for (int i = 1; i < nSize; ++i){
a[i] = dis[i] - dis[i - 1];
}
for (int i = 0; i < vec.size(); ++i){
int lid = lower_bound(dis.begin(), dis.begin()+nSize, vec[i].bg) - dis.begin();
int rid = lower_bound(dis.begin(), dis.begin()+nSize, vec[i].ed) - dis.begin();
vec[i].bg = lid + 1;
vec[i].ed = rid;
}
sort(vec.begin(), vec.end());
build(1, 1, nSize - 1); ll ans = 0;
ll preLen = 0;
ll prex = vec[0].x;
for (int i = 0; i < vec.size(); ++i){
ll val = vec[i].x;
while (i<vec.size()&&vec[i].x == val){
upd(1, vec[i].bg, vec[i].ed, vec[i].v);
++i;
}
--i;
ans += preLen*(vec[i].x - prex);
prex = vec[i].x;
preLen = query(1);
}
printf("Case %d: %I64d\n", ++ca, ans);
}
return 0;
}
FZU2187 回家种地(矩形面积并)的更多相关文章
- FZU 2187 回家种地 ( 扫描线 + 离散 求矩阵单次覆盖面积 )
2187 回家种地 Accept: 56 Submit: 230Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Descript ...
- [LeetCode] Rectangle Area 矩形面积
Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...
- POJ 1151 Atlantis(线段树-扫描线,矩形面积并)
题目链接:http://poj.org/problem?id=1151 题目大意:坐标轴上给你n个矩形, 问这n个矩形覆盖的面积 题目思路:矩形面积并. 代码如下: #include<stdio ...
- 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( ...
- 扫描线 + 线段树 : 求矩形面积的并 ---- hnu : 12884 Area Coverage
Area Coverage Time Limit: 10000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit user ...
- 【HDU 1542】Atlantis(线段树+离散化,矩形面积并)
求矩形面积并,离散化加线段树. 扫描线法: 用平行x轴的直线扫,每次ans+=(下一个高度-当前高度)*当前覆盖的宽度. #include<algorithm> #include<c ...
- 2015baidu复赛 矩形面积(包凸 && ps:附quickhull模板)
矩形面积 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 2015年百度之星初赛(1) --- F 矩形面积
矩形面积 Problem Description 小度熊有一个桌面,小度熊剪了很多矩形放在桌面上,小度熊想知道能把这些矩形包围起来的面积最小的矩形的面积是多少. Input 第一行一个正整数 T, ...
- POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并
题意:给出矩形两对角点坐标,求矩形面积并. 解法:线段树+离散化. 每加入一个矩形,将两个y值加入yy数组以待离散化,将左边界cover值置为1,右边界置为2,离散后建立的线段树其实是以y值建的树,线 ...
随机推荐
- php中关于empty()函数是否为真的判断
<?php// $a = 0; ==> 符合empty,empty($a)为true// $a = '0'; ==> 符合empty,empty($a)为true// $a = ...
- SourceTree 跳过登录注册,直接使用
SourceTree下载安装后,运行程序会要求你登录或注册账号才能使用, 然而登录或注册基本都收不到服务器的响应 (在国外嘛,安全起见),于是卡在此处无法使用了. 下面就来介绍一下跳过这尴尬环节的方法 ...
- JZOJ 100029. 【NOIP2017提高A组模拟7.8】陪审团
100029. [NOIP2017提高A组模拟7.8]陪审团 Time Limits: 1000 ms Memory Limits: 131072 KB Detailed Limits Got ...
- python面试题之介绍一下Python中webbrowser的用法
所属网站分类: 面试经典 > python 作者:外星人入侵 链接: http://www.pythonheidong.com/blog/article/13/ 来源:python黑洞网 www ...
- Django配置邮箱登录
1.settings下配置 # AUTH 方法(支持邮箱登录) AUTHENTICATION_BACKENDS = ('users.views.CustomBackend',) 2.views下逻辑如 ...
- c++ vector实例
#include <iostream> #include <string> #include <vector> #include <iostream> ...
- Django 四——ModelForm用法
内容概要: 1.新增数据库表中数据 2.更新数据库表中数据 Django的ModelForm Django中内置了Form和Model两个类,有时候页面的表单form类与Model类是一一对应,因此分 ...
- loj2173 「FJOI2016」建筑师
ref 真是道组合数学神题啊--第一次见第一类斯特林数-- #include <iostream> #include <cstdio> using namespace std; ...
- csu-2018年11月月赛Round2-div2题解
csu-2018年11月月赛Round2-div2题解 A(2193):昆虫繁殖 Description 科学家在热带森林中发现了一种特殊的昆虫,这种昆虫的繁殖能力很强.每对成虫过x个月产y对卵,每对 ...
- Visual C++ 经典的人脸识别算法源代码
说明:VC++ 经典的人脸识别算法实例,提供人脸五官定位具体算法及两种实现流程. 点击下载