UVA1492 - Adding New Machine(扫描线)

option=com_onlinejudge&Itemid=8&page=show_problem&category=523&problem=4238&mosmsg=Submission%20received%20with%20ID%2014379274" target="_blank" style="">题目链接

题目大意:给你N∗M个格子,这些格子中某些格子是放了旧的机器。然后问如今要在这些格子放一台1∗M的新机器,问有多少种放法。

解题思路:这题照样是能够转换成面积并来做,对于有旧机器(x。y)的格子,那么(x - M + 1,y)都是不能够放新机器的格子,还有从(H - M + 2,H)都是不能够放新机器的格子,所以覆盖的范围就要扩大。

用扫描线算出这些不能够放新机器的格子,然后用总共的格子数剪掉就得到答案。分横着放和竖着放两种情况。

注意M = 1的时候要特判。由于不存在横着和竖着两种情况。

代码:

#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = 5e4 + 5;
typedef long long ll;
#define lson(x) (x<<1)
#define rson(x) ((x<<1) | 1) int x[2][maxn], y[2][maxn]; struct Node { int l, r, add, s;
void set (int l, int r, int add, int s) { this->l = l;
this->r = r;
this->add = add;
this->s = s;
}
}node[8 * maxn]; struct Line { int x, y1, y2, flag;
Line (int x, int y1, int y2, int flag) { this->x = x;
this->y1 = y1;
this->y2 = y2;
this->flag = flag;
} bool operator < (const Line& a) const {
return x < a.x;
}
}; vector<int> pos;
vector<Line> L;
int W, H, N, M; void pushup (int u) { if (node[u].add)
node[u].s = pos[node[u].r + 1] - pos[node[u].l];
else if (node[u].l == node[u].r)
node[u].s = 0;
else
node[u].s = node[lson(u)].s + node[rson(u)].s;
} void build (int u, int l, int r) { node[u].set (l, r, 0, 0);
if (l == r)
return; int m = (l + r)>>1;
build (lson(u), l, m);
build (rson(u), m + 1, r);
pushup(u);
} void update (int u, int l, int r, int v) { if (node[u].l >= l && node[u].r <= r) { node[u].add += v;
pushup(u);
return ;
} int m = (node[u].l + node[u].r)>>1;
if (l <= m)
update (lson(u), l, r, v);
if (r > m)
update (rson(u), l, r, v);
pushup(u);
} void init () { for (int i = 0; i < N; i++)
scanf ("%d%d%d%d", &x[0][i], &y[0][i], &x[1][i], &y[1][i]);
} ll solve (int w, int h, int x[2][maxn], int y[2][maxn]) { L.clear();
pos.clear();
int tmp; for (int i = 0; i < N; i++) { tmp = max(y[0][i] - M + 1, 1);
L.push_back(Line(x[0][i], tmp, y[1][i] + 1, 1));
L.push_back(Line(x[1][i] + 1, tmp, y[1][i] + 1, -1));
pos.push_back(tmp);
pos.push_back(y[1][i] + 1);
} tmp = max(1, h - M + 2);
L.push_back(Line(1, tmp, h + 1, 1));
L.push_back(Line(w + 1, tmp, h + 1, -1));
pos.push_back(tmp);
pos.push_back(h + 1); sort (L.begin(), L.end());
sort (pos.begin(), pos.end());
pos.erase (unique(pos.begin(), pos.end()), pos.end()); build(1, 0, (int)pos.size() - 1); ll ans = 0;
int l, r;
for (int i = 0; i < L.size() - 1; i++) { l = lower_bound(pos.begin(), pos.end(), L[i].y1) - pos.begin();
r = lower_bound(pos.begin(), pos.end(), L[i].y2) - pos.begin();
update(1, l, r - 1, L[i].flag);
// printf ("%d %d\n", node[1].s, L[i + 1].x - L[i].x);
ans += (ll)node[1].s * (L[i + 1].x - L[i].x);
} return ans;
} int main () { ll ans;
while (scanf ("%d%d%d%d", &W, &H, &N, &M) != EOF) { init(); if (M == 1) {
ans = 0;
for (int i = 0; i < N; i++)
ans += (ll) (x[1][i] + 1 - x[0][i]) * (y[1][i] + 1- y[0][i]);
ans = (ll)W * H - ans;
} else
ans = 2 * (ll)W * H - solve(H, W, y, x) - solve(W, H, x, y);
printf ("%lld\n", ans);
}
return 0;
}

UVA1492 - Adding New Machine(扫描线)的更多相关文章

  1. HDU 4052 Adding New Machine(矩形面积并)

    Adding New Machine Problem Description Incredible Crazily Progressing Company (ICPC) suffered a lot ...

  2. Adding New Machine ZOJ - 3540

    https://vjudge.net/problem/ZOJ-3540 错误记录: 扫描线没有考虑到同一行的要删除在前,加入在后:由于用了特殊的方式所以想当然以为不需要考虑这个问题 #include& ...

  3. HDU 4052 Adding New Machine (线段树+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4052 初始给你w*h的矩阵,给你n个矩形(互不相交),按这些矩形尺寸把初始的矩形扣掉,形成一个新的'矩 ...

  4. 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)

    转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnl ...

  5. hdu 4052 线段树扫描线、奇特处理

    Adding New Machine Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  6. .Uva&LA部分题目代码

    1.LA 5694 Adding New Machine 关键词:数据结构,线段树,扫描线(FIFO) #include <algorithm> #include <cstdio&g ...

  7. QEMU, a Fast and Portable Dynamic Translator-Fabrice Bellard-翻译

    Abstract We present the internals of QEMU, a fast machine emulator using an original portable dynami ...

  8. Machine Learning : Pre-processing features

    from:http://analyticsbot.ml/2016/10/machine-learning-pre-processing-features/ Machine Learning : Pre ...

  9. ON THE EVOLUTION OF MACHINE LEARNING: FROM LINEAR MODELS TO NEURAL NETWORKS

    ON THE EVOLUTION OF MACHINE LEARNING: FROM LINEAR MODELS TO NEURAL NETWORKS We recently interviewed ...

随机推荐

  1. Scala 元组

    与列表一样,元组也是不可变的,但与列表不同的是元组可以包含不同类型的元素. 元组的值是通过将单个的值包含在圆括号中构成的.例如: val t = (1, 3.14, "Fred") ...

  2. Octopress + GitHub Page 搭建个人博客

    Tips:博客已搬家,新地址:http://wanxudong.top 首先说明两个关键术语: Octopress Octopress是基于 Jekyll 的博客框架.他们的关系就像 jQuery 与 ...

  3. undefined null 各种值比较(面试题)

    undefined和null与任何有意义的值比较返回的都是false,但是null与undefined之间互相比较返回的是true. console.log(null == false); //fal ...

  4. 解决WPF中重载Window.OnRender函数失效问题

    今天实验一个绘图算法的时候,偶然发现重载Window.OnRender的方法是没有效果的. public partial class MainWindow : Window { public Main ...

  5. 我弄的一些TASKER配置

    http://tieba.baidu.com/p/2184969007 我弄的一些配置,需要的童鞋们找自己想要的吧,有些配置感觉还是很繁琐,请高手不吝赐教才好,图片太多,就发链接了. ◆保持屏幕开启, ...

  6. gdb参考手册

    http://www.sourceware.org/gdb/current/onlinedocs/gdb.html

  7. 在apache环境中使用 python stock 请求遇到error: [Errno 13] Permission denied

    一个python 项目运行在linux 环境下,使用apache做为web容器. 调用urllib2.urlopen(your url) 或者 xmlrpclib.ServerProxy()请求某个服 ...

  8. C++UI框架

    WTL都算不上什么Framework,就是利用泛型特性对Win API做了层封装,设计思路也没摆脱MFC的影响,实际上用泛型做UI Framework也只能算是一次行为艺术,这个思路下继续发展就会变得 ...

  9. 查看和调试Qt源码(动态编译的QT也可进入源码)good

    简述 在调试程序的时候,有时需要调试进入 Qt 源码,这不仅有利于我们了解内部实现机制,而且对于解决一些隐蔽性问题很有帮助. 都知道 F11 是“单步进入”,可是在调试的过程中,按下 F11 却无法进 ...

  10. Cookie && Session之验证实例

    为了防止各种自动登录,以及反作弊和破坏,往往会要求登录时让用户输入随机产生的验证码(这组验证码是一组数字和字母),这样可以起到一定的防止他人利用程序让机器自动反复登录的情况.在PHP下要实现这种功能是 ...