2015 UESTC 数据结构专题E题 秋实大哥与家 线段树扫描线求矩形面积交
E - 秋实大哥与家
Time Limit: 1 Sec Memory Limit: 256 MB
题目连接
http://acm.uestc.edu.cn/#/contest/show/59
Description
所以他买了很多家具。
秋实大哥的家可以看成一个W×H的矩阵,每一件家具可以看成一个矩形,他们放置在秋实大哥的家里,相互之间没有重叠。
现在秋实大哥购置了一个新的大小为1×M的家具,秋实大哥想知道他有多少种方式来安放这个家具。
Input
接下来n行,每行包含4个整数x1,y1,x2,y2,分别表示这个家具左下角的坐标和右上角的坐标。
1≤n,H,W,M≤100000,1≤x1≤x2≤W,1≤y1≤y2≤H。
Output
Sample Input
2 2 2 2
Sample Output
HINT
题意
题解:
对于每一个矩形,我们都向左边延展m-1厘米,包括边界,然后这道题就可以转化为求剩下的面积是多少啦
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
#define LL(x) (x<<1)
#define RR(x) (x<<1|1)
typedef long long LL;
const int N=;
struct Line
{
int x,y1,y2,valu;
Line(){}
Line(int a,int b,int c,int d){x=a;y1=b;y2=c;valu=d;}
bool operator<(const Line &b)const
{
return x<b.x;
}
};
struct node
{
int lft,rht,sum,valu;
int mid(){return lft+(rht-lft)/;}
}; map<int,int> imap;
vector<int> y;
vector<Line> line;
int data[N][]; struct Segtree
{
node tree[N*];
void relax(int ind)
{
if(tree[ind].valu>)
tree[ind].sum=y[tree[ind].rht]-y[tree[ind].lft];
else
{
if(tree[ind].lft+==tree[ind].rht) tree[ind].sum=;
else tree[ind].sum=tree[LL(ind)].sum+tree[RR(ind)].sum;
}
}
void build(int lft,int rht,int ind)
{
tree[ind].lft=lft; tree[ind].rht=rht;
tree[ind].sum=; tree[ind].valu=;
if(lft+!=rht)
{
int mid=tree[ind].mid();
build(lft,mid,LL(ind));
build(mid,rht,RR(ind));
}
}
void updata(int be,int end,int ind,int valu)
{
int lft=tree[ind].lft,rht=tree[ind].rht;
if(be<=lft&&rht<=end)
{
tree[ind].valu+=valu;
relax(ind);
}
else
{
int mid=tree[ind].mid();
if(be<mid) updata(be,end,LL(ind),valu);
if(end>mid) updata(be,end,RR(ind),valu);
relax(ind);
}
}
}seg; LL solve(int n,int w,int h,int m)
{
y.clear(); line.clear(); imap.clear();
y.push_back(); y.push_back(h);
line.push_back(Line(max(,w-m),,h,));
line.push_back(Line(w,,h,-));
for(int i=;i<n;i++)
{
int x1=max(,data[i][]-m),y1=data[i][];
int x2=data[i][],y2=data[i][];
line.push_back(Line(x1,y1,y2,));
line.push_back(Line(x2,y1,y2,-));
y.push_back(y1);y.push_back(y2);
}
sort(y.begin(),y.end());
y.erase(unique(y.begin(),y.end()),y.end());
for(int i=;i<(int)y.size();i++)
imap.insert(make_pair(y[i],i));
sort(line.begin(),line.end()); LL res=;
seg.build(,(int)y.size(),);
for(int i=;i<(int)line.size();i++)
{
if(i!=) res+=(LL)seg.tree[].sum*(line[i].x-line[i-].x);
seg.updata(imap[line[i].y1],imap[line[i].y2],,line[i].valu);
}
return res;
}
int main()
{
//线段树 矩形面积
int w,h,n,m;
while(scanf("%d%d%d%d",&w,&h,&n,&m)!=EOF)
{
for(int i=;i<n;i++)
for(int j=;j<;j++)
{
scanf("%d",&data[i][j]);
if(j==||j==) data[i][j]++;
}
LL res1=(LL)w*h-solve(n,w+,h+,m-);
for(int i=;i<n;i++)
{
swap(data[i][],data[i][]);
swap(data[i][],data[i][]);
}
LL res2=(LL)w*h-solve(n,h+,w+,m-);
if(m!=) printf("%lld\n",res1+res2);
else printf("%lld\n",res1);
}
return ;
}
2015 UESTC 数据结构专题E题 秋实大哥与家 线段树扫描线求矩形面积交的更多相关文章
- 2015 UESTC 数据结构专题B题 秋实大哥与花 线段树 区间加,区间查询和
B - 秋实大哥与花 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 De ...
- 2015 UESTC 数据结构专题A题 秋实大哥与小朋友 线段树 区间更新,单点查询,离散化
秋实大哥与小朋友 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Desc ...
- 2015 UESTC 数据结构专题C题 秋实大哥与快餐店 字典树
C - 秋实大哥与快餐店 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 ...
- 2015 UESTC 数据结构专题G题 秋实大哥去打工 单调栈
秋实大哥去打工 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Descr ...
- 2015 UESTC 数据结构专题N题 秋实大哥搞算数 表达式求值/栈
秋实大哥搞算数 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1074 Des ...
- 2015 UESTC 数据结构专题H题 秋实大哥打游戏 带权并查集
秋实大哥打游戏 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Descr ...
- 2015 UESTC 数据结构专题D题 秋实大哥与战争 SET的妙用
D - 秋实大哥与战争 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 D ...
- 2015 UESTC 数据结构专题D题 秋实大哥与战争 变化版本的线段树,合并区间,单点查询
D - 秋实大哥与战争 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 D ...
- 2015 UESTC 搜索专题K题 秋实大哥の恋爱物语 kmp
秋实大哥の恋爱物语 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 De ...
随机推荐
- Vim的分屏功能
本篇文章主要教你如何使用 Vim 分屏功能. 分屏启动Vim 使用大写的O参数来垂直分屏. vim -On file1 file2 ... 使用小写的o参数来水平分屏. vim -on file1 f ...
- 牛奶ddw如何通过以太坊钱包实现互相打赏
很多朋友不清楚如何转账ddw,但是万能的网友是无敌的,这两天就自己摸索的一点经验总结下今天的转账经验. 1. 提取到自己的账户 这个大家都知道如何操作,使用官方的钱包 在“日日盈app”中点击&quo ...
- u-boot引导内核过程
目标板:2440 u-boot引导内核启动时,传入内核的参数为bootcmd=nand read.jffs2 0x30007FC0 kernel; bootm 0x30007FC0 一.nand re ...
- web.xml中的dispatchservlet后,js,css,甚至gif都不能正常显示
这个可以说是很多初学Springmvc的人都会碰到一个令人头痛的问题 那就是为什么我配置好web.xml中的dispatchservlet后,js,css,甚至gif都不能正常显示了 我们来看看我们配 ...
- 1、量化投资—为什么选择Python?
Python在量化领域的现状 就跟Java在web领域无可撼动的地位一样,Python也已经在金融量化投资领域占据了重要位置,从各个业务链条都能找到相应的框架实现. 在量化投资(证券和比特币)开源项目 ...
- mysql的两种存储引擎
MySQL 有多种存储引擎,目前常用的是 MyISAM 和 InnoDB 这两个引擎,除了这两个引擎以为还有许多其他引擎,有官方的,也有一些公司自己研发的.这篇文章主要简单概述一下常用常见的 MySQ ...
- ISSCC 2017论文导读 Session 14:A 288μW Programmable Deep-Learning Processor with 270KB On-Chip Weight
A 288μW Programmable Deep-Learning Processor with 270KB On-Chip Weight Storage Using Non-Uniform Mem ...
- AndroidStudio升到最新版本(3.1.2)之后
暂时发现的需要大家注意的地方 1.androidstudio3无法导入moudle? 例如:我写了一个简单的项目,需要导入一个第三方的moudle,我导入: 因为AS升级之后,没有突出颜色的变化(变黑 ...
- Docker Zero Deployment and Secrets (一)
在本节中,主要介绍在Docker swarm中如何不中断应用高可靠性的情况下更新服务和stack.这也叫做zero downtime deployment.还有就是swam如何管理密钥,保证容器之间的 ...
- poj2253 Frogger(Floyd)
题目链接 http://poj.org/problem?id=2253 题意 给出青蛙A,B和若干石头的坐标,现在青蛙A要跳到青蛙B所在的石头上,求出所有路径中最远那一跳的最小值. 思路 Floyd算 ...