POJ 2582 Mayor's posters 线段树入门题+离散化
来源:互联网 发布:淘宝直通车调价技巧 编辑:程序博客网 时间:2024/06/03 20:17
本来线段树就学的不扎实,后来个什么离散化,乱乱的啊、、后来看了一人写的,用结构体记录的方法进行离散化。感觉写的挺好的。
题意很简单:就是在一堵墙上涂色,然后输出你能看到的颜色的种类。离散化+涂色问题。
Mayor's posters
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 37301 Accepted: 10846
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:
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
Source
#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-7#define M 10001000//#define LL __int64#define LL long long#define INF 0x3f3f3f3f#define PI 3.1415926535898const int maxn = 100010;using namespace std;struct node{ int l, r; int col; int vis;} f[4*maxn];struct node1{ int a, n, f;} p[maxn*2];struct node2{ int l, r, col;} q[maxn];int used[maxn];int cmp(node1 a, node1 b){ return a.a < b.a;}void Bulid(int l, int r, int site){ f[site].l = l; f[site].r = r; f[site].col = 0; f[site].vis = 0; if(l == r) return; int mid = (l+r)>>1; Bulid(l, mid, site<<1); Bulid(mid+1, r, site<<1|1);}int Search(int l, int r, int site){ int cum = f[site].col; if(f[site].vis) { if(!used[cum]) { used[cum] = 1; return 1; } else return 0; } int mid = (f[site].l+f[site].r)>>1;//注意这里面的边界,是二分子树。 return Search(l, mid, site<<1)+Search(mid+1, r, site<<1|1);}void Updata(int l, int r, int cnum, int site){ if(f[site].vis && f[site].col == cnum) return; if(f[site].l == l && f[site].r == r) { f[site].col = cnum; f[site].vis = 1; return ; } if(f[site].vis) { f[site<<1].col = f[site<<1|1].col = f[site].col; f[site<<1].vis = f[site<<1|1].vis = f[site].vis; f[site].vis = 0; } int mid = (f[site].l+f[site].r)>>1; if(r <= mid) Updata(l, r, cnum, site<<1); else if(l > mid) Updata(l, r, cnum, site<<1|1); else { Updata(l, mid, cnum, site<<1); Updata(mid+1, r, cnum, site<<1|1); }}int main(){ int k; int n; cin >>k; int l, r; while(k--) { scanf("%d",&n); for(int i = 1; i <= n; i++)//离散化 { scanf("%d %d",&l, &r); p[i*2-1].a = l;//保存起始位置 p[i*2-1].n = i; p[i*2-1].f = 0; p[i*2].a = r;//保存结束位置 p[i*2].n = i; p[i*2].f = 1; } sort(p+1, p+n*2+1, cmp); p[0].a = p[1].a; int m = 1, nn, f; for(int i = 1; i <= 2*n; i++) { nn = p[i].n; f = p[i].f; if(p[i].a != p[i-1].a) m++; if(!f) { q[nn].l = m; q[nn].col = nn; } else { q[nn].r = m; q[nn].col = nn; } } Bulid(1,m, 1); for(int i = 1; i <= n; i++) Updata(q[i].l, q[i].r, q[i].col, 1); memset(used, 0 , sizeof(used)); printf("%d\n",Search(1, m, 1)); } return 0;}
0 0
- POJ 2582 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(线段树+离散化)
- POJ 2528 Mayor's posters (线段树+离散化)
- poj 2528 Mayor's posters(线段树+离散化)
- POJ 2528 Mayor's posters 线段树+离散化
- poj - 2528 - Mayor's posters 线段树+离散化
- POJ Mayor's posters (线段树+离散化处理)
- poj 2528 Mayor's posters 线段树+离散化
- poj 2528 Mayor's posters(离散化+线段树)
- POJ 2528 Mayor's posters (线段树+离散化)
- 学习Datatable增删改
- POJ - 1703 Find them, Catch them (并查集2)
- 拆机&清灰&重装系统&数据恢复——心得汇总
- AIX内存查看命令
- VS2008编译boost库
- POJ 2582 Mayor's posters 线段树入门题+离散化
- android 获取SD、ROM容量
- 关于如何实现“拒绝从网络访问这台计算机”中添加guest的办法
- 几个有用内核函数封装(进程、注册表路径)
- jsp运行原理
- 男人事业励志名言
- 堆排序原理图解
- Hive of Hadoop 体系结构
- 引用类型,隐式转换列表,三元操作符