【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 ...
随机推荐
- java 获取音频文件时长
需要导入jar包:jave 1.0.2 jar 如果是maven项目,在pom.xml文件中添加: <dependency> <groupId>it.sauronsoftwar ...
- c#中的String方法
1.Replace(替换字符):public string Replace(char oldChar,char newChar);在对象中寻找oldChar,如果寻找到,就用newChar将oldCh ...
- Webstrom卡顿问题解决
1.设置node_modules 打开项目,新建node_modules空文件夹,然后右击选择Mark Directory as,选择Excluded. 2.设置ingore文件 files-> ...
- wordpress 使用jquery需要主要的问题
wordpress 使用jquery时,不能直接使用$, 而是用jQuery 代替$, 而且wordpress默认调用jquery
- centos 7 smplayer vlc播放器
centos7安装多媒体播放器SMPlayer 2017-03-13 21:37:14 分类: LINUX 转自:https://wiki.centos.org/TipsAndTricks/Multi ...
- C. RMQ with Shifts
C. RMQ with Shifts Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 131072KB 64-bit intege ...
- [luoguP1053] 篝火晚会(贪心 + 乱搞)
传送门 假设第一个位置是1,那么枚举它的左右两边是谁,有两种情况,然后可以递推求出序列. 然后可以贪心,两个序列有多少个不同的数,答案就是多少,具体为啥,yy一下即可 然后就是判断递推求出的序列和目标 ...
- BZOJ3532 [Sdoi2014]Lis 【网络流退流】
题目 给定序列A,序列中的每一项Ai有删除代价Bi和附加属性Ci.请删除若 干项,使得4的最长上升子序列长度减少至少1,且付出的代价之和最小,并输出方案. 如果有多种方案,请输出将删去项的附加属性排序 ...
- BZOJ3124 [Sdoi2013]直径 【树的直径】
题目 小Q最近学习了一些图论知识.根据课本,有如下定义.树:无回路且连通的无向图,每条边都有正整数的权值来表示其长度.如果一棵树有N个节点,可以证明其有且仅有N-1 条边. 路径:一棵树上,任意两个节 ...
- P2085 最小函数值 (堆)
题目描述 有n个函数,分别为F1,F2,...,Fn.定义Fi(x)=Aix^2+Bix+Ci (x∈N*).给定这些Ai.Bi和Ci,请求出所有函数的所有函数值中最小的m个(如有重复的要输出多个). ...