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 ;
}

最新文章

  1. Windows Store App Image开发示例
  2. tornado web高级开发项目之抽屉官网的页面登陆验证、form验证、点赞、评论、文章分页处理、发送邮箱验证码、登陆验证码、注册、发布文章、上传图片
  3. rails的字符编码
  4. LightOj 1065 - Number Sequence (矩阵快速幂,简单)
  5. dsoframer控件在64系统上使用问题小汇总
  6. 基于nginx+lua简单的灰度发布系统
  7. CLR Via C#: 类型基础
  8. DVWA 黑客攻防实战(十五) 绕过内容安全策略 Content Security Policy (CSP) Bypass
  9. 使用docker试用各种软件及docker-ES设置
  10. Android Studio之SVN打分支、切换分支及合并分支
  11. spark-sql(spark sql cli)客户端集成hive
  12. MidoNet 安装(Kilo RDO)(最老版)
  13. Python 全集变量
  14. 3-安装hive
  15. 基于jQuery功能非常强大的图片裁剪插件
  16. 在 Linux 中使用超级用户权限
  17. centos 6.5安装docker
  18. selenium+python : Waits---study
  19. 《LeetBook》leetcode题解(5):Longest Palindromic [M]——回文串判断
  20. Python代码审计中一些需要重点关注的项

热门文章

  1. 学习Python函数笔记之二(内置函数)
  2. Python异常捕捉try except else finally有return时执行顺序探究
  3. Linux命令学习手册-route命令
  4. 011 CountDownLatch,CyclicBarrier和Semaphore
  5. html---规范、细节积累-01
  6. 版本控制软件——tortoiseSVN的基础使用
  7. csu 1749: Soldiers &#39; Training(贪心)
  8. linux下设置opencv环境变量
  9. wondows下安装pytho&amp;pip
  10. [loj6039]「雅礼集训 2017 Day5」珠宝 dp+决策单调性+分治