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. Kivy: Crossplatform Framework for NUI

    Kivy: Crossplatform Framework for NUI ivy - Open source Python library for rapid development of appl ...

  2. 解决yum升级的问题“There was a problem importing one of the Python modules”

    yum命令升级的时候,报出这个错误. There was a problem importing one of the Python modules required to run yum. The ...

  3. C/C++迭代器使用具体解释

    迭代器是一种检查容器内元素并遍历元素的数据类型.能够替代下标訪问vector对象的元素. 每种容器类型都定义了自己的迭代器类型,如 vector: vector<int>::iterato ...

  4. PHP - Windows安装Pear

    1. 打开命令窗口,切换到php的安装目录,执行以下命令(你也可以添加一个php的环境变量,就不用如此麻烦的切换目录,但是我安装了多个版本的php,所以就没有添加环境变量). 2. 当出现下面这句话时 ...

  5. PHP - 防止非法调用页面

    这是在服务器内部: 首先定义一个常量 在调用页面的时候,检测是否存在此常量 如果存在,则调用 否则,做出提示. 创建常量: 创建常量的函数名称: define //创建一个常量,以便于页面调用,从主页 ...

  6. LA 3942 Remember the Word(前缀树&树上DP)

    3942 - Remember the Word Neal is very curious about combinatorial problems, and now here comes a pro ...

  7. nest expression &amp; Pyparsing

    http://pyparsing.wikispaces.com/ http://bbs.csdn.net/topics/330052586 C++ boost "<([^<> ...

  8. Qt控件精讲一:按钮

    原地址:http://blog.csdn.net/yuxikuo_1/article/details/17397109 Qt Creater提供6种Button控件.如图1. Button控件介绍 控 ...

  9. Oracle 经常使用的改动语句

    一.表Table 測试表:MY_TEST_TABLE -- Create table create table MY_TEST_TABLE ( A VARCHAR2(30), B NUMBER(10) ...

  10. 创建采购订单批到程序用的BAPI

    CALL FUNCTION 'BAPI_PO_CREATE1' EXPORTING poheader = poheader poheaderx = poheaderx * POADDRVENDOR = ...