题目大意

在墙上贴海报,墙壁是由一排连续的瓷砖铺成,海报贴在墙壁上必须占据连续的几块瓷砖,海报可以互相覆盖,问最后可以看见几张海报(未被完全覆盖)。

题目分析

墙壁是由连续的一个区间构成,每个海报占据几块瓷砖,即占据一个区间。每次进行贴海报,是进行区间操作,而最后查询有多少个海报可见也是对区间进行查询。对区间进行操作,考虑使用线段树

由于瓷砖数目太大 100000000,直接进行构建线段树会超内存,也会超时。因此需要进行离散化之后再使用线段树: 
    当所有的海报都铺完毕之后,会将墙壁分割成多个连续的区间(由海报的左右两边作为边界),这些区间长度不固定,但是将他们视为相同的一个单元并不影响结果,因此将所有的瓷砖离散化为 这些由海报的边缘分割出来的单元。 
    当张贴海报p的时候,确定该海报占据离散化之后的那些单元区间,然后将该区间标记为海报p;之后再次贴海报p1时,覆盖了海报p的一部分,需要更新之前p覆盖的单元的标记。由于线段树的特点,可以在标记的时候延迟标记。 
    张贴完所有海报之后,进行查询。从线段树的根节点开始递归查询,看节点代表的区间被那个海报完全覆盖。这样可以确定有哪些海报最终可见。

实现(c++)

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std; #define MAX_POSTER_NUM 10001 //海报最多张数
#define MAX_SEG_NUM 2*MAX_POSTER_NUM //由海报边缘确定的最多的分段数
#define MAX_NODE_NUM 4*MAX_SEG_NUM //线段树的节点最多 #define MIN(a, b) a < b? a : b
#define MAX(a, b) a > b? a : b
struct Node{
int left; //该节点代表的离散化之后区间的起始点序号
int right; //该节点代表的离散化之后区间的终止点序号
int beg_byte; //该节点代表离散化之后的区间起始点 代表的起始瓷砖号
int end_byte; //该节点代表离散化之后的区间终止点 代表的终止瓷砖号
int top_cover_num; //该节点代表的离散化之后区间被 那块海报完全覆盖,没被任何瓷砖完全覆盖则为-1
};
Node gNodes[MAX_NODE_NUM]; struct Poster{
int left; //海报占据瓷砖区间的开始
int right; //海报占据瓷砖区间的结束
}; struct SegUnit{
int beg; //离散化之后的区间 的起始瓷砖号
int end; //离散化之后的区间 的终止瓷砖号
};
SegUnit gSegUnit[MAX_SEG_NUM];
Poster gPosters[MAX_POSTER_NUM];
int gSegUnitNum; //离散化之后的总区间数目 vector<int> gPartPoint; //用于离散化区间
int gVisibleCount[MAX_POSTER_NUM]; //判断海报是否可见 //构建线段树, left, right 为离散化之后区间的起始和结束; index为线段树节点编号
void BuildSegTree(int left, int right, int index){
gNodes[index].left = left;
gNodes[index].right = right;
gNodes[index].top_cover_num = -1;
if (left == right){
gNodes[index].beg_byte = gSegUnit[left].beg;
gNodes[index].end_byte = gSegUnit[left].end;
return;
}
int left_child = 2 * index + 1, right_child = 2 * index + 2;
int mid = (left + right) / 2;
BuildSegTree(left, mid, left_child);
BuildSegTree(mid + 1, right, right_child); //父节点的起始和结束的瓷砖编号由 左右子节点决定
gNodes[index].beg_byte = gNodes[left_child].beg_byte;
gNodes[index].end_byte = gNodes[right_child].end_byte;
} //离散化
void InitSegUnit(){
gSegUnitNum = 0;
for (int i = 0; i < gPartPoint.size() - 1; i++){
gSegUnit[gSegUnitNum].beg = gPartPoint[i];
gSegUnit[gSegUnitNum].end = gPartPoint[i + 1];
gSegUnitNum++; }
}
//更新,节点index 之前被某张海报完全覆盖,现在又有新的海报完全或者部分覆盖index。则将index的top_cover_num 更新
void PushDown(int index){
int left_child = 2 * index + 1;
int right_child = 2 * index + 2;
gNodes[left_child].top_cover_num = gNodes[index].top_cover_num;
gNodes[right_child].top_cover_num = gNodes[index].top_cover_num;
} //贴海报
void Post(int poster_num, int poster_beg, int poster_end, int index){
if (gNodes[index].beg_byte >= poster_beg && gNodes[index].end_byte <= poster_end){
gNodes[index].top_cover_num = poster_num;
return;
}
else if (gNodes[index].beg_byte > poster_end || gNodes[index].end_byte < poster_beg){
return;
}
if (poster_beg >= poster_end){
return;
}
int left_child = 2 * index + 1, right_child = 2 * index + 2;
int left_beg = gNodes[left_child].beg_byte, left_end = gNodes[left_child].end_byte,
right_beg = gNodes[right_child].beg_byte, right_end = gNodes[right_child].end_byte; if (gNodes[index].top_cover_num != -1){
PushDown(index);
gNodes[index].top_cover_num = -1;
} Post(poster_num, poster_beg, MIN(left_end, poster_end), left_child);
Post(poster_num, MAX(right_beg, poster_beg), poster_end, right_child);
} //统计有多少海报是可见的
void CalVisibleSeg(int index){
if (gNodes[index].top_cover_num != -1){
gVisibleCount[gNodes[index].top_cover_num] = 1;
return;
} if (gNodes[index].left == gNodes[index].right){
return;
} int left_child = 2 * index + 1;
int right_child = 2 * index + 2;
CalVisibleSeg(left_child);
CalVisibleSeg(right_child);
} void debug(int index){ printf("index = %d, left = %d, right = %d, beg_byte = %d, end_byte = %d, top_num = %d\n", index, gNodes[index].left,
gNodes[index].right, gNodes[index].beg_byte, gNodes[index].end_byte, gNodes[index].top_cover_num); if (gNodes[index].left == gNodes[index].right){
return;
} int left = 2 * index + 1;
int right = 2 * index + 2;
debug(left);
debug(right);
}
int main(){
int cas, poster_num;
scanf("%d", &cas);
while (cas--){
scanf("%d", &poster_num);
gPartPoint.clear();
gPartPoint.reserve(poster_num * 2);
memset(gVisibleCount, 0, sizeof(gVisibleCount)); for (int i = 0; i < poster_num; i++){
scanf("%d %d", &gPosters[i].left, &gPosters[i].right);
gPosters[i].right++; // 将闭区间转换为开区间 [i, j] 覆盖i,i+1, ..j 瓷砖,
//转换为 [i, j +1), 也表示覆盖 i,i+1, ..j 瓷砖 gPartPoint.push_back(gPosters[i].left);
gPartPoint.push_back(gPosters[i].right);
}
//离散化
sort(gPartPoint.begin(), gPartPoint.end());
vector<int>::iterator it = unique(gPartPoint.begin(), gPartPoint.end());
gPartPoint.erase(it, gPartPoint.end());
InitSegUnit();
BuildSegTree(0, gSegUnitNum - 1, 0);
for (int i = 0; i < poster_num; i++){
Post(i, gPosters[i].left, gPosters[i].right, 0);
}
CalVisibleSeg(0);
int result = 0;
for (int i = 0; i < poster_num; i++){
result += gVisibleCount[i];
}
printf("%d\n", result);
}
return 0;
}

最新文章

  1. proteus 运行出错,用户名不可使用中文!
  2. php提示 Notice: Use of undefined constant name - assumed
  3. LeetCode Binary Tree Longest Consecutive Sequence
  4. 循环不变量loop invariant 与 算法的正确性
  5. memcached安装和php-memcached扩展安装.update.2014-08-15
  6. DLL注入之SetWindowsHookEx
  7. 网页热力图 heatmap js
  8. 在word中显示漂亮的代码
  9. 【Oracle&amp;SQLServer】并集、交际、补集
  10. SQL Server索引 - 聚集索引、非聚集索引、非聚集唯一索引 &lt;第八篇&gt;
  11. Python标准库:内置函数dict(**kwarg)
  12. IIS 7.0 Features and Vista Editions
  13. 浅析const标识符在C++函数的功能
  14. 【HotSpot】jps命令行详解
  15. ajaxfileupload插件,C#返回Json数据报错
  16. (NO.00004)iOS实现打砖块游戏(九):游戏中小球与反弹棒的碰撞
  17. python开发基础之数据类型、字符编码、文件操作
  18. 第213天:12个HTML和CSS必须知道的重点难点问题
  19. Luogu P3366 【模板】最小生成树
  20. @Resource 注解的使用

热门文章

  1. Extjs Toolbar 当做弹出菜单
  2. Tslib步骤以及出现问题的解决方案(转)
  3. Node.js Streams:你需要知道的一切
  4. Python if-else and while
  5. 结合使用 Oracle 和 Ruby on Rails 的补充
  6. 机器学习理论之SVM
  7. C++中函数的返回值
  8. sdi 采集卡---环视频拼接直播方案
  9. (转)x264参数中文详解(X264 Settings)
  10. 【AngularJS】AngularJS整合Springmvc、Mybatis环境搭建