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. nginx 配置代理某个路径

    location /test{ proxy_pass http://localhost:8765/test; proxy_set_header Host $http_host; } 其中红色的那句可以 ...

  2. 配置kernel的log buf大小(如果kmsg log被覆盖)

    如果在打印kmsg log时发现log被覆盖,log 的buf不够大可以使用默认配置调buf: defconfig CONFIG_LOG_BUF_SHIFT=20  (默认是17  2的17次方)   ...

  3. WebClient vs HttpClient vs HttpWebRequest

    转载:http://www.diogonunes.com/blog/webclient-vs-httpclient-vs-httpwebrequest/ Just when I was startin ...

  4. 浅谈Linux系统中如何查看进程 ——ps,pstree,top,w,全解

    进程是一个其中运行着一个或多个线程的地址空间和这些线程所需要的系统资源.一般来说,Linux系统会在进程之间共享程序代码和系统函数库,所以在任何时刻内存中都只有代码的一份拷贝. 1,ps命令 作用:p ...

  5. Django 注册

    一. 本地图片上传预览 1. 上传文件框隐藏到图片上面,点击图片相当于点上传文件框 <div class="login"> <div style="po ...

  6. booklist for machine learning

    Recommended Books Here is a list of books which I have read and feel it is worth recommending to fri ...

  7. WP SMTP插件为啥我一直设置的不对?

    我也是摸索好久才搞定的,如果你是万网空间先去修改一下参数在万网后台设置PHP.ini参数设置,因为万网阿里云免费虚拟主机禁用了WordPress默认使用的PHP mail()发信函数,而 stream ...

  8. codeforce 1A Theatre Square

    A. Theatre Square Theatre Square in the capital city of Berland has a rectangular shape with the siz ...

  9. LeetCode 136. Single Number(只出现一次的数字)

    LeetCode 136. Single Number(只出现一次的数字)

  10. 使用log4j输出日志

    今天在倒腾log4j,其实蛮简单的. 一.首先是下载log4j的jar包. 二.将下载的jar包添加到web工程的WEB-INF/lib包下 三.在src/config包下新建log4j.proper ...