Mayor's posters
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 39795   Accepted: 11552

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

大致题意:有n个连续的点,每次给给定区间的点涂上一种颜色,且每次涂的颜色都不同,后涂的颜色会覆盖先涂的颜色,问最后能看到几种颜色。

这题须要用到离散化来将数据映射到一段非常小的范围以大幅降低时间空间复杂度,lazy区间不须要马上更新,可是在寻找lazy区间时要更新碰到的lazy父区间,提供一组測试数据:

6

5

1 4

2 6

8 10

3 4

7 10

3

5 6

4 5

6 8

3

1 10

1 3

6 10

5

1 4

2 6

8 10

3 4

7 10

4

2 4

3 5

1 3

5 7

3

1 10

1 4 

5 10

ans:

4

2

3

4

3

2

有非常多人包含我之前的代码答案都是4 2 2 4 3 2,(可是也能AC,POJ这题数据略渣),第三组数据错误的原因是忽略了位置相邻但区间不相邻的情况,解决方法是在间隔大于1的两点间插入一个中间值,这样映射的时候不该相邻的区间才不会相邻。findHash函数换成二分查找后时间从954ms降低到79ms.

2014-9-6 0:22:36更新,C++WA,G++AC,47ms,倒序插入海报

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define inf 10000002
#define maxn 10002
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std; bool tree[maxn << 4];
struct Node{
int l, r;
} post[maxn];
int hash[inf], tmp[maxn << 1]; bool update(int left, int right, int l, int r, int rt)
{
if(tree[rt]) return false;
if(left == l && right == r){
return tree[rt] = true;
}
bool rst, a, b;
int mid = (l + r) >> 1;
if(right <= mid) rst = update(left, right, lson);
else if(left > mid) rst = update(left, right, rson);
else {
a = update(left, mid, lson);
b = update(mid + 1, right, rson);
rst = a || b;
}
tree[rt] = tree[rt << 1] && tree[rt << 1 | 1];
return rst;
} int main()
{
//freopen("stdin.txt", "r", stdin);
int t, n, i, id, num, count, ans;
scanf("%d", &t);
while(t--){
scanf("%d", &n);
for(i = id = 0; i < n; ++i){
scanf("%d%d", &post[i].l, &post[i].r);
tmp[id++] = post[i].l;
tmp[id++] = post[i].r;
}
sort(tmp, tmp + id);
num = unique(tmp, tmp + id) - tmp;
for(i = count = 0; i < num; ++i){
hash[tmp[i]] = count;
if(i < num - 1){
if(tmp[i] + 1 == tmp[i+1]) ++count;
else count += 2;
} }
memset(tree, 0, sizeof(tree));
for(i = n - 1, ans = 0; i >= 0; --i){
if(update(hash[post[i].l], hash[post[i].r], 0, count, 1))
++ans;
}
printf("%d\n", ans);
}
return 0;
}


//#define DEBUG
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define maxn 10002
using std::sort; int hash[maxn << 2], vis[maxn << 2], visColor[maxn], ans;
int tree[maxn << 4], ori[maxn << 1], idHash, idVis, idOri; int findHash(int n)
{
int left = 0, right = idHash - 1, mid;
while(left <= right){
mid = (left + right) >> 1;
if(n < hash[mid]) right = mid - 1;
else if(n > hash[mid]) left = mid + 1;
else return mid;
}
} void pushDown(int rt)
{
tree[rt << 1] = tree[rt << 1 | 1] = tree[rt];
tree[rt] = -1;
} void build(int l, int r, int rt)
{
tree[rt] = -1;
if(r == l) return; int mid = (l + r) >> 1;
build(lson);
build(rson);
} void update(int left, int right, int val, int l, int r, int rt)
{
if(left == l && right == r){
tree[rt] = val; return;
} if(tree[rt] != -1) pushDown(rt); int mid = (l + r) >> 1;
if(right <= mid) update(left, right, val, lson);
else if(left > mid) update(left, right, val, rson);
else{
update(left, mid, val, lson);
update(mid + 1, right, val, rson);
}
} void query(int l, int r, int rt)
{
if(tree[rt] != -1){
if(!visColor[tree[rt]]){
++ans; visColor[tree[rt]] = 1;
}
return;
} //最后延迟段必然覆盖全部ori区间点,由于映射的每一个点都被用到 if(l == r) return; int mid = (l + r) >> 1;
query(lson);
query(rson);
} int main()
{
#ifdef DEBUG
freopen("stdin.txt", "r", stdin);
freopen("stdout.txt", "w", stdout);
#endif int cas, n, a, b, i, temp;
scanf("%d", &cas); while(cas--){
scanf("%d", &n);
for(i = idVis = idOri = 0; i < n; ++i){
scanf("%d%d", &a, &b);
ori[idOri++] = a; ori[idOri++] = b;
vis[idVis++] = a; vis[idVis++] = b;
} sort(vis, vis + idVis); //temporary for(temp = idVis, i = 1; i < temp; ++i)
if(vis[i] - vis[i - 1] > 1) vis[idVis++] = vis[i] - 1; sort(vis, vis + idVis); hash[0] = vis[0];
for(i = idHash = 1; i < idVis; ++i)
if(vis[i] != vis[i - 1]) hash[idHash++] = vis[i]; for(i = 0; i < idOri; ++i)
ori[i] = findHash(ori[i]); build(0, idHash - 1, 1); //映射区间
memset(visColor, 0, sizeof(visColor)); for(i = 0; i < n; ++i){
update(ori[i << 1], ori[i << 1 | 1], i, 0, idHash - 1, 1);
} ans = 0; query(0, idHash - 1, 1);
printf("%d\n", ans);
}
return 0;
}

POJ2528 Mayor&#39;s posters 【线段树】+【成段更新】+【离散化】的更多相关文章

  1. POJ训练计划2528_Mayor&#39;s posters(线段树/成段更新+离散化)

    解题报告 id=2528">地址传送门 题意: 一些海报,覆盖上去后还能看到几张. 思路: 第一道离散化的题. 离散化的意思就是区间压缩然后映射. 给你这么几个区间[1,300000] ...

  2. ACM: Copying Data 线段树-成段更新-解题报告

    Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...

  3. Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)

    题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...

  4. POJ 2777 Count Color (线段树成段更新+二进制思维)

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  5. hdu 4747【线段树-成段更新】.cpp

    题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...

  6. HDU1698_Just a Hook(线段树/成段更新)

    解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...

  7. HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )

    线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...

  8. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  9. POJ3468_A Simple Problem with Integers(线段树/成段更新)

    解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...

  10. poj 3648 线段树成段更新

    线段树成段更新需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候.延迟标记的意思是:这个区间的左右儿子都需要被更新,但是当 ...

随机推荐

  1. OPENSSL库的使用-DES篇

    一.单DES算法ECB模式加解密 1.使用函数DES_set_key_unchecked设置密钥 2.使用函数DES_ecb_encrypt来进行数据加解密 void DES_ecb_encrypt( ...

  2. JS图片上传后base64转码

    代码: // 获取文件流 var fileObj = document.getElementById('inputId').files; // 实例化一个FileReader对象 var reader ...

  3. vi命令提示:Terminal too wide

    putty: 在我的电脑上,缺省的设置是这样的: localhost:~ eygle$ stty -aspeed 9600 baud; 51 rows; 171 columns; 在远程编辑文件时,减 ...

  4. 用c#开发微信(10) JSSDK 基本用法 分享接口“发送到朋友”

    微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包.通过使用微信JS-SDK,网页开发者可借助微信高效地使用拍照.选图.语音.位置等手机系统的能力,同时可以直接使用微信分享. ...

  5. UPC 2959: Caoshen like math 这就是个水题

    http://acm.upc.edu.cn/problem.php?id=2959 这就是个水题,之所以要写这个题是感觉很有纪念意义 用力看就是盲……23333333333333333 这个题就是最小 ...

  6. Nodejs 项目开发

    最近这几个月都在学习nodejs. 国内nodejs的资料相对较少,就我所搜索到的,CSDN目前的代码托管平台有不少从github弄过来的开源镜像,其它的不错的社区有cnodejs,byvoid的个人 ...

  7. asp.net2.0安全性(3)--验证与授权--转载来自车老师

    "验证"与"授权"是对网页资源安全管理的两道门. 验证(Authentication):检查用户是否是合法的用户.就像是网站大门口的保卫,服责验证使用的用户名和 ...

  8. 初入Android--环境搭建

    Android SDK 可以下载adt-bundle:包含了装好插件的eclipse和android sdk.下载好后,首先设置ANDROID_HOME环境变量:ANDROID_HOME=/home/ ...

  9. Eclipse用法和技巧一:还原视图和编辑器

    链接地址:http://blog.csdn.net/maybe_windleave/article/details/8763744 在实际使用eclipse过程中,由于经常关闭或者打开视图,某一刻你会 ...

  10. QT工程pro设置实践(with QtCreator)----非弄的像VS一样才顺手?

    源地址:http://my.oschina.net/jinzei/blog/100989?fromerr=DhQJzZQe 相信大家很多和我一样,用多了微软给的便利,用人家的就十分不习惯.于是就琢磨原 ...