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

最开始觉得和zoj1610 count colors应该是一个模板

但是交了以后发现MLE

数据范围太大 没办法直接用数组 所以要离散化

第一次用到离散化 学了个新内容 代码是借鉴了题解

eg 范围[1,6] [1.7] [2,10] [8 18] 将各点排序
1 1 2 6 7 8 10 18   离散后对应的坐标为
 1  2 3 4 5 6  7    再根据原来的点把它们对应起来,则离散后坐标为
[1,3] [1,4] [2,6] [5,7]

离散化代码

for(int i = 0; i < n * 2; i += 2){
//int a, b;
scanf("%d%d",&post[i].x,&post[i + 1].x);
post[i].id = post[i + 1].id = i;
}
sort(post, post + 2 * n, cmp1);
int tot = 0, pre = 0;
for(int i = 0; i < 2 * n; i++){
if(post[i].x == pre)
post[i].x = tot;
else{
pre = post[i].x;
post[i].x = ++tot;
}
}

从后往前贴 能保证已经能看见的poster里总能有最后的一部分不会被遮住

如果发现这段区域已经被完全覆盖了就返回

完整代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<limits>
#include<stack>
#include<queue>
#include<cmath>
#define inf 1000005
//Ï߶ÎÊ÷DÌâ
//http://blog.csdn.net/dt2131/article/details/52919953
//http://www.cnblogs.com/jackge/archive/2013/04/25/3041637.html
using namespace std; const int maxn = 100010;
int c, n,flag;
//int tree[maxn << 2], dis[maxn];
bool vis[maxn];
struct node{
int id, x;
}post[maxn << 2];
struct Tree{
int l, r;
bool vis;
}tree[maxn << 2]; void build(int L, int R, int rt)
{
tree[rt].l = L;
tree[rt].r = R;
tree[rt].vis = 0;
if(tree[rt].l == tree[rt].r)
return;
int mid = (L + R) / 2;
build(L, mid, rt * 2);
build(mid + 1, R, rt * 2 + 1);
} void pushup(int rt)
{
tree[rt].vis = tree[2 * rt].vis && tree[2 * rt + 1].vis;
} /*void pushdown(int rt)
{
if(tree[rt] != -1){
tree[2 * rt] = tree[2 * rt + 1] = tree[rt];
tree[rt] = -1;
}
} void update(int a, int b, int data, int l, int r, int rt)
{
if(a <= l && b >= r){
tree[rt] = data;
return;
}
if(tree[rt] == data)
return;
pushdown(rt);
int mid = (l + r) / 2;
if(a <= mid)
update(a, b, data, l, mid, 2 * rt);
if(b > mid)
update(a, b, data, mid + 1, r, 2 * rt + 1);
}*/ /*void query(int l, int r, int rt)
{
if(tree[rt] != -1){
for(int i = l; i <= r; i++){
dis[i] = tree[rt];
}
return;
}
if(l != r && tree[rt] == -1){
int mid = (l + r) / 2;
query(l, mid, 2 * rt);
query(mid + 1, r, 2 * rt + 1);
}
}
*/
void query(int L, int R, int rt)
{
if(tree[rt].vis)
return;
if(tree[rt].l == L && tree[rt].r == R)
{
tree[rt].vis = 1;
flag = 1;
return;
}
int mid = (tree[rt].l + tree[rt].r) / 2;
if(R <= mid)
query(L, R, 2 * rt);
else if(L >= mid + 1)
query(L, R, 2 * rt + 1);
else{
query(L, mid, 2 * rt);
query(mid + 1, R, 2 * rt + 1);
}
pushup(rt);
} bool cmp1(node a, node b)
{
return a.x < b.x;
} bool cmp2(node a, node b)
{
if(a.id == b.id)
return a.x < b.x;
return a.id > b.id;
} int main()
{
scanf("%d",&c);
while(c--){
scanf("%d",&n);
//memset(tree, -1, sizeof(tree));
//memset(dis, -1, sizeof(dis));
//int k = 1;
for(int i = 0; i < n * 2; i += 2){
//int a, b;
scanf("%d%d",&post[i].x,&post[i + 1].x);
post[i].id = post[i + 1].id = i;
}
sort(post, post + 2 * n, cmp1);
int tot = 0, pre = 0;
for(int i = 0; i < 2 * n; i++){
if(post[i].x == pre)
post[i].x = tot;
else{
pre = post[i].x;
post[i].x = ++tot;
}
}
build(1, 2 * n, 1);
sort(post, post + 2 * n, cmp2);
int ans = 0;
for(int i = 0; i < 2 * n; i += 2){
int l = post[i].x;
int r = post[i + 1].x;
flag = 0;
query(l, r, 1);
if(flag)
ans++;
}
/*memset(vis, 0, sizeof(vis)); int res = 0;
for(int i = 0; i < maxn;){
while(i < maxn && dis[i] == -1)
i++;
if(i >= maxn)
break;
int temp = dis[i];
if(!vis[temp]){
vis[temp] = true;
res++;
}
//res[temp]++;
while(i < maxn && dis[i] == temp)
i++;
} /*int ans = 0;
for(int i = 0; i < maxn; i++){
ans += res[i];
}*/
printf("%d\n",ans);
}
return 0;
}

poj2528 Mayor's posters【线段树】的更多相关文章

  1. poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 43507   Accepted: 12693 ...

  2. POJ2528 Mayor's posters —— 线段树染色 + 离散化

    题目链接:https://vjudge.net/problem/POJ-2528 The citizens of Bytetown, AB, could not stand that the cand ...

  3. [poj2528] Mayor's posters (线段树+离散化)

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

  4. poj2528 Mayor's posters(线段树之成段更新)

    Mayor's posters Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 37346Accepted: 10864 Descr ...

  5. POJ2528:Mayor's posters(线段树区间更新+离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  6. poj2528 Mayor's posters(线段树区间修改+特殊离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  7. poj2528 Mayor's posters(线段树区间覆盖)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 50888   Accepted: 14737 ...

  8. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  9. Mayor's posters(线段树+离散化POJ2528)

    Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 51175 Accepted: 14820 Des ...

  10. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

随机推荐

  1. [Arch] 03. Practice UML in project

    参考列表: 搜索:跟我学UML建模工具StarUML 系列文章 第一部分,下载和安装及破解StarUML工具软件 第二部分,StarUML工具软件的主要功能界面和UML图的创建示例 [Design P ...

  2. C#调用Delphi的dll之详解

    C#调用Delphi接口方法,有两种解决办法: 一.将Delphi程序编译成一个COM组件,然后在C#里引用COM组件. 二.非托管调用Dephi的DLL文件. 这里我们主要讲解一下第二种方法,讲第二 ...

  3. JVM虚拟机内存模型以及GC机制

    JAVA堆的描述如下: 内存由 Perm 和 Heap 组成. 其中 Heap = {Old + NEW = { Eden , from, to } } JVM内存模型中分两大块,一块是 NEW Ge ...

  4. Centos6.3 下使用 Tomcat-6.0.43 非root用户 部署 生产环境 端口转发方式

    一.安装JDK环境 方法一. 官方下载链接 http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260 ...

  5. 【ArcGIS】WebAdaptorIIS 安装前准备及配置Portal For ArcGIS的问题解决

    1.计算机全名配置 2.IIS-服务器证书配置 3.端口绑定 备注:配置Portal For ArcGIS总会提示计算机域名.全名错误.完全限定域名,可能就是没有进行第一步操作 4.Portal目录

  6. 使用 CSV 文件存储

    将爬取到的数据以 CSV 文件形式存储: import csv import requests req = requests.get("http://www.baidu.com/" ...

  7. [SecureCRT] 解决 securecrt failed to open the host key database file 的问题

    SecureCRT 在 Windows XP 和 Windows 7 中的个人应用数据路径是不同的,在 Windows 7 中,应用数据路径为:C:\Users\<username>\Ap ...

  8. AngularJs HTML DOM、AngularJS 事件以及模块的学习(5)

    今天的基础就到了操作DOM,事件和模块的学习,其实我个人感觉学习起来AngularJS并没有想象中的那么的艰难,可能是因为这个太基础化吧,但是我们从初学开始就应该更加的自信一些,后来我可能会写一个小的 ...

  9. 基础知识《十一》Java异常处理总结

    Java异常处理总结           异常处理是程序设计中一个非常重要的方面,也是程序设计的一大难点,从C开始,你也许已经知道如何用if...else...来控制异常了,也许是自发的,然而这种控制 ...

  10. linux系统环境搭建

    一.安装jdk 参考帖子 用yum安装JDK(CentOS) 1.查看yum库中都有哪些jdk版本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 [r ...