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 ...
随机推荐
- poj 2115 C Looooops(扩展gcd)
题目链接 这个题犯了两个小错误,感觉没错,结果怒交了20+遍,各种改看别人题解,感觉思路没有错误,就是wa. 后来看diccuss和自己查错,发现自己的ecgcd里的x*(a/b)写成了x*a/b.还 ...
- fil_space_create
/*******************************************************************//** Creates a space memory obje ...
- JS省队集训记
不知不觉省队集训已经结束,离noi也越来越近了呢 论考前实战训练的重要性,让我随便总结一下这几天的考试 Day 1 T1 唉,感觉跟xj测试很像啊?meet in middle,不过这种题不多测是什么 ...
- UVa 10601 (Polya计数 等价类计数) Cubes
用6种颜色去染正方体的12条棱,但是每种颜色都都限制了使用次数. 要确定正方体的每一条棱,可以先选择6个面之一作为顶面,然后剩下的四个面选一个作为前面,共有24种. 所以正方体的置换群共有24个置换. ...
- bzoj1834: [ZJOI2010]network 网络扩容
努力看了很久样例一直过不了...然后各种输出中间过程啊巴拉巴拉弄了1h,没办法了...然后突然想到啊原来的边可以用啊为什么不用...于是A了...感人肺腑 #include<cstdio> ...
- [反汇编练习] 160个CrackMe之015
[反汇编练习] 160个CrackMe之015. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...
- 对于随机变量的标准差standard deviation、样本标准差sample standard deviation、标准误差standard error的解释
参考:http://blog.csdn.net/ysuncn/article/details/1749729
- Doubango ims 框架 分析之 多媒体部分
序言 RTP提供带有实时特性的端对端数据传输服务,传输的数据如:交互式的音频和视频.那些服务包括有效载荷类型定义,序列号,时间戳和传输监测控制.应用程序在UDP上运行RTP来使用它的多路技术和chec ...
- js获取客户端IP及地理位置
php获取方法: 1.<?php 2.function get_ip_place(){ 3.$ip=file_get_contents("http://fw.qq.com/ipaddr ...
- [SharePoint 2010]关于基于声明(Claims)的用户认证模式
转:http://blog.csdn.net/zw_2011/article/details/7417132 SharePoint 2010在用户认证模式上,较之以前的版本有了非常大的改变.在Shar ...