poj2528 Mayor's posters(线段树,离散化)
离散化的思想:

n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000)。
求出最后还能看见多少张海报。
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int MAXN = 10000+5;
struct Node{
int l,r;
int c;
}tree[14*MAXN]; //线段树
struct Seg{
int l,r;
}s[MAXN]; //存储报纸的范围
struct Seg2{
int p;
int index;
}a[2*MAXN]; //存放端点信息
bool cmp(Seg2 x,Seg2 y){
return x.p<y.p;
}
int color[2*MAXN];
int ans=0;
void build(int node,int l,int r){
tree[node].l=l,tree[node].r=r,tree[node].c=0;
if (l==r) return;
build(node*2,l,(l+r)/2);
build(node*2+1,(l+r)/2+1,r);
}
void update(int node,int l,int r,int v){
//cout<<l<<" "<<r<<endl;
if (tree[node].l>=l && tree[node].r<=r){
tree[node].c=v;
return;
}
if (tree[node].c>0){
tree[2*node].c=tree[2*node+1].c=tree[node].c;
tree[node].c=0;
}
int mid=(tree[node].l+tree[node].r)/2;
if (r<=mid)
update(2*node,l,r,v);
else if (mid<l)
update(2*node+1,l,r,v);
else{
update(2*node,l,mid,v);
update(2*node+1,mid+1,r,v);
}
}
void count(int node){
if (tree[node].c>0){
if (color[tree[node].c]==0){
ans++;
color[tree[node].c]++;
}
return;
}
if (tree[node].l==tree[node].r)
return;
count(2*node);
count(2*node+1);
}
void solve(int n,int t){
build(1,1,t);
//cout<<"--------"<<endl;
for (int i=1;i<=n;i++){
update(1,s[i].l,s[i].r,i);
//cout<<"123456789\n";
}
ans=0;
memset(color,0,sizeof(color));
count(1);
printf("%d\n",ans);
}
int main(){
int t,n;
scanf("%d",&t);
while (t--){
scanf("%d",&n);
for (int i=1;i<=n;i++){
scanf("%d%d",&s[i].l,&s[i].r);
a[2*i-1].p=s[i].l;
a[2*i-1].index=i; //标记为左端点
a[2*i].p=s[i].r;
a[2*i].index=-i; //标记为右端点
}
sort(a+1,a+2*n+1,cmp);
//数据离散化
int t=1;
int tmp=a[1].p;
for (int i=1;i<=2*n;i++){
if (tmp!=a[i].p){
tmp=a[i].p;
t++;
}
if (a[i].index>0)
s[a[i].index].l=t;
else
s[-a[i].index].r=t;
}
/*
for (int i=1;i<=n;i++){
printf("%d %d\n",s[i].l,s[i].r);
}
*/
solve(n,t);
}
return 0;
}
poj2528 Mayor's posters(线段树,离散化)的更多相关文章
- [poj2528] Mayor's posters (线段树+离散化)
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- Mayor's posters (线段树+离散化)
Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...
- Mayor's posters(线段树+离散化POJ2528)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 51175 Accepted: 14820 Des ...
- POJ 2528 Mayor's posters (线段树+离散化)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions:75394 Accepted: 21747 ...
- poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化
Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...
- POJ2528Mayor's posters[线段树 离散化]
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 59683 Accepted: 17296 ...
- POJ2528 Mayor's posters 【线段树】+【成段更新】+【离散化】
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39795 Accepted: 11552 ...
随机推荐
- C/C++知识要点5——智能指针原理及自己定义实现
智能指针概述: 智能指针用来管理动态对象.其行为类似于常规指针,重要的差别是:它负责自己主动释放所指向的对象. C++ 11标准库提供两种智能指针:shared_ptr.unique_ptr 差别是: ...
- Mac OSX Yosemite 10.10 brew 错误:mktemp: mkdtemp failed on /tmp/git-LIPo: No such file or directory
这个问题困扰了我非常久非常久.使得我不得不花一点时间来说一下解决方法. 事情是这种:前两天兴高採烈的更新了一下宝贝mac到10.10. 一切看起来都那么美好,可是. .当我又一次安装magento的时 ...
- Apache Kylin高级部分之使用Hive视图
本章节我们将介绍为什么须要在Kylin创建Cube过程中使用Hive视图.而假设使用Hive视图.能够带来什么优点.解决什么样的问题.以及须要学会怎样使用视图.使用视图有什么限制等等. 1. ...
- 上机题目(中级)- 用小数形式输出指定符号出现的频率 (Java)
题目例如以下:
- intellij idea 运行jedis
到这里下载 http://mvnrepository.com/ jar包! 将jar包放入项目目录中,并引入! 引入包到项目中!创建对象! package com.company; import re ...
- java生成6位随机数的5种方法
转自:https://blog.csdn.net/u012491783/article/details/76862526/
- 在Maven中引入spring的DAO、DOMAIN、CONTROLLER、VIEW
除了mysql外麻雀虽小,五脏俱全. 参照之前的博客建立好的maven项目如图. 第一步 : 向maven项目中的pom文件添加依赖 ,然后maven install
- Fragment间相互调用并传值
public class MainFragment extends Fragment { private static final String ARG_DATE="com.example. ...
- java实现sql批量插入参数
背景: 需要更新一些不规范的时间格式,如将某个时间格式化为yy-MM-dd,实际上为 yy-MM-dd hh:mm:ss,并且需要提供回滚脚本. 例如:规范化时间的脚本如下: ,) WHERE tes ...
- Redis的配置文件详解
daemonize:如需要在后台运行,把该项的值改为yes pdifile:把pid文件放在/var/run/redis.pid,可以配置到其他地址 bind:指定redis只接收来自该IP的请求,如 ...