POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor’s posters (线段树 区间更新 区间查询 离散化)
题意分析
贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报。
最多有10000张海报,海报左右坐标范围不超过10000000。
一看见10000000肯定就要离散化了,因为建树肯定是建不下。离散化的方法是:先存到一个数组里面,然后sort,之后unique去重,最后查他离散化的坐标lower_bound就行了。特别注意如果是从下标为0开始存储,最后结果要加一。多亏wmr神犇提醒。
这题是覆盖问题。如果正向考虑,后者就可以覆盖在前者之上;如果逆向考虑,就是前者不能涂在后者之上。这里采用逆向考虑的方法。
具体实现细节是,我们按照给出的顺序逆向枚举,将查询和更新一体化,如果按照给出的区间查询到某个地方,发现没有涂色,那说明这张海报一定可以看见,反之不能被看见,根据每次查询的结果,来决定计数器是否自增。
代码总览
#include <cstdio>
#include <cstring>
#include <algorithm>
#define nmax 10005
using namespace std;
struct Tree{
int l,r;
//int val;
bool iscover;
int mid(){
return (l+r)>>1;
}
};
Tree tree[nmax<<4];
struct point{
int l;
int r;
}p[nmax<<1];
int finalpos[nmax<<1];
bool flag = false;
int ans = 0;
void PushUp(int rt)
{
tree[rt].iscover = tree[rt<<1].iscover && tree[rt<<1|1].iscover;
}
void Build(int l, int r, int rt)
{
tree[rt].l = l; tree[rt].r = r;
tree[rt].iscover = false;
if(l == r){
return;
}
Build(l,tree[rt].mid(),rt<<1);
Build(tree[rt].mid()+1,r,rt<<1|1);
}
void Query(int l,int r,int rt)
{
if(tree[rt].iscover) return;
if(l>tree[rt].r || r<tree[rt].l) return ;
if(tree[rt].l == tree[rt].r){
if(!tree[rt].iscover){
tree[rt].iscover = true;
flag = true;
}
return;
}
//PushDown(rt);
//if(l <= tree[rt].l && tree[rt].r <= r) return tree[rt].val;
Query(l,r,rt<<1) , Query(l,r,rt<<1|1);
PushUp(rt);
}
int t,n;
int tag = 0;
void discretization()
{
tag = 0;
for(int i = 0;i<n;++i){
scanf(" %d %d",&p[i].l,&p[i].r);
finalpos[tag++] = p[i].l;
finalpos[tag++] = p[i].r;
}
sort(finalpos,finalpos+tag);
tag = unique(finalpos,finalpos+tag) - finalpos;
}
int main()
{
//freopen("in2528.txt","r",stdin);
scanf("%d",&t);
while(t--){
scanf("%d",&n);
discretization();
Build(1,nmax<<2,1);
ans = 0;
for(int i = n-1; i>=0;--i){
flag = false;
int a = lower_bound(finalpos,finalpos+tag,p[i].l) - finalpos+1;
int b = lower_bound(finalpos,finalpos+tag,p[i].r) - finalpos+1;
Query(a,b,1);
if(flag) ans++;
}
printf("%d\n",ans);
}
return 0;
}
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)的更多相关文章
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- 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(线段树,区间覆盖,单点查询)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45703 Accepted: 13239 ...
- POJ 2528 Mayor's posters (线段树+区间覆盖+离散化)
题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- POJ2528:Mayor's posters(线段树区间更新+离散化)
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
- 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 转载自: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 ...
随机推荐
- 使用Mininet创建网络拓扑
使用Mininet创建Topo Python脚本实现创建拓扑 #coding:utf-8 from mininet.net import Mininet from mininet.topo impor ...
- Laxcus大数据操作系统2.0(5)- 第二章 数据组织
第二章 数据组织 在数据的组织结构设计上,Laxcus严格遵循数据和数据描述分离的原则,这个理念与关系数据库完全一致.在此基础上,为了保证大规模数据存取和计算的需要,我们设计了大量新的数据处理技术.同 ...
- k8s zookeeper、kafka部署
安装zookeeper apiVersion: v1 kind: ConfigMap metadata: name: zookeeper-config namespace: kube-system a ...
- hadoop2.7.1安装和部署
操作系统:Red Hat Enterprise Linux Server release 6.2 (Santiago) hadoop2.7.1 三台redhat linux主机,ip分别为10.204 ...
- 无人驾驶技术之Kalman Filter原理介绍
基本思想 以K-1时刻的最优估计Xk-1为准,预测K时刻的状态变量Xk/k-1,同时又对该状态进行观测,得到观测变量Zk,再在预测与观之间进行分析,或者说是以观测量对预测量进行修正,从而得到K时刻的最 ...
- 算法笔记(c++)--回文
算法笔记(c++)--回文 #include<iostream> #include<algorithm> #include<vector> using namesp ...
- Visiting a Friend(思维)
Description Pig is visiting a friend. Pig's house is located at point 0, and his friend's house is l ...
- 解决Ubuntu16.04下git clone太慢问题
记录一些博客,省着自己再去找了... ss-qt5安装 生成.pac genpac --pac-proxy "SOCKS5 127.0.0.1:1080" --gfwlist-pr ...
- 软工实践-Alpha 冲刺 (6/10)
队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 已经解决登录注册等基本功能的界面. 完成了主界面的基本布局 ...
- lintcode-414-两个整数相除
414-两个整数相除 将两个整数相除,要求不使用乘法.除法和 mod 运算符. 如果溢出,返回 2147483647 . 样例 给定被除数 = 100 ,除数 = 9,返回 11. 标签 二分法 思路 ...