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

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

比方:(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. ASP.NET MVC 通用角色权限管理系统

    RightControl 介绍 .NET 通用后台角色权限管理系统,已完成.项目地址:http://106.14.77.184/Admin/Login 码云地址:https://gitee.com/L ...

  2. 1. Go的安装和第一行代码

    Go 语言环境安装 Go 语言支持以下系统: Linux FreeBSD Mac OS X(也称为 Darwin) Windows 安装包下载地址为:https://golang.org/dl/. 如 ...

  3. java foreach的实现原理

    http://blog.csdn.net/moqihao/article/details/51078464 本质是通过集合的iterator方式实现,所以再使用foreach集合,要强制判断集合的是否 ...

  4. 大数据学习——scala入门程序

    安装scala.msi https://blog.csdn.net/sinat_32867867/article/details/80305302 notepad++ object HelloScal ...

  5. wordpress 使用jquery需要主要的问题

    wordpress 使用jquery时,不能直接使用$, 而是用jQuery 代替$, 而且wordpress默认调用jquery

  6. Get 了滤镜、动画、AR 特效,速来炫出你的短视频开发特技!

    在滤镜美颜.搞怪特效.炫酷场景等各种新奇玩法驱动下,短视频开始让人上瘾. 12 月 3 日,七牛云联合八大短视频特效平台共同推出了中国短视频开发者创意大赛(China Short Video Cont ...

  7. 【NOI Linux】复习一波命令行

    $linux$ 终端真是用不惯. 假设 a 是一个可执行文件( $linux$ 下的可执行文件没有后缀 ) 1. size a 计算一个程序的静态内存(全局数组变量.栈空间.堆空间等),单位是字节.除 ...

  8. c++ primer note

    ---恢复内容开始--- 1.decltype 2.auto 3.cbegin 4.cend 5.constexpr 6.(*Parray)[10]=&arr; //Parray 指向一个含有 ...

  9. 51nod1026 矩阵中不重复的元素 V2

    $n \leq 500000,m \leq 500000$的矩阵,第一行第一列是$a^b,2 \leq a,b \leq 500000$,如果一个数是$i^j$那他右边是$i^{j+1}$,下面是${ ...

  10. Xcode打包应用为ipa

    Xcode教程 Xcode4发布测试 打包Archive操作是本文要介绍的内容,发布测试的最后一步打包(Archive),Xcode4帮助文档有比较详细介绍,但是居然是错的,这里说明一下. 1.设置& ...