Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并
Vika has an infinite sheet of squared paper. Initially all squares are white. She introduced a two-dimensional coordinate system on this sheet and drew n black horizontal and vertical segments parallel to the coordinate axes. All segments have width equal to 1 square, that means every segment occupy some set of neighbouring squares situated in one row or one column.
Your task is to calculate the number of painted cells. If a cell was painted more than once, it should be calculated exactly once.
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of segments drawn by Vika.
Each of the next n lines contains four integers x1, y1, x2 and y2 ( - 109 ≤ x1, y1, x2, y2 ≤ 109) — the coordinates of the endpoints of the segments drawn by Vika. It is guaranteed that all the segments are parallel to coordinate axes. Segments may touch, overlap and even completely coincide.
Print the number of cells painted by Vika. If a cell was painted more than once, it should be calculated exactly once in the answer.
3
0 1 2 1
1 4 1 2
0 3 2 3
8
In the first sample Vika will paint squares (0, 1), (1, 1), (2, 1), (1, 2), (1, 3), (1, 4), (0, 3) and (2, 3).
题意:
给你n天平行x,y轴的线段
问你遍历的点有多少个
题解:
将线段 扩展成一个长度为x * 1 的矩阵
做一遍线段树扫描线求矩阵面积并
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000") const int N = 5e5+, M = 5e5+, inf = 2e9, mod = 1e9+;
const double Pi = acos(-1.0);
typedef long long LL;
#define ls k<<1
#define rs ls | 1 int san[N], sum[N], vis[N], n, cnt = ;
struct edge{
int l,r,x,in;
edge(int l = , int r = , int x = , int in = ) : l(l), r(r), x(x), in(in) {}
bool operator < (const edge &b) const {
return x < b.x || x == b.x && in > b.in;
}
}e[N];
int Hash(int x) {return lower_bound(san+,san+cnt+,x) - san;}
void push_up(int k,int ll,int rr) {
if(vis[k]) sum[k] = san[rr + ] - san[ll];
else if(ll == rr) sum[k] = ;
else sum[k] = sum[ls] + sum[rs];
}
void update(int l,int r,int c,int ll,int rr,int k) {
if(ll == l && rr == r) {
vis[k] += c;
push_up(k,ll,rr);
return ;
}
int mid = (ll + rr) >> ;
if(r <= mid) update(l,r,c,ll,mid,ls);
else if(l > mid) update(l,r,c,mid+,rr,rs);
else update(l,mid,c,ll,mid,ls), update(mid+,r,c,mid+,rr,rs);
push_up(k,ll,rr);
}
int main() {
scanf("%d",&n);
for(int i = ; i <= n; ++i) {
int x,y,xx,yy;
scanf("%d%d%d%d",&x,&y,&xx,&yy);
if(x > xx) swap(x,xx);
if(y > yy) swap(y,yy);
xx++, yy++;
san[++cnt] = y;
san[++cnt] = yy;
e[i] = edge(y,yy,x,);
e[i+n] = edge(y,yy,xx,-);
} sort(san+,san+cnt+);
cnt = unique(san + , san + cnt + ) - san - ; int m = n << ;
sort(e+,e+m+); LL ans = ;
for(int i = ; i <= m; ++i) {
int l = Hash(e[i].l);
int r = Hash(e[i].r) - ;
if(l <= r) update(l,r,e[i].in,,m,);
ans += 1LL * sum[] * (e[i+].x - e[i].x);
}
cout<<ans<<endl;
}
Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并的更多相关文章
- Codeforces Round #337 (Div. 2) D. Vika and Segments (线段树+扫描线+离散化)
题目链接:http://codeforces.com/contest/610/problem/D 就是给你宽度为1的n个线段,然你求总共有多少单位的长度. 相当于用线段树求面积并,只不过宽为1,注意y ...
- Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树扫描线
D. Vika and Segments 题目连接: http://www.codeforces.com/contest/610/problem/D Description Vika has an i ...
- Codeforces Round #292 (Div. 1) C. Drazil and Park 线段树
C. Drazil and Park 题目连接: http://codeforces.com/contest/516/problem/C Description Drazil is a monkey. ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树
题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...
- Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash
E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)
题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所 ...
- Codeforces Round #312 (Div. 2) E. A Simple Task 线段树
E. A Simple Task 题目连接: http://www.codeforces.com/contest/558/problem/E Description This task is very ...
随机推荐
- POJ 2769
http://poj.org/problem?id=2796 题意:求n个数的和乘以这n个数中的最小值的积最大的数,以及其范围. 思路:求每一个数两边的比其大的数的和,再乘以这个数.还有一个范围,用单 ...
- c++数据类型和定义
我们都知道,刚开始学习数学的时候.乘法口诀.99乘法口诀.这个是大家都需要背的.背熟了这个,大家才能知道遇到算术题如何计算.这个99乘法口诀就是一种定义. 同样任何的语言都会有很多的定义.比如语文:各 ...
- 服务器×××上的MSDTC不可用解决办法
MSDTC(分布式交易协调器),协调跨多个数据库.消息队列.文件系统等资源管理器的事务.该服务的进程名为Msdtc.exe,该进程调用系统Microsoft Personal Web Server和M ...
- VC++ LoadLibrary失败,错误126(找不到指定的模块)
在VS中调用一个资源模块dll,LoadLibrary返回值为NULL,没有加载成功.GetLastError后原因为"找不到指定的模块"!代码如下: HINSTANCE hIns ...
- asp.net mvc 部分视图加载区别
ASP.NET MVC 部分视图 ASP.NET(11) 版权声明:本文为博主原创文章,未经博主允许不得转载. [部分视图] ASP.NET MVC 里的部分视图,相当于 Web Form 里的 ...
- 【linux】学习4
文件压缩: gzip :压缩 解压缩 zcat: 读取压缩文件 gzip text1 :压缩text1 得到 text1.gz 原文件不见了 gzip -c text1 > text1.g ...
- 【python】dict4ini和xmltodict模块用途
dict4ini模块:可以读写配置文件 xmltodict模块:将xml和json互相转换 https://pypi.python.org/pypi/xmltodict
- 更新补丁Bind
1.查询补丁版本信息 (1) rpm -qa|grep bind (2) dig @localhost version.bind 2.下载安装 BIND最新漏洞和升级解决办法 现在有非常多的公司的都有 ...
- iOS开发MAC下配置Svn和Git
如果你对iOS开发中的版本控制还不了解那么你可以先看看这篇(大致看一遍就ok) http://www.cnblogs.com/iCocos/p/4767692.html 关于版本控制使用起来并不难 ...
- 解决Eclipse里的Maven工程pom.xml文件报:web.xml is missing and <failOnMissingWebXml> is set to true错误
打开eclipse准备进行开发时,发现项目上有个红星号,查看错误后发现报了一个:"web.xml is missing and <failOnMissingWebXml> is ...