Mayor's posters 离散化+线段树染色
来源:互联网 发布:java格式化json字符串 编辑:程序博客网 时间:2024/06/12 13:49
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:
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.
- 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.
The picture below illustrates the case of the sample input.
Sample Input
151 42 68 103 47 10
Sample Output
4
额,,关于离散化网上有很多博客都讲的很清楚,简单说就是一种映射,把大范围的数都映射到小空间上,不然会MLE,不要被“离散化”这个词吓到,其实很容易实现的
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; const int N = 1e4 + 5; struct tree{ int l, r; int color;}STree[N << 4];int li[N], ri[N], x[N << 2];int cnt;bool vis[N << 2];void Build(int l, int r, int i){ STree[i].l = l; STree[i].r = r; STree[i].color = -1; if(l == r) return; int mid = (l + r) >> 1; Build(l, mid, i << 1); Build(mid + 1, r, i << 1 | 1);}void Push_Down(int i){ if(STree[i].color != -1) { STree[i << 1].color = STree[i << 1 | 1].color = STree[i].color; STree[i].color = -1; }}void Update(int i, int l, int r, int color){ if(STree[i].l >= l && STree[i].r <= r) { STree[i].color = color; return; } Push_Down(i); int mid = (STree[i].l + STree[i].r) >> 1; if(l <= mid) Update(i << 1, l, r, color); if(r > mid) Update(i << 1 | 1, l, r, color);}void Query(int i){ if(STree[i].color != -1) { if(!vis[STree[i].color]) { cnt++; vis[STree[i].color] = 1; } return; } if(STree[i].l == STree[i].r) return; Query(i << 1); Query(i << 1 | 1);}int main() { int t, n; scanf("%d", &t); while(t--) { scanf("%d", &n); int k = 1; for(int i = 1; i <= n; i++) { scanf("%d%d", &li[i], &ri[i]); x[k++] = li[i]; x[k++] = ri[i]; } sort(x, x + k); int m = 1; for(int i = 1; i < k; i++) if(x[i] != x[i-1]) x[m++] = x[i]; for(int i = m - 1; i > 1; i--) if(x[i] != x[i-1] + 1) x[m++] = x[i-1] + 1; sort(x, x + m); Build(1, m - 1, 1); for(int i = 1; i <= n; i++) { int l = lower_bound(x, x + m, li[i]) - x; int r = lower_bound(x, x + m, ri[i]) - x; Update(1, l, r, i); } memset(vis, 0, sizeof(vis)); cnt = 0; Query(1); printf("%d\n", cnt); } return 0; }
阅读全文
0 0
- Mayor's posters 离散化+线段树染色
- POJ2528 Mayor's posters(线段树+离散化+染色)
- 【线段树+离散化】Mayor's Posters
- 线段树+离散化Mayor's posters
- 离散化+线段树 POJ 2528 (Mayor's posters)
- POJ - 2528 Mayor's posters(线段树+离散化)
- POJ Mayor's posters 2528(线段树+离散化)
- poj 2528 Mayor's posters(线段树,离散化,成段更新染色)
- (重温)poj 2528 Mayor's posters 线段树 染色+离散化
- poj 2528 Mayor's posters(线段树+离散化,染色)
- POJ 2528-Mayor's posters(线段树区间染色+离散化)
- POJ 2528 Mayor's posters(线段树染色+离散化)
- POJ 2528 Mayor's posters (区间离散化、线段树区间染色)
- poj_2528 Mayor's posters(线段树染色+离散化处理)
- POJ 2528 Mayor's posters (线段树 区间更新+离散化 经典染色问题)
- POJ 2528 Mayor's posters(离散化+线段树)
- poj 2528 Mayor's posters 离散化 线段树
- pku 2528 Mayor's posters-线段树+离散化
- asynchttpclient学习
- Linux环境下安装JDK
- HDU 3744
- MATLAB笔记
- cookie、session、storage总结
- Mayor's posters 离散化+线段树染色
- java打印等腰三角形,金字塔,for循环嵌套
- 【tyvj1293】小v的红豆(次短路---一条路只能经过一次)
- iOS开发---UIImage和NSData的相互转换
- 实验九 路由器RIP动态路由配置
- Ubuntu14.04与Win10双系统环境的配置(引导, 无线网卡)
- 【CS 1557】热浪(Bellman_ford、Spfa、Spfa-slf三杀)
- 《将博客搬至CSDN》
- View的事件分发机制