POJ2528 Mayor's posters 【线段树】+【成段更新】+【离散化】
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 39795 | Accepted: 11552 |
Description
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 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
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's posters 【线段树】+【成段更新】+【离散化】的更多相关文章
- POJ训练计划2528_Mayor's posters(线段树/成段更新+离散化)
解题报告 id=2528">地址传送门 题意: 一些海报,覆盖上去后还能看到几张. 思路: 第一道离散化的题. 离散化的意思就是区间压缩然后映射. 给你这么几个区间[1,300000] ...
- ACM: Copying Data 线段树-成段更新-解题报告
Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...
- 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. 这题 ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- hdu 4747【线段树-成段更新】.cpp
题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...
- HDU1698_Just a Hook(线段树/成段更新)
解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...
- HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )
线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- POJ3468_A Simple Problem with Integers(线段树/成段更新)
解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...
- poj 3648 线段树成段更新
线段树成段更新需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候.延迟标记的意思是:这个区间的左右儿子都需要被更新,但是当 ...
随机推荐
- Boost学习之可移植路径操作--filesystem
Boost.Filesystem 库为对路径.文件和目录进行查询和操作提供了可移植的工具,已经被C++标准委员会接纳包含到TR2中. 编译 使用Boost.Filesystem 库之前要先编译它,请参 ...
- Java 生成本文文件的时候,Dos格式转成Unix格式
仅仅须要在生成文本的最后 加上 sb.append("\n\r");就可以 是/n/r 不是/r/n
- BZOJ 2005: [Noi2010]能量采集( 数论 + 容斥原理 )
一个点(x, y)的能量损失为 (gcd(x, y) - 1) * 2 + 1 = gcd(x, y) * 2 - 1. 设g(i)为 gcd(x, y) = i ( 1 <= x <= ...
- asp.net导出Excel类库
using System; using System.Collections.Generic; using System.Reflection; using System.Web; using Exc ...
- 基于visual Studio2013解决C语言竞赛题之1029二元数组平均值
题目 解决代码及点评 /* 功能:求二维数组中每行元素的平均值,不许引入其它的数组 时间:16:21 2013/10/24 */ #include<stdio ...
- Linux设备驱动中的ioctl
memdev.h #ifndef _MEMDEV_H #define _MEMDEV_H #define MEM_MAGIC 'm' #define MEM_RESTART _IO(MEM_MAGIC ...
- 如何解决android logcat不打印信息在android开发中
用eclipse进行android开发中经常遇到logcat无任何信息输出,这给我们调试程序带来很大的不便.解决办法:window-->show view-->选择android下的dev ...
- 用jQuery实现鼠标在table上移动进行样式变化
1.定义样式 <style type="text/css"> .striped { background-color:red; ...
- 【C语言】超大数乘法运算
昨天做排列组合的时候遇到A(a,b)这个问题,需要计算A(20,20)超大,计算机32位的,最大数只能是2^32,这让我很悲伤! 于是乎就自己研究了如何进行超大数的计算! /************* ...
- mfc修改应用程序外观
1.在窗口创建前修改窗体外观 在BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)函数中修改,其中CREATESTRUCT结构中有诸如窗口大小 ...