离散化的思想:

对于这样的数据
(3,10000)。
(9,1000000)。
(5。100000),
(1,1000)。
(7,1000000)
我们能够将其处理为
(2,7)。
(5,9)。
(3,8),
(1,6),
(4,9)

我们再对离散化之后的数据进行处理即可了。
题目意思:

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&#39;s posters(线段树,离散化)的更多相关文章

  1. [poj2528] Mayor's posters (线段树+离散化)

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

  2. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  3. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

  4. Mayor's posters (线段树+离散化)

    Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...

  5. Mayor's posters(线段树+离散化POJ2528)

    Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 51175 Accepted: 14820 Des ...

  6. POJ 2528 Mayor's posters (线段树+离散化)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:75394   Accepted: 21747 ...

  7. poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化

    Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...

  8. POJ2528Mayor's posters[线段树 离散化]

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59683   Accepted: 17296 ...

  9. POJ2528 Mayor&#39;s posters 【线段树】+【成段更新】+【离散化】

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39795   Accepted: 11552 ...

随机推荐

  1. Solr 搜索的过程和所须要的參数

    一个典型的搜索处理过程,以及所须要的參数例如以下: qt:指定一个RequestHandler,即/select.缺省是使用DisMax RequestHandler defType:选择一个quer ...

  2. unity3d面试题摘选(全)

    ======================================= 数据结构和算法非常重要.图形学也非常重要! 大的游戏公司非常看重个人基础.综合能力.也有的看重你实际工作能力,看你的De ...

  3. PHP7添加swoole扩展

    swoole需要php版本在7.0以上. 1.进入php目录中的bin目录下,通过pecl指令进行安装. cd /usr/local/php7/bin [root@localhost bin]# pw ...

  4. nyoj--95--众数问题(水题)

    众数问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 所谓众数,就是对于给定的含有N个元素的多重集合,每个元素在S中出现次数最多的成为该元素的重数, 多重集合S重的重 ...

  5. oracle 11g rac 修改VIP、scan VIP、priv IP

    11GR2 RAC modify vip,public ip,private ip,scan vip实施步骤1 修改目的    根据业务的需求,需要由原来的临时IP改为生产ip,以下为调整前后对应的I ...

  6. 2017-3-3 leetcod 1 35 448

    ACM退役了,接下来是考研的准备,刷刷leetcode保证不会生手,也算是调剂生活,初步计划是每天三题吧,希望可以坚持下去. 打算按照专题来做,先是Array....本来以为特别水,结果.... == ...

  7. Hadoop 三剑客之 —— 分布式文件存储系统 HDFS

    一.介绍 二.HDFS 设计原理     2.1 HDFS 架构     2.2 文件系统命名空间     2.3 数据复制     2.4 数据复制的实现原理     2.5 副本的选择     2 ...

  8. React 父组件触发子组件事件

    Parent组件 import React from "react"; import Child from "./component/Child"; class ...

  9. c++面向对象程序设计 谭浩强 第五章答案

    1: #include <iostream> using namespace std; class Student {public: void get_value() {cin>&g ...

  10. 4. Median of Two Sorted Arrays[H]两个有序数组的中位数

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the midian of the ...