HDU 3277 City Horizon(线段树 + 离散化)

2024-02-19 20:38
文章标签 hdu 线段 离散 city horizon 3277

本文主要是介绍HDU 3277 City Horizon(线段树 + 离散化),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接:http://poj.org/problem?id=3277


这个题目算是我真正意义上做的第一个离散化和线段树结合的题目,做了很长时间,不断打补丁最终才AC,从这个题目中我学到了很多

1、离散化的思想

2、线段树的数组下标建树方式(以前全部都是在数组上用指针建立树)

3、L>>1+1是先做1+1,也就是说L>>1+1 <==> L>>2 ,被这个东西坑了好长时间,所以不十分熟悉的运算符不要想当然啊

4、数组下标建立线段树在操作方便程度上和在对应操作情况下数组下标建立树的方式程序运行起来速度快些(不是数组下标快,而是操作简单了)



开始看这个题目始终没能理解离散化到底是怎么回事,不过现在明白了,也就是直线上那么多整数点,如果我们要一个单位的来建立线段树

不说超时了,内存你也开不了,考虑到给定的矩形数目有限,那么离散化,开始不理解离散化是没理解为什么这样就能行,现在懂了点,开

始先对所有线上的点排序,然后根据这些点来建立区间,想一下所有的矩形下边只会在这些点之间,也就是我么只需要给这些点从左到右编

号,然后建立线段树就可以了,接下来插入过程直接用二分查找找到矩形左右下标对应的编号然后更新这个区间就可以了,这个过程还是需

要好好理解的。

下面程序中我觉得这段代码还是需要解释下的:

if(re_root[root].L+1!=re_root[root].R && re_root[root].h !=-1)
re_root[root<<1].h=re_root[(root<<1)+1].h=re_root[root].h,re_root[root].h=-1;

上面代码是在插入过程中的往下拉的一段代码,当全这个大区间root,如果接下来的更新涉及到这个区间,而又不是整个区间

那么当前这个没更新的h就要往下拉,因为当前区间的某一段不是这个高度h了,那么就要向下拉然后把这个区间的h变成-1,

其实-1的意思就是告诉你查询的时候接着向下查询,这段区间要么没有矩形,要么就有高度不同的矩形(高度为0和高度不

为0的也算不同高度矩形)

在插入的时候对高度从小到大排序,这样在插入的时候会省事些,其实不排序也没什么大事!

数组下标建立线段树的AC代码(指针版改装):

#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
#define maxn 440004
struct point
{long long l,r,h;
}org[maxn];
struct node
{long long L,R;long long  h;
}re_root[maxn];
long long  n;
long long cut[maxn*3];
bool cmp1(const point &a,const point &b)
{return a.h < b.h;
}
long long  pos;
int build_tree(long long root,long long l,long long r)
{re_root[root].L=l,re_root[root].R=r;re_root[root].h=-1;if(l+1==r)return 0;long long mid=(l+r)>>1;build_tree(root<<1,l,mid);build_tree((root<<1)+1,mid,r);//教训啊return 0;
}
int find_num(long long  l,long long r,long long  num)
{long long  mid;while(l<=r){mid=(l+r)>>1;if(cut[mid]==num)return mid;if(cut[mid]>num)r=mid-1;elsel=mid+1;}return 0;
}
int insert(long long root,long long l,long long  r,long long  num)
{if(num <= re_root[root].h)return 0;if(re_root[root].L==l && re_root[root].R==r){re_root[root].h=num;return 0;}if(re_root[root].L+1!=re_root[root].R && re_root[root].h !=-1)re_root[root<<1].h=re_root[(root<<1)+1].h=re_root[root].h,re_root[root].h=-1;if(re_root[root].L+1==re_root[root].R)return 0;long long  mid=(re_root[root].L + re_root[root].R)>>1;if(r <= mid)insert(root<<1,l,r,num);else if(l>=mid)insert((root<<1)+1,l,r,num);elseinsert(root<<1,l,mid,num),insert((root<<1)+1,mid,r,num);return 0;
}
long long  find_ans(long long root)
{if(re_root[root].h!=-1){//  printf("%lld %lld %lld\n",cut[re_root[root].R],cut[re_root[root].L],re_root[root].h);return ((long long)(cut[re_root[root].R]-cut[re_root[root].L])*re_root[root].h);}if(re_root[root].L+1 == re_root[root].R)return 0;long long mid=(re_root[root].L+re_root[root].R)>>1;return find_ans(root<<1)+find_ans((root<<1)+1);
}
int main()
{long long  i,j,k,r,l;while(scanf("%lld",&n)!=EOF){k=0;pos=1;for(i=0;i<n;i++){scanf("%lld%lld%lld",&org[i].l,&org[i].r,&org[i].h);cut[k++]=org[i].l,cut[k++]=org[i].r;}sort(cut,cut+k);sort(org,org+n,cmp1);r=1;for(i=1;i<k;i++)if(cut[i]!=cut[i-1])cut[r++]=cut[i];k=r;build_tree(1,0,k-1);for(i=0;i<n;i++){l=find_num(0,k-1,org[i].l);r=find_num(0,k-1,org[i].r);insert(1,l,r,org[i].h);}printf("%lld\n",find_ans(1));}return 0;
}

指针的TLE代码:


#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
using namespace std;
#define maxn 440004
struct point
{long long l,r,h;
}org[maxn];
struct node
{long long L,R;long long  h;node *left,*right;
}re_root[maxn];
long long  n;
long long cut[maxn*3];
bool cmp1(const point &a,const point &b)
{return a.h < b.h;
}
long long  pos;
int build_tree(node *root)
{if(root->L+1 == root->R)return 0;long long  mid=(root->L+root->R)>>1;root->left=&re_root[pos];re_root[pos].h=-1,re_root[pos].L=root->L,re_root[pos++].R=mid;build_tree(root->left);root->right=&re_root[pos];re_root[pos].L=mid,re_root[pos].R=root->R,re_root[pos++].h=-1;build_tree(root->right);return 0;
}
int find_num(long long  l,long long r,long long  num)
{long long  mid;while(l<=r){mid=(l+r)>>1;if(cut[mid]==num)return mid;if(cut[mid]>num)r=mid-1;elsel=mid+1;}return 0;
}
int insert(node *root,long long l,long long  r,long long  num)
{if(root->h >= num)return 0;if(root->L+1 == root->R){root->h=num;return 0;}if(root->L+1!=root->R && root->h !=-1)root->left->h=root->right->h=root->h,root->h=-1;long long  mid=(root->L + root->R)>>1;if(r <= mid)insert(root->left,l,r,num);else if(l>=mid)insert(root->right,l,r,num);elseinsert(root->left,l,mid,num),insert(root->right,mid,r,num);return 0;
}
long long  find_ans(node *root)
{if(root->h!=-1){return ((long long)(cut[root->R]-cut[root->L])*root->h);}if(root->L+1 == root->R)return 0;long long mid=(root->L+root->R)>>1;return find_ans(root->left)+find_ans(root->right);
}
int main()
{long long  i,j,k,r,l;while(scanf("%lld",&n)!=EOF){k=0;pos=1;for(i=0;i<n;i++){scanf("%lld%lld%lld",&org[i].l,&org[i].r,&org[i].h);cut[k++]=org[i].l,cut[k++]=org[i].r;}sort(cut,cut+k);//   sort(org,org+n,cmp1);r=1;for(i=1;i<k;i++)if(cut[i]!=cut[i-1])cut[r++]=cut[i];k=r;re_root[0].L=0,re_root[0].R=k-1,re_root[0].h=-1;build_tree(&re_root[0]);for(i=0;i<n;i++){l=find_num(0,k-1,org[i].l);r=find_num(0,k-1,org[i].r);insert(&re_root[0],l,r,org[i].h);}printf("%lld\n",find_ans(&re_root[0]));//<<endl;}return 0;
}


这篇关于HDU 3277 City Horizon(线段树 + 离散化)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/725895

相关文章

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

hdu 3790 (单源最短路dijkstra)

题意: 每条边都有长度d 和花费p,给你起点s 终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。 解析: 考察对dijkstra的理解。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstrin