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

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

比方:(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. AVL树总结

    定义:一棵AVL树或者是空树,或者是具有下列性质的二叉搜索树:它的左子树和右子树都是AVL树,且左右子树的高度之差的绝对值不超过1 AVL树失衡旋转总结: 假如以T为根的子树失衡.定义平衡因子为 H( ...

  2. python知识点拾遗

    内容概要 1.__str__ 2.os.path相关方法 1.__str__ 我们先定义一个Student类,打印一个实例: class Student(object): def __init__(s ...

  3. 【转载】linux之sed用法

    linux之sed用法 原文地址:http://www.cnblogs.com/dong008259/archive/2011/12/07/2279897.html   sed是一个很好的文件处理工具 ...

  4. c标准库 徐明远 背景基础

    背景基础 1.c语言库用c语言编写   其他语言则不同 早期语言的库是用汇编语言编写的    不同的计算机体系结构有不同的汇编语言   所以在移植性方面差一点   而c语言可以编写出高度可移植性的代码 ...

  5. NYOJ 104 最大和

    最大和 时间限制:1000 ms  |  内存限制:65535 KB 难度:5   描述 给定一个由整数组成二维矩阵(r*c),现在需要找出它的一个子矩阵,使得这个子矩阵内的所有元素之和最大,并把这个 ...

  6. 【bzoj4519】[Cqoi2016]不同的最小割 分治+最小割

    题目描述 学过图论的同学都知道最小割的概念:对于一个图,某个对图中结点的划分将图中所有结点分成两个部分,如果结点s,t不在同一个部分中,则称这个划分是关于s,t的割.对于带权图来说,将所有顶点处在不同 ...

  7. HDU 1423 Greatest Common Increasing Subsequence ——动态规划

    好久以前的坑了. 最长公共上升子序列. 没什么好说的,自己太菜了 #include <map> #include <cmath> #include <queue> ...

  8. BZOJ 1297: [SCOI2009]迷路 [矩阵快速幂]

    Description windy在有向图中迷路了. 该有向图有 N 个节点,windy从节点 0 出发,他必须恰好在 T 时刻到达节点 N-1. 现在给出该有向图,你能告诉windy总共有多少种不同 ...

  9. Laravel 数据库操作之Eloquent ORM模型

    //模型中的相关代码 namespace App; use Illuminate\Database\Eloquent\Model; class Student extends Model{ //默认对 ...

  10. android获取手机号

    private String getPhoneNum(){ //与手机建立连接 TelephonyManager tm = (TelephonyManager)getSystemService(Con ...