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. Ajax的三种实现及JSON解析

    本文为学习笔记,属新手文章,欢迎指教!! 本文主要是比较三种实现Ajax的方式,为以后的学习开个头. 准备: 1.  prototype.js 2.  jquery1.3.2.min.js 3.  j ...

  2. android 由于界面控件过多耗时处理办法

    在开发当中,有时候可能界面嵌套较多,那么导致控件实例化增多,有时候会大大影响界面加载的速度,特别在viewpage中的时候,要是第一页里面要inflate一个控件比较多的页面的时候,就会影响整个Act ...

  3. TImage也有OnClick事件,可以当按钮使用,配上合适的图片(背景透明,效果前凸)更是几乎以假乱真

    本质上TImage与TSpeedButton没有什么区别,都是没有句柄的,但都可以执行OnClick事件.有空分析一下.

  4. android studio 报错,google后无果

    你可能在环境变量中配置了adk的环境变量,同时eclipse可studio公用一个avd,在两个之间切换时过出错

  5. Get started - UIkit documentation

    Get started - UIkit documentation Get started Get familiar with the basic setup and structure of UIk ...

  6. linux命令:使用man, 导出man

    要查一个命令怎么使用,使用"man 命令", eg: man find, man ls; "info 命令"貌似也可以看, info find, info ls ...

  7. 简单理清一下proto与prototype

    这篇博客主要是为了理清自己的思路. 先上图,所有内容都从这张图来讲. 在js中,所有的东西都是对象,包括是function. prototype这个属性是函数特有的.有两层含义,第一层含义指的是某对象 ...

  8. bootbox api

    bootbox是boostrap集成的弹窗,基本能完成后台系统的需求,下面是一些使用方法 1.bootbox.alert bootbox.alert使用方法主要有三种 直接传内容 bootbox.al ...

  9. Cocos2d-x 手游聊天系统需求分析

    手游聊天系统需求分析 转载请注明:IT_xiao小巫 移动开发狂热者群:299402133 策划需求图 參考系统:刀塔传奇 点击这个.然后弹出以下的对话框 游戏类型:卡牌 分析:刀塔传奇聊天系统分为3 ...

  10. Swift - 使用socket进行通信(附聊天室样例)

    在Swift开发中,如果我们需要保持客服端和服务器的长连接进行双向的数据通信,使用socket是一种很好的解决方案. 下面通过一个聊天室的样例来演示socket通信,这里我们使用了一个封装好的sock ...