博客 :http://www.cnblogs.com/wuyiqi/archive/2012/03/19/2405885.html

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules:

  • Every candidate can place exactly one poster on the wall.
  • All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown).
  • The wall is divided into segments and the width of each segment is one byte.
  • Each poster must completely cover a contiguous number of wall segments.

They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections.
Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall.

Input

The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers l i and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, l i+1 ,... , ri.

Output

For each input data set print the number of visible posters after all the posters are placed.

The picture below illustrates the case of the sample input.

Sample Input

1
5
1 4
2 6
8 10
3 4
7 10

Sample Output

4

题目大意 :  向一面墙上贴东西, 后贴的会覆盖前面的,问最终可以看到的有几种?

这题快要WA 哭我了, 一顿runtime error , 自己上网上搜的AC 代码,开始用g++的, 一直runtime error,后来试了下, C++ 网上的代码就 A 了 , 然后后来也找到了我的代码的问题。

线段树的节点大小一定要 开 到 最下面叶子节点 的 4 倍 啊 !! 

这题还有一点 就是数据很大, 直接线段树操作 铁定是超时 超内存 , 因此要对输入的点进行离散化 。具体离散化 我在专门写一个博客 

下面是我的AC 代码 , C++ G++ 都能过
#include <iostream>
#include <cstdio>#include <cstring>
#include <cmath>
#include <algorithm>using namespace std;
const int eps = 1e4+5;struct point
{int x, y;
}po[eps];int pre[eps<<2];
int a, b, ans;
int lazy[eps<<4];
bool pt[eps<<2];void down(int k){lazy[k<<1] = lazy[k<<1|1] = lazy[k];lazy[k] = 0;
}void change(int l, int r, int pt, int k){if (a <= l && r <= b){lazy[k] = pt;return;}if (lazy[k]) down(k);int m = (l + r) >> 1;if (a <= m) change(l, m, pt, k<<1);if (b > m) change(m+1, r, pt, k<<1|1);
}void query(int l, int r, int k){if (lazy[k]){if (pt[lazy[k]]) ans++;pt[lazy[k]] = false;return;}if (l == r) return;int m = (l + r) >> 1;query(l, m, k<<1);query(m+1, r, k<<1|1);
}int fun(int l, int r, int key){while (l <= r){int mid = (l + r) >> 1;if (pre[mid] == key) return mid;else if (pre[mid] < key) l = mid + 1;else r = mid - 1;}
}int main() {int t, n;cin >> t;while (t--){cin >> n;memset(lazy, 0, sizeof(lazy));int k = 0;for(int i = 1; i <= n; i++){scanf("%d%d", &po[i].x, &po[i].y);pre[k++] = po[i].x, pre[k++] = po[i].y;}sort(pre, pre+k);int m = 1;for(int i = 1; i < k; i++){if (pre[i] != pre[i-1]) pre[m++] = pre[i];}for(int i = m - 1; i >= 1; i--){if (pre[i] != pre[i-1] + 1) pre[m++] = pre[i-1] + 1;}sort(pre, pre+m);for(int i = 1; i <= n; i++){a = fun(0, m - 1, po[i].x) + 1;b = fun(0, m - 1, po[i].y) + 1;change(1, m, i, 1);        }ans = 0; memset(pt, 1, sizeof(pt));query(1, m, 1);printf("%d\n", ans);}return 0;
}

转载于:https://www.cnblogs.com/ccut-ry/p/7643495.html

线段树-离散化处理点相关推荐

  1. poj2528贴海报(线段树离散化)

    //poj2528贴海报(线段树离散化) #include<cstring> #include<iostream> #include<cstdio> #includ ...

  2. 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)

    [POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  3. poj 2528 Mayor's posters(线段树+离散化)

    1 /* 2 poj 2528 Mayor's posters 3 线段树 + 离散化 4 5 离散化的理解: 6 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用 ...

  4. HDOJ 2492 Ping pong 线段树+离散化

    //2492 Ping pong 线段树+离散化 /* 题意: 有一陀人从左到右排成一排,每个人有一个唯一的技能值,每个人都找其他人比赛, 比赛前要再找一个人做裁判,裁判的技能值不能比这两个人都高,也 ...

  5. POJ Mayor's posters——线段树+离散化

    原文:http://blog.163.com/cuiqiongjie@126/blog/static/85642734201261151553308/ 大致题意: 有一面墙,被等分为1QW份,一份的宽 ...

  6. poj 2482 Stars in Your Window(线段树+离散化+线扫描)

    题目:http://poj.org/problem?id=2482 大意:在一个坐标系中给你n(10^4)个点(点的坐标范围为0<=x,y<2^31),每个点有一个权值,然后给你一个长宽分 ...

  7. POJ - 2528 线段树+离散化

    其实很早就在白书上的常用技巧上 看到离散化的操作,但是之前一直没遇到过需要离散化的题目(应该是我太菜的缘故),所以一直也没怎么重视,下面说说这道题目的考点,也就是离散化. 什么是离散化呢?请先自行百度 ...

  8. BZOJ 1852 [MexicoOI06]最长不下降序列(贪心+DP+线段树+离散化)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1852 [题目大意] 给你N对数A1,B1--An,Bn.要求你从中找出最多的对, 把它 ...

  9. 1019.Line Painting(线段树 离散化)

    1019 离散化都忘记怎么写了 注意两个端点 离散化后用线段树更新区间 混色为-1  黑为2  白为1  因为N不大 最后直接循环标记这一段的颜色查找 1 #include <iostream& ...

最新文章

  1. DirectX 9高层着色语言介绍3——语言基础(2)
  2. .NET中颜色的转换方法总结
  3. 基于WSAAsyncSelect模型实现的聊天室图形客户端
  4. 揭秘!2020年4月全国程序员工资统计,新出炉!(包含地区和语言排行)
  5. 【APICloud系列|11】使用APPuploader申请ios开发证书及ios发布证书教程
  6. [转]Spinner的常用技巧
  7. MotionEstimate运动估计综述
  8. 天正安装autocad启动失败_安装天正后cad无法启动 - 卡饭网
  9. leapftp:425 failed to establish connection解决方法
  10. xpath中的contains多个条件的匹配
  11. 站在思想层面看MVX架构
  12. 问题 I: 水流问题
  13. Java基础知识之封装+继承+多态详解
  14. 6个巧做PPT的实用小技巧,学会了瞬间让你的PPT酷起来!
  15. 舰炮火控设备模拟与测试系统的设计
  16. stm32f103 id绑定软件加密破解方法
  17. 没有 Nginx 的未来,Cloudflare 工程师正在用 Rust 重构代码!
  18. STM32MP157 Linux系统移植开发篇4: BootLoader(Uboot)移植
  19. 计算机管理权限数据完整性,某省发布数据完整性审计指南
  20. 2018年中南大学机试题D题

热门文章

  1. mysql连接池失效_连接池隔天失效之异常处理
  2. mysql 导入8msql文件_MySQL导入大容量SQL文件数据问题
  3. break后面的语句还执行吗_流程控制语句
  4. FPGA之道(52)状态机的概念
  5. 【 FPGA 】FIR 滤波器之半带插值器(Half-band Interpolator)
  6. FPGA从Xilinx的7系列学起(2)
  7. 编程语言介绍、python解释器执行代码的过程
  8. Habitica 4.85.5 发布,习惯游戏养成应用
  9. JavaWEB开发21——综合项目(图书商城)
  10. java 不同数据类型之间的转换