E - 秋实大哥与家

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://acm.uestc.edu.cn/#/contest/show/59

Description

秋实大哥是一个顾家的男人,他认为人生就是旅途,不管我们漂到哪,最终还是会回到温暖的地方——家。

所以他买了很多家具。

秋实大哥的家可以看成一个W×H的矩阵,每一件家具可以看成一个矩形,他们放置在秋实大哥的家里,相互之间没有重叠。

现在秋实大哥购置了一个新的大小为1×M的家具,秋实大哥想知道他有多少种方式来安放这个家具。

Input

第一行包含四个整数W,H,n,M,表示秋实大哥家的大小为W×H,现在已有n个家具,新购置的家具大小为1×M。

接下来n行,每行包含4个整数x1,y1,x2,y2,分别表示这个家具左下角的坐标和右上角的坐标。

1≤n,H,W,M≤100000,1≤x1≤x2≤W,1≤y1≤y2≤H。

Output

输出一行包含一个整数,表示有多少种方案可以来放置秋实大哥的新家具,要求不能跟原有的家具重叠,也不能超出家的范围。
 

Sample Input

3 3 1 2
2 2 2 2

Sample Output

8

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题 秋实大哥与家 线段树扫描线求矩形面积交的更多相关文章

  1. 2015 UESTC 数据结构专题B题 秋实大哥与花 线段树 区间加,区间查询和

    B - 秋实大哥与花 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 De ...

  2. 2015 UESTC 数据结构专题A题 秋实大哥与小朋友 线段树 区间更新,单点查询,离散化

    秋实大哥与小朋友 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Desc ...

  3. 2015 UESTC 数据结构专题C题 秋实大哥与快餐店 字典树

    C - 秋实大哥与快餐店 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 ...

  4. 2015 UESTC 数据结构专题G题 秋实大哥去打工 单调栈

    秋实大哥去打工 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Descr ...

  5. 2015 UESTC 数据结构专题N题 秋实大哥搞算数 表达式求值/栈

    秋实大哥搞算数 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1074 Des ...

  6. 2015 UESTC 数据结构专题H题 秋实大哥打游戏 带权并查集

    秋实大哥打游戏 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 Descr ...

  7. 2015 UESTC 数据结构专题D题 秋实大哥与战争 SET的妙用

    D - 秋实大哥与战争 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 D ...

  8. 2015 UESTC 数据结构专题D题 秋实大哥与战争 变化版本的线段树,合并区间,单点查询

    D - 秋实大哥与战争 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 D ...

  9. 2015 UESTC 搜索专题K题 秋实大哥の恋爱物语 kmp

    秋实大哥の恋爱物语 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 De ...

随机推荐

  1. java类中访问属性

    package first; public class for_protect { private int age=10; int number = 100; public void show(){ ...

  2. 64_t6

    texlive-recipebook-svn37026.0-33.fc26.2.noarch.rpm 24-May-2017 15:44 37946 texlive-recipecard-doc-sv ...

  3. 运维小知识之nginx---nginx配置Jboss集群负载均衡

      codyl 2016-01-26 00:53:00 浏览385 评论0 负载均衡 转自 运维小知识之nginx---nginx配置Jboss集群负载均衡-博客-云栖社区-阿里云https://yq ...

  4. /proc/cpuinfo 文件分析(查看CPU信息)

    /proc/cpuinfo文件分析 根据以下内容,我们则可以很方便的知道当前系统关于CPU.CPU的核数.CPU是否启用超线程等信息. <1>查询系统具有多少个逻辑核:cat /proc/ ...

  5. WebService初识

    Web service 是一种跨编程语言和跨操作系统平台的远程调用技术,即跨平台远程调用技术.也就是说,不管是J2EE架构,还是.net架构 只要按照规范就可以进行通信,实现数据交互等. 这里说的&q ...

  6. POJ 2230 Watchcow(欧拉回路:输出点路径)

    题目链接:http://poj.org/problem?id=2230 题目大意:给你n个点m条边,Bessie希望能走过每条边两次,且两次的方向相反,让你输出以点的形式输出路径. 解题思路:其实就是 ...

  7. POJ 1511 Invitation Cards(Dijkstra(优先队列)+SPFA(邻接表优化))

    题目链接:http://poj.org/problem?id=1511 题目大意:给你n个点,m条边(1<=n<=m<=1e6),每条边长度不超过1e9.问你从起点到各个点以及从各个 ...

  8. 三十分钟理解计算图上的微积分:Backpropagation,反向微分

    神经网络的训练算法,目前基本上是以Backpropagation (BP) 反向传播为主(加上一些变化),NN的训练是在1986年被提出,但实际上,BP 已经在不同领域中被重复发明了数十次了(参见 G ...

  9. JavaScript 兼容性总结

     请实现鼠标点击任意标签,alert该标签的名称(注意兼容性) function elementName(evt){ evt = evt|| window.event; var selected = ...

  10. IEEEXtreme 10.0 - Flower Games

    这是 meelo 原创的 IEEEXtreme极限编程比赛题解 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.com/contests/ieeextreme-c ...