POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528
给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板。
很明显的线段树区间更新问题,每次放置木板就更新区间里的值。由于l和r范围比较大,内存就不够了,所以就用离散化的技巧 比如将1 4化为1 2,范围缩小,但是不影响答案。
写了这题之后对区间更新的理解有点加深了,重点在覆盖的理解(更新左右两个孩子节点,然后值清空),还是要多做做题目。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int MAXN = 2e4 + ;
struct data {
int l , r , sum;
}T[MAXN << ];
map <int , int> mp;
int x[MAXN] , y[MAXN] , cont , id[MAXN]; void init(int p , int l , int r) {
int mid = (l + r) >> ;
T[p].l = l , T[p].r = r;
if(l == r) {
T[p].sum = ;
return ;
}
init(p << , l , mid);
init((p << )| , mid + , r);
T[p].sum = T[p << ].sum + T[(p << )|].sum;
} int query(int index , int p) {
int mid = (T[p].l + T[p].r) >> ;
if(index == T[p].l && T[p].r == index) {
return T[p].sum;
}
if(T[p].sum) {
T[p << ].sum = T[(p << )|].sum = T[p].sum;
T[p].sum = ;
}
if(index <= mid) {
query(index , p << );
}
else {
query(index , (p << )|);
}
} void updata(int p , int l , int r , int val) {
int mid = (T[p].l + T[p].r) >> ;
if(T[p].l >= l && T[p].r <= r) { //找到了区间,更新这个区间
T[p].sum = val;
return ;
}
if(T[p].sum) { //重点注意,如果这个区间被访问了,并且这个区间要更新,就要将这个区间的值更新到其左右孩子的节点,并且要将这个区间的值清空,这样才能算是覆盖
T[p << ].sum = T[(p << )|].sum = T[p].sum;
T[p].sum = ;
}
if(r <= mid) {
updata(p << , l , r , val);
}
else if(l > mid) {
updata((p << )| , l , r , val);
}
else {
updata(p << , l , mid , val);
updata((p << )| , mid + , r , val);
}
} int main()
{
int t , n;
scanf("%d" , &t);
while(t--) {
scanf("%d" , &n);
mp.clear();
cont = ;
for(int i = ; i <= n ; i++) {
scanf("%d %d" , x + i , y + i);
if(!mp[x[i]]) {
id[++cont] = x[i];
mp[x[i]]++;
}
if(!mp[y[i]]) {
id[++cont] = y[i];
mp[y[i]]++;
}
}
sort(id + , id + cont + );
int len = -; //离散化之后的最大的数
for(int i = ; i <= n ; i++) { //离散化
x[i] = lower_bound(id + , id + cont + , x[i]) - id;
len = max(x[i] , len);
y[i] = lower_bound(id + , id + cont + , y[i]) - id;
len = max(y[i] , len);
}
init( , , len);
for(int i = ; i <= n ; i++) {
updata( , x[i] , y[i] , i);
}
mp.clear();
int res = ;
for(int i = ; i <= len ; i++) {
int temp = query(i , );
if(!mp[temp] && temp) {
res++;
mp[temp]++;
}
}
printf("%d\n" , res);
}
}
POJ 2528 Mayor's posters (线段树区间更新+离散化)的更多相关文章
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- poj 2528 Mayor's posters 线段树区间更新
Mayor's posters Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...
- POJ 2528 Mayor's posters (线段树+区间覆盖+离散化)
题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...
- POJ2528:Mayor's posters(线段树区间更新+离散化)
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
- POJ 2528 Mayor's posters(线段树,区间覆盖,单点查询)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45703 Accepted: 13239 ...
- POJ 2528 Mayor’s posters (线段树段替换 && 离散化)
题意 : 在墙上贴海报, n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000).求出最后还能看见多少张海报. 分析 ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化
Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...
随机推荐
- abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronized?
abstract的method不可以是static的,因为抽象的方法是要被子类实现的,而static与子类扯不上关系! native方法表示该方法要用另外一种依赖平台的编程语言实现的,不存在着被子类实 ...
- uva10820Send a Table
筛法. 首先使cnt[i]=sqr(n/i),这样cnt[i]就表示gcd(x,y)大于等于i的数对的个数,然后倒序枚举减去gcd大于i的个数就可以得到ans[i].最终得到ans[1]. 这个算法单 ...
- UVa 11529 (计数) Strange Tax Calculation
枚举一个中心点,然后将其他点绕着这个点按照极角排序. 统计这个中心点在外面的三角形的个数,然后用C(n-1, 3)减去这个数就是包含这个点的三角形的数量. 然后再枚举一个起点L,终点为弧度小于π的点R ...
- bzoj3798: 特殊的质数
分块打表.块内的暴力块外的打表.开始没有j>0所以WA了. #include<cstdio> #include<cmath> #include<cstring> ...
- HDU 4607 Park Visit (DP最长链)
[题目]题意:N个城市形成一棵树,相邻城市之间的距离是1,问访问K个城市的最短路程是多少,共有M次询问(1 <= N, M <= 100000, 1 <= K <= N). [ ...
- Java [Leetcode 168]Excel Sheet Column Title
题目描述: Given a positive integer, return its corresponding column title as appear in an Excel sheet. F ...
- 开源的rtsp实现
开源的rtsp实现 ============== -- by BeagleTam ...
- IOS的XML文件解析,利用了NSData和NSFileHandle
如果需要了解关于文档对象模型和XML的介绍,参看 http://www.cnblogs.com/xinchrome/p/4890723.html 读取XML 上代码: NSFileHandle *fi ...
- (2)Spring集成Quartz定时任务框架介绍和Cron表达式详解
在JavaEE系统中,我们会经常用到定时任务,比如每天凌晨生成前天报表,每一小时生成汇总数据等等.我们可以使用java.util.Timer结合java.util.TimerTask来完成这项工作,但 ...
- 【MySQL for Mac】终极解决——MySQL在Mac的字符集设置
这个问题烦恼一天了,现在终于得以解决.分享给大家 首先贴出来,亲测不可行的博客连接: http://www.2cto.com/database/201305/215563.html http://bl ...