Mayor's posters

Description

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 li 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 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+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

这是一道很经典的线段树离散化问题,题目中给出的区间[li,ri]的数据都很大,直接用线段树做的话至少需要10000000*4的空间,因此,我们首先要对数据进行离散化处理
首先,我们要弄清楚离散化的概念,我们根据题目样例进行分析,可以看到[1,4],[2,6],[8,10],[3,4],[7,10]这五个区间,我们将这10个数都拿出来排个序,创建序列a
1 2 3 4 4 6 7 8 10 10
接下来我们对序列a去下重
1 2 3 4 6 7 8 10
然后我们用他们的相对大小(例如:6在这个序列中是第5大,10在这个序列中是第8大)建立另一个序列b
1 2 3 4 5 6 7 8
最后我们用每个数对应的相对大小来替换原来的区间就变成了这样
[1,4],[2,5],[7,8],[3,4],[6,8]
我们用这个区间来计算一下最后能观察到的海报数的话会发现,结果仍然是4

我个人对离散化的理解就是用数字的相对大小来替换他的实际大小从而达到减小其值,却不破坏它的位置关系的目的
这道题我们利用这样的方法就能将li,ri这么大的数缩小到最大只有20000(因为最多有20000个点)
理解题意后我们直接看代码,至于线段树部分就是普通的区间修改而已
#pragma GCC optimize("Ofast")

#include <iostream>
#include <algorithm>
#include <set> using namespace std; #define endl '\n'
#define ll long long struct node
{
int l, r, flag;
} tree[]; int base[], ls[];
set<int> s; void tree_build(int i, int l, int r)
{
tree[i].l = l;
tree[i].r = r;
tree[i].flag = ;
if (l == r)
{
return;
}
int mid = (l + r) >> ;
tree_build(i << , l, mid);
tree_build(i << | , mid + , r);
} void push_down(int i)
{
if (tree[i].flag)
{
tree[i << ].flag = tree[i].flag;
tree[i << | ].flag = tree[i].flag;
tree[i].flag = ;
}
} void update(int i, int l, int r, int num)
{
if (tree[i].l >= l && tree[i].r <= r)
{
tree[i].flag = num;
return;
}
if (tree[i].r < l || tree[i].l > r)
{
return;
}
push_down(i);
if (tree[i << ].r >= l)
{
update(i << , l, r, num);
}
if (tree[i << | ].l <= r)
{
update(i << | , l, r, num);
}
} void search(int i, int l, int r)
{
if (tree[i].flag)
{
s.insert(tree[i].flag);//利用set自动去重的性质来记录海报数量
return;
}
if (l == r)
{
return;
}
int mid = (l + r) >> ;
search(i << , l, mid);
search(i << | , mid + , r);
} int main()
{
ios::sync_with_stdio();
cin.tie();
cout.tie();
int t;
cin >> t;
while (t--)
{
int n, x, y, pos = ;
cin >> n;
s.clear();
tree_build(, , );
for (int i = ; i <= n; ++i)
{
//这地方我们开两个数组,base用与计算每个数的相对位置,ls是离散化后的数组
cin >> x >> y;
base[pos] = x;
ls[pos++] = x;
base[pos] = y;
ls[pos++] = y;
}
//无论是sort还是unique还是lower_bound区间设定都是左闭右开的形式,品,你细细的品
sort(base + , base + pos);
int num = unique(base + , base + pos) - base;//对base排序并去重,必须排序后才能用unique
for (int i = ; i < pos; ++i)
{
ls[i] = lower_bound(base + , base + num, ls[i]) - base;
}
for (int i = ; i < pos; i += )
{
update(, ls[i - ], ls[i], i);
}
search(, , );
cout << s.size() << endl;
}
return ;
}

你以为这就完事了?其实这样离散化在这道题中会有一些bug,我们看这组数据[1,10],[1,3],[6,10],很明显答案是3
但是离散化之后为[1,4],[1,2],[3,4],答案变成了2
为解决这种问题,我们可以在更新线段树的时候将区间从[l,r]变成[l,r-1],就将区间转化成了[1,3],[1,1],[3,3]这样的树
但是当我们遇到这样的数据[1,3],[1,1],[2,2],[3,3],就会导致区间更新时出错,我们可以将初始数据的r都加上1,就排除了li和ri相等的情况,如果没有这种情况,离散化后的区间也都是一样的
其实这道题数据很弱,不管这样的情况也能过(逃
#pragma GCC optimize("Ofast")

#include <iostream>
#include <algorithm>
#include <set> using namespace std; #define endl '\n'
#define ll long long struct node
{
int l, r, flag;
} tree[]; int base[], ls[];
set<int> s; void tree_build(int i, int l, int r)
{
tree[i].l = l;
tree[i].r = r;
tree[i].flag = ;
if (l == r)
{
return;
}
int mid = (l + r) >> ;
tree_build(i << , l, mid);
tree_build(i << | , mid + , r);
} void push_down(int i)
{
if (tree[i].flag)
{
tree[i << ].flag = tree[i].flag;
tree[i << | ].flag = tree[i].flag;
tree[i].flag = ;
}
} void update(int i, int l, int r, int num)
{
if (tree[i].l >= l && tree[i].r <= r)
{
tree[i].flag = num;
return;
}
if (tree[i].r < l || tree[i].l > r)
{
return;
}
push_down(i);
if (tree[i << ].r >= l)
{
update(i << , l, r, num);
}
if (tree[i << | ].l <= r)
{
update(i << | , l, r, num);
}
} void search(int i, int l, int r)
{
//cout << tree[i].flag << " " << tree[i].l << " " << tree[i].r << endl;
if (tree[i].flag)
{
//cout << tree[i].flag << " " << tree[i].l << " " << tree[i].r << endl;
s.insert(tree[i].flag);
return;
}
if (l == r)
{
return;
}
int mid = (l + r) >> ;
search(i << , l, mid);
search(i << | , mid + , r);
} int main()
{
ios::sync_with_stdio();
cin.tie();
cout.tie();
int t;
cin >> t;
while (t--)
{
int n, x, y, pos = ;
cin >> n;
s.clear();
tree_build(, , );
for (int i = ; i <= n; ++i)
{
cin >> x >> y;
base[pos] = x;
ls[pos++] = x;
base[pos] = y + ;
ls[pos++] = y + ;
}
sort(base + , base + pos);
int num = unique(base + , base + pos) - base;
for (int i = ; i < pos; ++i)
{
ls[i] = lower_bound(base + , base + num, ls[i]) - base;
}
for (int i = ; i < pos; i += )
{
update(, ls[i - ], ls[i] - , i);
}
search(, , );
cout << s.size() << endl;
}
return ;
}

 

Mayor's posters (线段树+离散化)的更多相关文章

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

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

  2. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

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

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

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

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

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

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

  6. poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化

    Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...

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

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

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

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

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

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

随机推荐

  1. lumen 笔记一

    可以用config()函数和evn()函数来获取 .evn里面的配置内容 config('app.timezone') 获取配置config(['app.timezone' => 'China/ ...

  2. 21.模块的执行以及__name__

    执行结果: "E:\Program Files\JetBrains\PycharmProjects\python_demo\venv\Scripts\python.exe" &qu ...

  3. 网络状态诊断工具——netstat命令

    netstat命令可以用来查询整个系统的网络状态.百度百科的定义如下: Netstat的定义是: Netstat是在内核中访问网络连接状态及其相关信息的程序,它能提供TCP连接,TCP和UDP监听,进 ...

  4. 【学习笔鸡】快速沃尔什变换FWT

    [学习笔鸡]快速沃尔什变换FWT OR的FWT 快速解决: \[ C[i]=\sum_{j|k=i} A[j]B[k] \] FWT使得我们 \[ FWT(C)=FWT(A)*FWT(B) \] 其中 ...

  5. 洛谷$P2572\ [SCOI2010]$ 序列操作 线段树/珂朵莉树

    正解:线段树/珂朵莉树 解题报告: 传送门$w$ 本来是想写线段树的,,,然后神仙$tt$跟我港可以用珂朵莉所以决定顺便学下珂朵莉趴$QwQ$ 还是先写线段树做法$QwQ$? 操作一二三四都很$eas ...

  6. 洛谷$P$2123 皇后游戏 贪心

    正解:贪心 解题报告: 传送门! 心血来潮打算把$luogu$提高历练地及其之前的所有专题都打通关,,,$so$可能会写一些比较水的题目的题解$QAQ$ 这种题,显然就套路地考虑交换相邻两个人的次序的 ...

  7. 不懂Neo4j?没关系,先学增删改查

    从上篇文章中我们了解到了什么是Neo4j.为什么要用Neo4j.什么场景使用 以及怎么安装,如果您还不想熟悉,点击此处,传送过去哦~ 既然Neo4j是一个图数据库,那么毫无疑问,增删改查是必不可少的, ...

  8. 微信小程序开发笔记(二)

    一.前言 继承上一篇所说的,有了对微信小程序的基础概念后,这边将会示范动手做一个小程序,在动手的过程中我们可以更快的熟悉小程序里面的架构和开发流程. 二.小程序的设计 这次要做的是一个猜数字的程序,程 ...

  9. Ceph 文件系统-全网最炫酷的Ceph Dashboard页面和Ceph监控 -- <5>

    Ceph Dashboard实现 Ceph Dashboard介绍 Ceph 的监控可视化界面方案很多----grafana.Kraken.但是从Luminous开始,Ceph 提供了原生的Dashb ...

  10. springmvc接收json数据的常见方式

    经常使用Ajax异步请求来进行数据传输,传的数据是json数据,json数据又有对象,数组.所有总结下springmvc获取前端传来的json数据方式:1.以RequestParam接收前端传来的是j ...