【hdu】Mayor's posters(线段树区间问题)
须要离散化处理,线段树的区间改动问题。
须要注意的就是离散化的时候,由于给的数字是一段单位长度,所以须要特殊处理(由于线段的覆盖和点的覆盖是不一样的)
比方:(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's posters(线段树区间问题)的更多相关文章
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- 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 (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...
- 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 ...
随机推荐
- checkStyle使用手册
1. Annotations(注解:5个) Annotation Use Style(注解使用风格) 这项检查可以控制要使用的注解的样式. Missing Deprecated(缺少deprecad) ...
- numpy之flatnonzero函数
Return indices that are non-zero in the flattened version of a. This is equivalent to a.ravel().nonz ...
- POJ-1696 Space Ant 凸包版花式水过!
Space Ant 明天早上最后一科毛概了,竟然毫无复习之意,沉迷刷题无法自拔~~ 题意:说实 ...
- tab栏切换效果
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 查询UNDO使用情况
查看UNDO事务占用情况 SELECT s.username, s.sid, pr.PID, s.OSUSER, s.MACHINE, s.PROGRAM, rs.segment_id, r.usn, ...
- BZOJ3122 [Sdoi2013]随机数生成器 【BSGS】
题目 输入格式 输入含有多组数据,第一行一个正整数T,表示这个测试点内的数据组数. 接下来T行,每行有五个整数p,a,b,X1,t,表示一组数据.保证X1和t都是合法的页码. 注意:P一定为质数 输出 ...
- 多线程-java并发编程实战笔记
线程安全性 编写线程安全的代码实质上就是管理对状态的访问,而且通常都是共享的,可变的状态. 一个对象的状态就是他的数据,存储在状态变量中,比如实例域或静态域.所谓共享是指一个对象可以被多个线程访问:所 ...
- 【HDOJ5955】Guessing the Dice Roll(概率DP,AC自动机,高斯消元)
题意: 有n个人,每个人有一个长为L的由1~6组成的数串,现在扔一个骰子,依次记录扔出的数字,如果当前扔出的最后L个数字与某个人的数串匹配,那么这个人就算获胜,现在问每个人获胜的概率是多少. n,l& ...
- 标准C程序设计七---03
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- lnux 下 core文件
1. core文件的简单介绍在一个程序崩溃时,它一般会在指定目录下生成一个core文件.core文件仅仅是一个内存映象(同时加上调试信息),主要是用来调试的. 2. 开启或关闭core文件的生成用以下 ...