离散化的思想:

对于这样的数据
(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. POJ 3243

    Babystep算法.具体为什么,我也不太明白,好像资料不多. #include <iostream> #include <cstdio> #include <cstri ...

  2. quick-cocos2dx 之transition.execute()的缓动效果

    注:本文图片来源(http://hosted.zeh.com.br/tweener/docs/en-us/misc/transitions.html. 侵权请告知,即刻删除) 什么是缓动, 缓动(ea ...

  3. 有关文档碎片(document fragment)的使用方法

    通常情况下改动.删除或者添加DOM元素. 更新DOM会导致浏览器又一次绘制屏幕,也会导 致reflow,这样会带来巨大的开销.我们通常解决这的办法尽量降低更新DOM.这也就意 味着将DOM的改变分批处 ...

  4. Hadoop高速入门

    Hadoop高速入门 先决条件 支持平台 GNU/Linux是产品开发和执行的平台. Hadoop已在有2000个节点的GNU/Linux主机组成的集群系统上得到验证. Win32平台是作为开发平台支 ...

  5. 数组进行多少次OP操作,才干有序

    1 题目描写叙述: 有一个数组:2,1,4,3.对于数组,有一种操作op(idx):将该index相应的数字移到首位.比方: op(3):  2 1 43 -> 3 2 1 4 op(1):   ...

  6. 希尔加密算法(湖南师范大学第六届大学生计算机程序设计竞赛)hnuoj11552

    解密 Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 2, Accept ...

  7. jQuery幻灯片插件Owl Carousel

    简介 Owl Carousel 是一个强大.实用但小巧的 jQuery 幻灯片插件,它具有一下特点: 兼容所有浏览器 支持响应式 支持 CSS3 过度 支持触摸事件 支持 JSON 及自定义 JSON ...

  8. phpmyadmin客户端多服务器配置

    修改libraries/config.default.php 545行,添加 $cfg['Servers']['2'] = $cfg['Servers'][$i];$cfg['Servers']['2 ...

  9. 【转】用CocoaPods做iOS程序的依赖管理 -- 不错

    原文网址:http://blog.devtang.com/2014/05/25/use-cocoapod-to-manage-ios-lib-dependency/ 文档更新说明 2012-12-02 ...

  10. 关于HTML与CSS与class

    在web前端开发中接触的一直是html.css.javascript. 在这个过程中,经常使用的是html中的span.div元素以及css的选择器. 为了方便查找在这里将这些内容的基础知识记录下来. ...