须要离散化处理,线段树的区间改动问题。

须要注意的就是离散化的时候,由于给的数字是一段单位长度,所以须要特殊处理(由于线段的覆盖和点的覆盖是不一样的)

比方:(1,10)(1,4) (6,10)

离散化之后是 1 , 4 , 6 , 10 分别离散为 1 2 3 4

覆盖的时候先覆盖1 4 之后覆盖了1 2 之后覆盖了 2 3,结果为2

可是实际上应该是3

13450359 201301052100 2528 Accepted 1140K 985MS C++ 1960B 2014-09-17 15:42:33

985MS险过。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
using namespace std;
typedef long long LL;
const int maxn = 11111;
int n,m,ans;
bool vis[maxn];
int arr[maxn << 2];
int arr_l[maxn];
int arr_r[maxn];
int tr[maxn << 4];
void UpDate(int l,int r,int value,int L,int R,int pos){
if(l <= L && R <= r){
tr[pos] = value;
return ;
}
if(tr[pos] != -1){
tr[pos << 1] = tr[pos];
tr[(pos << 1)|1] = tr[pos];
tr[pos] = -1;
}
int m = (L + R) >> 1;
if(l <= m) UpDate(l,r,value,L,m,pos << 1);
if(r > m) UpDate(l,r,value,m + 1,R,(pos << 1)|1);
return ;
}
void Query(int L,int R,int pos){
if(tr[pos] != -1){
int e = tr[pos];
if(!vis[e]){
vis[e] = true;
ans ++;
}
return ;
}
if(L == R) return ;
int m = (L + R) >> 1;
Query(L,m,pos << 1);
Query(m + 1,R,(pos << 1)|1);
return ;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
memset(vis,false,sizeof(vis));
memset(tr,-1,sizeof(tr));
scanf("%d",&m);
int size = 0;
for(int i = 0 ; i < m ; i++){
scanf("%d%d",&arr_l[i],&arr_r[i]);
arr[size ++] = arr_l[i];
arr[size ++] = arr_r[i];
}
sort(arr,arr + size);
n = unique(arr,arr + size) - arr; //去反复
for(int i = n - 1 ; i > 0 ; i--) //区间转化成点
if(arr[i] != arr[i - 1] + 1)
arr[n ++] = arr[i - 1] + 1;
sort(arr,arr + n);
ans = 0;
for(int i = 0 ; i < m ; i++){
int l = find(arr,arr + n,arr_l[i]) - arr; //二分查找
int r = find(arr,arr + n,arr_r[i]) - arr; //二分查找
UpDate(l,r,i,0,n - 1,1);
}
Query(0,n - 1,1);
printf("%d\n",ans);
}
return 0;
}

【hdu】Mayor&#39;s posters(线段树区间问题)的更多相关文章

  1. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  2. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  3. HDU.1689 Just a Hook (线段树 区间替换 区间总和)

    HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...

  4. POJ2528:Mayor's posters(线段树区间更新+离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  5. poj2528 Mayor's posters(线段树区间修改+特殊离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

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

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

  7. HDU 1698 Just a Hook(线段树 区间替换)

    Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...

  8. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  9. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

  10. HDU 1698 Just a Hook(线段树区间更新查询)

    描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...

随机推荐

  1. luogu1640 [SCOI2010]连续攻击游戏

    二分图匹配,一边是属性值,一边是武器 #include <iostream> #include <cstring> #include <cstdio> using ...

  2. IOS 自动布局-UIStackPanel和UIGridPanel(三)

    在这一篇了我将继续讲解UIGridPanel. 在iphone的app里面可以经常看到一些九宫格布局的应用,做过html开发的对这类布局应该是很熟悉的.在IOS中要实现这样的布局方法还是蛮多的,但是我 ...

  3. enq: TX - row lock contention“等待事件的处理

      enq: TX - row lock contention“等待事件的处理   session1: SQL> conn scott/triger Connected. SQL> CRE ...

  4. lnmp环境的使用

    lnmp环境的使用 安装的软件都安装到了:/usr/local 管理nginx service nginx start|stop|restart|reload 管理mysql 直接执行mysql即可登 ...

  5. HDU-4848 Wow! Such Conquering! 爆搜+剪枝

    Wow! Such Conquering! 题意:一个n*n的数字格,Txy表示x到y的时间.最后一行n-1个数字代表分别到2-n的最晚时间,自己在1号点,求到达这些点的时间和的最少值,如果没有满足情 ...

  6. Nginx+Php中限制站点目录防止跨站的配置方案记录

    Nginx+Php中限制站点目录防止跨站的配置方案记录(使用open_basedir)-------------------方法1)在Nginx配置文件中加入: 1 fastcgi_param  PH ...

  7. 使用Apriori算法进行关联分析

    关联分析是一种在大规模数据集中寻找有趣关系的任务.这些关系可以有两种形式:频繁项集或者关联规则.频繁项集是指经常出现在一块的物品的集合,关联规则暗示两种物品之间可能存在很强的关系.一个项集的支持度被定 ...

  8. bzoj2120 数颜色 莫队 带修改

    [bzoj2120]数颜色 Description 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔 ...

  9. lnux 下 core文件

    1. core文件的简单介绍在一个程序崩溃时,它一般会在指定目录下生成一个core文件.core文件仅仅是一个内存映象(同时加上调试信息),主要是用来调试的. 2. 开启或关闭core文件的生成用以下 ...

  10. jvisualvm远程监控 Visual GC plugin NOT supported for this JVM

    1. 找到jdk安装目录. 2. 进入jdk的 bin目录,新建文件jstatd.all.policy. 3.编辑jstatd.all.policy文件,内容如下: 4. 给jstatd.all.po ...