UVA11983 - Weird Advertisement(扫描线)

题目链接

题目大意:给你n个覆盖矩形,问哪些整数点是被覆盖了k次。

题目大意:这题和hdu1542是一个题型。可是这题求的是覆盖k次的点。所以pushup里面就要改一下。详细的看代码把。大概的意思就是每次都是通过以下的两个孩子节点的覆盖信息更新父节点的覆盖信息。然后这题也是须要离散化建树。比較须要注意的是这题和hdu1542不一样的地方是边界,由于那题是求面积。边界并不须要考虑。而这题是求点的个数,边界上的点也是要算的。建树的时候考虑叶子节点范围的时候就要包含边界上的点。

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector> using namespace std; #define lson(x) (x<<1)
#define rson(x) ((x<<1) | 1) const int maxn = 60005;
typedef long long ll; 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;
}
}; struct Node { int l, r, add;
ll s[12];
void set (int l, int r, int add) { this->l = l;
this->r = r;
this->add = add;
}
}node[maxn * 4]; vector<int> pos;
vector<Line> L;
int n, k; void pushup(int u) { for (int i = 0; i <= k; i++)
node[u].s[i] = 0L; int t;
if (node[u].l == node[u].r) {
t = min(k, node[u].add);
node[u].s[t] = pos[node[u].l + 1] - pos[node[u].l];
} else {
for (int i = 0; i <= k; i++) {
t = min (i + node[u].add, k);
node[u].s[t] += node[lson(u)].s[i] + node[rson(u)].s[i];
}
}
} void build (int u, int l, int r) { node[u].set(l, r, 0); if (l == r) {
pushup(u);
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 (l <= node[u].l && r >= node[u].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 () { int x1, y1, x2, y2;
scanf ("%d%d", &n, &k);
L.clear();
pos.clear(); for (int i = 0; i < n; i++) { scanf ("%d%d%d%d", &x1, &y1, &x2, &y2); L.push_back(Line(x1, y1, y2 + 1, 1));
L.push_back(Line(x2 + 1, y1, y2 + 1, -1));
pos.push_back(y1);
pos.push_back(y2 + 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 solve () { init();
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);
ans += node[1].s[k] * (L[i + 1].x - L[i].x);
}
return ans;
} int main () { int T;
scanf ("%d", &T); for (int cas = 1; cas <= T; cas++)
printf ("Case %d: %lld\n", cas, solve());
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

UVA11983 - Weird Advertisement(扫描线)的更多相关文章

  1. uva 11983 Weird Advertisement 扫描线

    Weird Advertisement Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/probl ...

  2. UVA 11983 Weird Advertisement

    题意:求矩形覆盖k次以上的区域总面积. 因为k≤10,可以在线段树上维护覆盖次数为0,...,k, ≥k的长度数量. 然后就是一个离散化以后扫描线的问题了. 离散化用的是半开半闭区间,以方便表示没有被 ...

  3. UVA 11983 Weird Advertisement --线段树求矩形问题

    题意:给出n个矩形,求矩形中被覆盖K次以上的面积的和. 解法:整体与求矩形面积并差不多,不过在更新pushup改变len的时候,要有一层循环,来更新tree[rt].len[i],其中tree[rt] ...

  4. UVA 11983 Weird Advertisement(线段树求矩形并的面积)

    UVA 11983 题目大意是说给你N个矩形,让你求被覆盖k次以上的点的总个数(x,y<1e9) 首先这个题有一个转化,吧每个矩形的x2,y2+1这样就转化为了求N个矩形被覆盖k次以上的区域的面 ...

  5. [转载]完全版线段树 by notonlysuccess大牛

    原文出处:http://www.notonlysuccess.com/ (好像现在这个博客已经挂掉了,在网上找到的全部都是转载) 今天在清北学堂听课,听到了一些很令人吃惊的消息.至于这消息具体是啥,等 ...

  6. 【转】线段树完全版~by NotOnlySuccess

    线段树完全版  ~by NotOnlySuccess 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章了,觉 ...

  7. 《完全版线段树》——notonlysuccess

    转载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文 ...

  8. 【转】 线段树完全版 ~by NotOnlySuccess

    载自:NotOnlySuccess的博客 [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时觉得挺自豪的,还去pku打广告,但是现在我自己都不太好意思去看那篇文章 ...

  9. 【转载】完全版线段树 by notonlysuccess大牛

    原文出处:http://www.notonlysuccess.com/ 今晚上比赛就考到了 排兵布阵啊,难受. [完全版]线段树 很早前写的那篇线段树专辑至今一直是本博客阅读点击量最大的一片文章,当时 ...

随机推荐

  1. Java正则表达式例子汇总

    1.过滤特殊字符 package com.sheepmu.text; /* * @author sheepmu */ public class HWCompetition { public stati ...

  2. define a class for a linked list and write a method to delete the nth node.

    1.问题 define a class for a linked list and write a method to delete the nth node. 2.算法 template <t ...

  3. mysql语句在node.js中的写法

    总结一下mysql语句在node.js中的各种写法,参考了npm网站mysql模块给的实例. 查询 select //1 db.query('select * from tuanshang_users ...

  4. hdu1004----用java链表实现

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  5. CF(427D-Match &amp; Catch)后缀数组应用

    题意:给两个字符串,求一个最短的子串.使得这个子串在两个字符串中出现的次数都等于1.出现的定义为:能够重叠的出现. 解法:后缀数组的应用.从小枚举长度.假设一个长度len合法的话:则一定存在这个样的s ...

  6. 谈论json - json经常使用的功能

    json经常使用的功能有JSON.parse().JSON.stringify(),供json对象和字符串之间的相互转换. 1.JSON.parse() 将 JavaScript 对象符号 (JSON ...

  7. 玩转Web之JavaScript(三)-----javaScript语法总结(三) 窗口/滚动条/文本的相关语法

    JS语法集锦(三) 窗口/滚动条/文本 alert("文本")    警告框:警告框经常用于确保用户可以得到某些信息,当警告框出现后,用户需要点击确定按钮才能继续进行操作. con ...

  8. 探秘Java虚拟机——内存管理与垃圾回收(转)

    本文主要是基于Sun JDK 1.6 Garbage Collector(作者:毕玄)的整理与总结,原文请读者在网上搜索. 1.Java虚拟机运行时的数据区 2.常用的内存区域调节参数 -Xms:初始 ...

  9. Linux核心regulator建筑和准备

    电源引入的物种 (百度百科)LDO这是low dropout regulator,这意味着低压差线性稳压器.它相比于传统的线性调节器.传统的线性稳压器.例如78xx系列芯片需要输入电压比输出电压高2v ...

  10. 【iOS7一些总结】9、与列表显示(在):列表显示UITableView

    列表显示,顾名思义它是在一个列表视图的形式显示在屏幕上的数据的内容.于ios在列表视图UITableView达到.这个类在实际应用中频繁,是很easy理解.这里将UITableView的主要使用方法总 ...