hdu 1542 upcoj 2540 矩形面积并 线段树

线段树矩形面积并讲解 ACM中对于矩形面积并用线段树+离散化+ 扫描线一类问题求解 立即下载

  几天没AC了 。。。太爷跟我说什么有个线段树的题很简单,可以做一下。。于是被坑了。。

  那个题是upcoj 2540,求一堆矩形重叠2次部分的面积,现在我还只会线段树简单的点修改区间修改,呵呵呵一看这题我果断不会做。。百度了一下,看了太爷给的一个不错的线段树汇总http://www.notonlysuccess.com/index.php/segment-tree-complete/,算是对这种矩形面积并的类型有点了解了,最基础的是hdu 1542,求矩形面积的和,upcoj上重叠2次的是这种问题的变形。

  这种题目的方法是扫描线法。先把所有x坐标记录,排序,把一个矩形的上下两条线加入结构体,按y坐标排序。注意一般的线段树区间[L,R]就是指[L,R],这里区间[L,R]指的是[x[L],x[R]]这段区间,L是点x[L],R是点x[R],取mid的时候也是x[mid]。所谓扫描线就是假设有一根线顺着y坐标从下往上扫。


  hdu 1542

Atlantis

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6082    Accepted Submission(s): 2661


Problem Description
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.
 

Input
The input file consists of several test cases. Each test case starts with a line containing a single integer n (1<=n<=100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.

The input file is terminated by a line containing a single 0. Don’t process it.
 

Output
For each test case, your program should output one section. The first line of each section must be “Test case #k”, where k is the number of the test case (starting with 1). The second one must be “Total explored area: a”, where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.

Output a blank line after each test case.
 

Sample Input
  
2 10 10 20 20 15 15 25 25.5 0
 

Sample Output
  
Test case #1 Total explored area: 180.00
 

  有两种方法,第一种方法比较慢,感觉也没有第二种方法好理解,看看就行了。这种方法对于每个y坐标的扫描线的区间就是这条线的[x1,x2],并且update的时候必须要找到线段树底。


  红色代表扫描线,update的时候ly[o]代表[L,R]这个区间上一次扫描线的y坐标,l[o]和r[o]代表x[L]和x[R],covered[o]代表这个区间目前有几条线覆盖(如果covered不为0,说明当前位置在矩形内部),初始化的时候把下面的边cover设为1,上面的边cover设为-1。这种方法几乎是需要查询到叶子节点的,而且感觉没有通用性。

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cstring>
#include<queue>
#include<map>
#include<set>
#include<stack>
#define INF 0x3f3f3f3f
#define MAXN 110
#define MAXM 110
#define MAXNODE 8*MAXN
#define eps 1e-9
#define pi 4*atan(1.0)
#define pii pair<int,int>
using namespace std;
int N,M,NX,covered[MAXNODE];
double ly[MAXNODE],l[MAXNODE],r[MAXNODE],x[2*MAXN];
struct Line{
    double x1,x2,y;
    int cover;
    bool operator < (const Line &a) const{
        return y<a.y;
    }
}line[2*MAXN];
void add_line(double x1,double y1,double x2,double y2){
    line[M].x1=x1;
    line[M].x2=x2;
    line[M].y=y1;
    line[M++].cover=1;
    line[M].x1=x1;
    line[M].x2=x2;
    line[M].y=y2;
    line[M++].cover=-1;
}
void build(int o,int L,int R){
    covered[o]=0;
    l[o]=x[L];
    r[o]=x[R];
    if(L+1>=R) return;
    int mid=(L+R)>>1;
    build(o<<1,L,mid);
    build(o<<1|1,mid,R);
}
double query(int o,int L,int R,double ql,double qr,double y,int cover){
    if(ql>=x[R]||qr<=x[L]) return 0;
    double ret=0;
    if(L+1>=R){
        if(covered[o]){
            ret+=(y-ly[o])*(r[o]-l[o]);
            covered[o]+=cover;
            ly[o]=y;
        }
        else{
            covered[o]+=cover;
            ly[o]=y;
        }
        return ret;
    }
    int mid=(L+R)/2;
    if(ql<=x[mid]) ret+=query(o<<1,L,mid,ql,qr,y,cover);
    if(qr>x[mid]) ret+=query(o<<1|1,mid,R,ql,qr,y,cover);
    return ret;
}
int main(){
    freopen("in.txt","r",stdin);
    int cas=0;
    while(scanf("%d",&N),N){
        memset(covered,0,sizeof(covered));
        M=0;
        NX=1;
        double x1,y1,x2,y2;
        for(int i=0;i<N;i++){
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            x[NX++]=x1;
            x[NX++]=x2;
            add_line(x1,y1,x2,y2);
        }
        sort(x+1,x+NX);
        sort(line,line+M);
        build(1,1,NX-1);
        double ans=0;
        for(int i=0;i<M;i++) ans+=query(1,1,NX-1,line[i].x1,line[i].x2,line[i].y,line[i].cover);
        printf("Test case #%d\nTotal explored area: %.2lf\n\n",++cas,ans);
    }
    return 0;
}

  推荐第二种方法,更好理解,并且可以用于这个问题的变形,更新的时候不用查找到叶子节点,效率明显高。


  这里的扫描线当作无限长,最上面的边不用扫,设sum[o]为节点o区间共有多长的距离被覆盖,那么扫描每条线的时候用sum[1]乘以下一条线的高度减去这条线的高度(因为这部分肯定是在矩形中的),covered[o]是区间o有多少条完全覆盖这个区间的线,扫描每个线的时候更新区间的covered,维护sum。

  注意我用的区间代表的是点,比如L,就代表排序后x[L]这个点,这样的话更新的时候区间分别是[L,mid],[mid,R]而不是mid+1了,这样的话树的高度可能会比以前多一层。如果用L代表排序后x[L]到x[L+1]这条线段,就还是以前的[L,mid],[mid+1,R]了。

  update之前用了二分找当前线的左右坐标分别是第几个点,其实也可以不用找,像第一种方法那样在更新的时候判断也可以。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define INF 0x3f3f3f3f
#define MAXN 210
#define MAXNODE 4*MAXN
#define pi 4*atan(1)
using namespace std;
int N,M,covered[MAXNODE];
double sum[MAXNODE],x[MAXN];
struct Line{
    double x1,x2,y;
    int cover;
    bool operator < (const Line & a) const{
        return y<a.y;
    }
}line[2*MAXN];
void add_line(double x1,double y1,double x2,double y2){
    line[M].x1=x1;
    line[M].x2=x2;
    line[M].y=y1;
    line[M++].cover=1;
    line[M].x1=x1;
    line[M].x2=x2;
    line[M].y=y2;
    line[M++].cover=-1;
}
int bsearch(double *a,int L,int R,double v){  //[L,R]
    while(L<=R){
        int mid=(L+R)/2;
        if(a[mid]==v) return mid;
        if(a[mid]>v) R=mid;
        else L=mid+1;
    }
    return -1;
}
void maintain(int o,int L,int R){
    if(covered[o]) sum[o]=x[R]-x[L];
    else if(L+1>=R) sum[o]=0;
    else sum[o]=sum[o<<1]+sum[o<<1|1];
}
void update(int o,int L,int R,int l,int r,int cover){
    if(l<=L&&r>=R){
        covered[o]+=cover;
        maintain(o,L,R);
        return;
    }
    int mid=(L+R)>>1;
    if(l<mid) update(o<<1,L,mid,l,r,cover);
    if(r>mid) update(o<<1|1,mid,R,l,r,cover);
    maintain(o,L,R);
}
int main(){
    //freopen("in.txt","r",stdin);
    int cas=0;
    while(scanf("%d",&N),N){
        int NX=1;
        M=0;
        double x1,y1,x2,y2;
        for(int i=0;i<N;i++){
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            x[NX++]=x1;
            x[NX++]=x2;
            add_line(x1,y1,x2,y2);
        }
        sort(x+1,x+NX);
        sort(line,line+M);
        NX=unique(x+1,x+NX)-x-1;
        memset(covered,0,sizeof(covered));
        memset(sum,0,sizeof(sum));
        double ans=0;
        for(int i=0;i<M-1;i++){
            int l=bsearch(x,1,NX,line[i].x1),r=bsearch(x,1,NX,line[i].x2);
            update(1,1,NX,l,r,line[i].cover);
            ans+=sum[1]*(line[i+1].y-line[i].y);
        }
        printf("Test case #%d\nTotal explored area: %.2lf\n\n",++cas,ans);
    }
    return 0;
}

 upc 2540

2540: Monitoring System

Time Limit: 5 Sec  Memory Limit: 128 MB
Submit: 39  Solved: 11
[ Submit][ Status][ Web Board]

Description

BCBC is a large bank. To ensure safety, they have set many cameras to monitor the total bank. Recently, they have found some problems in the monitoring system. In the current system, a camera can monitor a rectangle region. Some regions can be monitored by three or more cameras, which is waste; some regions can only be monitored by just one camera, which is not safe. So the manage intends to improve the system. Now he wants to know how many regions are monitored by just two cameras..

Input

There are multiple test cases. Each test case begins with an integer n (1<=n<=100000) standing for the number of cameras. Then n lines follow. Each line has four integers x1, y1, x2, y2 (1<x1, x2, y1, y2<10^9). The four integer descript the rectangle region one camera can monitor, (x1, y1) is the left bottom coordinate and (x2, y2) is the right up coordinate.

Output

.   Each test case output one integer that is the area of regions covered by just exactly two cameras, the result occupied one line.

Sample Input

31 1 3 32 2 4 41 2 3 4

Sample Output

2

  求覆盖2次的面积和。只要求出覆盖2次以上和3次以上的面积和相减就行。代码和上面的几乎相同,就是维护sum的时候要维护sum1,sum2,sum3,分别代表覆盖1次、2次、3次的面积和。维护sum2和sum3的函数和sum1有点不同。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define INF 0x3f3f3f3f
#define MAXN 200010
#define MAXNODE 4*MAXN
#define pi 4*atan(1)
using namespace std;
int N,M,covered[MAXNODE],sum1[MAXNODE],sum2[MAXNODE],sum3[MAXNODE],x[MAXN];
struct Line{
    int x1,x2,y,cover;
    bool operator < (const Line & a) const{
        return y<a.y;
    }
}line[2*MAXN];
void add_line(int x1,int y1,int x2,int y2){
    line[M].x1=x1;
    line[M].x2=x2;
    line[M].y=y1;
    line[M++].cover=1;
    line[M].x1=x1;
    line[M].x2=x2;
    line[M].y=y2;
    line[M++].cover=-1;
}
int bsearch(int *a,int L,int R,int v){  //[L,R]
    while(L<=R){
        int mid=(L+R)/2;
        if(a[mid]==v) return mid;
        if(a[mid]>v) R=mid;
        else L=mid+1;
    }
    return -1;
}
void maintain1(int o,int L,int R){
    if(covered[o]) sum1[o]=x[R]-x[L];
    else if(L+1>=R) sum1[o]=0;
    else sum1[o]=sum1[o<<1]+sum1[o<<1|1];
}
void maintain2(int o,int L,int R){
    if(covered[o]>=2) sum2[o]=x[R]-x[L];
    else if(covered[o]==1){
        if(L+1>=R) sum2[o]=0;
        else sum2[o]=sum1[o<<1]+sum1[o<<1|1];
    }
    else{
        if(L+1>=R) sum2[o]=0;
        else sum2[o]=sum2[o<<1]+sum2[o<<1|1];
    }
}
void maintain3(int o,int L,int R){
    if(covered[o]>=3) sum3[o]=x[R]-x[L];
    else if(covered[o]==2){
        if(L+1>=R) sum3[o]=0;
        else sum3[o]=sum1[o<<1]+sum1[o<<1|1];
    }
    else if(covered[o]==1){
        if(L+1>=R) sum3[o]=0;
        else sum3[o]=sum2[o<<1]+sum2[o<<1|1];
    }
    else{
        if(L+1>=R) sum3[o]=0;
        else sum3[o]=sum3[o<<1]+sum3[o<<1|1];
    }
}

void update(int o,int L,int R,int l,int r,int cover){
    if(l<=L&&r>=R){
        covered[o]+=cover;
        maintain1(o,L,R);
        maintain2(o,L,R);
        maintain3(o,L,R);
        return;
    }
    int mid=((L+R)>>1);
    if(l<mid) update(o<<1,L,mid,l,r,cover);
    if(r>mid) update(o<<1|1,mid,R,l,r,cover);
    maintain1(o,L,R);
    maintain2(o,L,R);
    maintain3(o,L,R);
}
int main(){
    freopen("in.txt","r",stdin);
    int cas=0;
    while(scanf("%d",&N)!=EOF){
        int NX=1,x1,y1,x2,y2;
        M=0;
        for(int i=0;i<N;i++){
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            x[NX++]=x1;
            x[NX++]=x2;
            add_line(x1,y1,x2,y2);
        }
        sort(x+1,x+NX);
        sort(line,line+M);
        NX=unique(x+1,x+NX)-x-1;
        int MAX=(NX<<2);
        for(int i=0;i<=MAX+10;i++) covered[i]=sum1[i]=sum2[i]=sum3[i]=0;
        long long ans=0;
        for(int i=0;i<M-1;i++){
            int l=bsearch(x,1,NX,line[i].x1),r=bsearch(x,1,NX,line[i].x2);
            if(line[i].x1!=line[i].x2)
            update(1,1,NX,l,r,line[i].cover);
            ans+=(long long)(sum2[1]-sum3[1])*(line[i+1].y-line[i].y);
        }
        printf("%lld\n",ans);
    }
    return 0;
}


Materials Studio与LAMMPS联合作战:如何用msi2lmp.exe完美转换聚合物模型(含PCFF力场配置) 本文详细解析了如何利用msi2lmp.exe工具将Materials Studio中的聚合物模型高效、准确地转换为LAMMPS可识别的data文件,并重点配置PCFF力场。文章提供了从MS模型构建、文件导出到命令行转换、参数调整及转换后验证的全流程实战指南与避坑技巧,旨在帮助研究者解决模型转换中的常见问题,实现跨平台分子动力学模拟的无缝衔接。 阅读详情

相关推荐

17【Protues单片机仿真】基于51单片机的太阳能智能谷物翻晒机器人

基于51单片机,避障,低于50CM报警,LED灯亮起,自动翻晒用光敏电阻,光照强度大,电机转动,相当于翻晒粮食,远程控制用按键代替,按下去电机就转动,相当于翻晒粮食,然后光照强度和超声波的距离都在LCD1602显示屏显示出来。

weixin_70360261的博客 1456

【线段树】矩形面积并

题目描述 给出n个矩形的左下角和右上角的坐标,求矩形面积的并。 矩形面积并指的是被矩形覆盖到的面积和,重叠部分仅算一次。 输入 多组输入,每组首先一个整数 n,代表矩形的数量; 接下来每行四个整数 x1,x2,y1,y2,表示左上角坐标(x1,y1)与右下角(x2,y2) 输出 每行一个整数,表示矩形的面积并。 数据范围 1 ≤\leq≤ n ≤\leq≤ 1e5 1 ≤\leq≤...

Pig_cfbsl的博客 2190

Akamai-3.0补环境逆向-DHL快递查询

那么我们的请求过程就是:1.请求首页的html文件,通过re或xpath,提取到这个url,拼接官网地址,得到这个js文件的地址。返回的_abck为0,表示环境正确,请求搜索接口返回200,说明返回的_abck正确,如果过程错误或环境补的有问题会返回403或428这两个状态码。那么,就是这个包返回的,而且这个包需要请求两次,第一次get请求,返回的这个-1的cookie,第二次请求,带上一个sensor_data的参数,进行post请求,最终返回这个0的cookie,代表你过了akamai这个盾。

2302_76420666的博客 2873

线段树常用案例2——矩形面积并

本篇博客在上一篇博客基础上讲解,链接:线段树 引例 来看一道题目:Atlantis 这道题的题意是给定若干个平行于坐标轴的矩形,求出这若干个矩形的面积之和,如果有重合的部分,只算一次。 首先考虑暴力的算法,即遍历每个矩形,用一个vis数组记录每个点的访问状态,最后可以统计出总的面积,这种方法在数据小且边长为整数的时候适用。 这类问题应该引入扫描线来做,下面来讲讲扫描线的概念。 扫描线在并行面积并中的应用 首先得定义扫描线 ,扫描线就是一条设想的线,它可以是从水平方向上下扫描,也可以从竖直方向左

qq_43569892的博客 712

CiteSpace基础操作(CNKI数据的导入分析)(一)

1 Cite Space简单介绍 2 文献导入 2.1 打开知网,选择高级检索

m0_50990607的博客 5万+

【2026OD新机考】【哈希表】20260422-文章特征提取【Py/Java/C++/C/JS/Go六种语言OD真题】【欧弟算法】全网注释最详细分类最全的华子OD真题题解

90+天陪伴式学习,100+直播课时,300+动画图解视频,500+LeetCode经典题,500+华为OD真题/大厂真题,还有简历修改、模拟面试、陪伴小群、资深HR对接将为你解锁。这道题的本质是多集合交集问题的变形,属于非常典型的哈希表应用题。,储存全局的每个片段中,各个字符最少出现的次数。表示该字母并非所有片段的公共字母。,按照顺序构建答案即可。没有字母在所有三个字符串中都出现。每个字母在各字符串中都至少出现。次,所以结果中每个字母出现一次。这样,当上述循环结束后,最终。显然在上述代码中,如果字母。

weixin_48157259的博客 83

计算几何-扫描线

第二步 :每一个矩形的下边权值为1(代表入边),上边权值为-1(出边),结合线段树维护区间,当扫到①时a[1]到a[2]这一段长度更新,扫描线长度a[2]-a[1],当扫到②边a[2]到a[3]更新,a[3]到a[4],a[2]到a[3]标记累计,扫描线长度a[4]-a[1]。当扫到③边a[1]到a[3]更新,扫描线长度a[4]-a[2],有一个发现a[2]到a[3]总共被累计两次,所以即便③边出边了一段a[2]到a[3]但是扫描线的长度还是有这段距离的。正解 下面完整代码👇。今日正题基础应用一面积并。

wyzz_yz的博客 525

基于STM32L4XX 、HAL库的ATD5984步进电机驱动器驱动程序应用设计

一、简介: ATD5984是一款内部集成了译码器的微特步进电机 驱动器,能使双极步进电机以全、半、1/4、/1/8、1/16 和1/32步进模式工作。步进模式由逻辑输入管脚MSx选 择。其输出驱动能力达到28V±1.6A。 译码器是ATD5984易于使用的关键,通过STEP管脚 输入一个脉冲就可以使电机完成一次步进,省去了相序 表,高频控制线及复杂的编程接口,使其更适于没有复 杂的微处理器或微处理器负担过重的场合。 在步进操作期间,ATD5984的内部电路可以自动

PP_Cottage的博客 423

矩形面积并、矩形面积交、矩形周长并(线段树、扫描线总结)

转自:http://blog.csdn.net/lwt36/article/details/48908031 HDU 1542 [POJ 1151] Atlantis (矩形面积并) 题意: 求N=100个矩形的面积并 分析: 离散化: 这些技巧都是老生常谈的了, 不然浮点数怎么建树, 离散化x坐标就可以了扫描线: 首先把矩形按y轴分成两条边,

ACM Dream 5416

Apple Store 上架过包:iOS审核4.3被拒原因分析与解决修改方案

Apple Store 上架过包:iOS审核4.3被拒原因分析与解决修改方案

专业解决各种上架过包问题,专业团队专业处理 764

30天入门 SAP BTP 开发系列之四:SAP BTP 控制台里 Global Account 的常规操作介绍

笔者前一篇文章,介绍了 SAP BTP 中 Global Account 的概念。本文咱们一起来看看 SAP BTP 控制台里,给 Global Account 开放了哪些操作功能。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 880

扫描线&离散化&线段树解决矩形面积并-洛谷P5490

求 nnn 个四边平行于坐标轴的矩形的面积并。第一行一个正整数 nnn。接下来 nnn 行每行四个非负整数 x1,y1,x2,y2x_1, y_1, x_2, y_2x1​,y1​,x2​,y2​,表示一个矩形的四个端点坐标为 (x1,y1),(x1,y2),(x2,y2),(x2,y1)(x_1, y_1),(x_1, y_2),(x_2, y_2),(x_2, y_1)(x1​,y1​),(x1​,y2​),(x2​,y2​),(x2​,y1​)。一行一个正整数,表示 nnn 个矩形的并集覆盖的总面积。

学习,你不是一个人在战斗 ~ 961302305(QQ,微信) 1471

游戏服务器框架之跨服(一)

如今的游戏开发,不搞个跨服玩法都不好意思说在做游戏了(当然,也跟游戏类型有关,一些轻度休闲游戏可以排除在外)。跨服玩法的设计,可以进一步激发玩家追求高战力的虚荣心,也可以汇聚玩家数量,避免单服日活跃低呈现死服现象。 几种常见的跨服玩法拓扑结构 一,不同游戏服的玩家进入中立服务器进行战斗,例如跨服天梯类玩法。 游戏服有很多个节点,中心服务器也有很多节点。只要保证对战的双方同时进入同一个中心服游戏进程即可。 二,若干个游戏服组成一个社区,全部服务器包含多个社区,例如世界服玩法。 三,游戏服A玩

littleschemer的博客 5788

poj-1151矩形面积并-线段树

title: poj-1151矩形面积并-线段树 date: 2018-10-30 22:35:11 tags: acm 刷题 categoties: ACM-线段树 概述 线段树问题里的另一个问题,,,矩形面积并,,,, 之前看lazy更新时看到下面这个的讲解,,,一大堆文字还有一大堆的图,,,,当时果断跳过,,, 今天花了一下午加一晚上的时间看了看这块知识,,,然后尝试自己写出代码,,,...

weixin_30684743的博客 229

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

POJ 1151 Atlantis 线段树求矩形面积并 方法详解

第一次做线段树扫描法的题,网搜各种讲解,发现大多数都讲得太过简洁,不是太容易理解。所以自己打算写一个详细的。看完必会o(∩_∩)o 顾名思义,扫描法就是用一根想象中的线扫过所有矩形,在写代码的过程中,这根线很重要。方向的话,可以左右扫,也可以上下扫。方法是一样的,这里我用的是由下向上的扫描法。 如上图所示,坐标系内有两个矩形。位置分别由左下角和右上角顶点的坐标来给出。上下扫描法...

weixin_30511107的博客 320

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

线段树求矩形面积并 方法详解 (扫描线)HDU 1542 & HDU 3265 & POJ 1151

线段树求矩形面积并 方法详解 (扫描线)HDU 1542 & HDU 3265 & POJ 1151

5352

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

线段树-矩形面积求并

题目链接 题目描述: 给定很多个矩形,给定方式是对角线坐标点.求面积的并。 大致思路: 扫描线+线段树; 1 #include <stdio.h> 2 #include <iostream> 3 #include <algorithm> 4 #include <string.h> 5 #i...

weixin_30851409的博客 186

HDOJ 1542 Atlantis

扫描线+线段树 求矩形面积并 Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5753    Accepted Submission(s): 2528 Problem Descript

码代码的猿猿 1597

hdu1542 Atlantis (线段树+扫描线+离散化)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9032    Accepted Submission(s): 3873 Problem Description There are several

苏苏爱自由 3180

Hdu 1542 Atlantis 线段树 求矩形面积并

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4691    Accepted Submission(s): 2095 Problem Description There are several

Azarath Metrion Zinthos 845

Hdu 1255 覆盖的面积 线段树+矩形面积并

继续面积并学习中。。。(线段树解决) 题意:给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积 思路:其实跟求矩形面积并的思想是一样的,只不过在update里做了一点修改,矩形面积并只需要求至少覆盖一次的面积,而这题是至少覆盖两次的面积,稍微做点修改就可以了 一样的,要求面积,就要求出至少覆盖两次的有效长度乘以高度差即可,求有效长度: 1.cnt>1 : 说明该区间被覆盖两次或以

SeeYouAlways 1207

hdu 1542 Atlantis(线段树 面积并+扫描线)

http://acm.hdu.edu.cn/showproblem.php?pid=1542 题意:给n个矩形的左下角和右上角的顶点坐标,求n个矩形的面积并。 思路: 首先离散化,因为矩形端点坐标非常大。如果从下向上扫描,离散横坐标,如果从右向左扫描,离散纵坐标。这里,离散横坐标,从下向上扫描。 离散时,先对横坐标排序, 去重,最后每个下标对应一个横坐标。再对离散后的横坐标建树。用结构体

1506

矩形相关问题——周长、面积并,面积交(总结)(线段树综合运用)(加载ing)

关于线段树应用的一些问题 今天做到求矩形合并面积的一道同类题——求矩形组的总周长和,但是..它好像和上课说的矩形面积并操作有些小小的不同.. 这是初始给出数据所得到的一幅图,由图中可以看到会有两种情况: 1、图中的矩形会有一块覆盖在另外一块上,但是这种情况下周长是不用计算的。(如下图) 2、图中的多块矩形相交后会有中空部分要求多算长度 以上两种情况都会对离散化有影响,这个方面真的想了好久...

朝阳的二愣子的博客 1604

hdu1542 面积交详细讲解

下面资料 均为网上收集 给定一个矩形的左下角坐标和右上角坐标分别为:(x1,y1)、(x2,y2),对这样的一个矩形,我们构造两条线段,一条定位在x1,它在y坐标的区间是[y1,y2],并且给定一个cover域值为1;另一条线段定位在x2,区间一样是[y1,y2],给定它一个cover值为-1。根据这样的方法对每个矩形都构造两个线段,最后将所有的线段根据所定位的x从左到右进行排序。 ...

java开发指南博客 【转载】 179

hdu 1542 线段树之扫描线之面积并

点击打开链接 题意:给你n个矩形,求它们的面积,重复的不重复计算 思路:用线段树的扫描线完成,将X坐标离散化后,从下到上扫描矩形,进行各种处理,看代码注释把#include #include #include #include #include using namespace std; typedef long long ll; const int maxn=1e3+10; #def

Dan__ge的博客 1938

uva11996 - Jewel Magic 伸展树

Problem J Jewel Magic I am a magician. I have a string of emeralds and pearls. I may insert new jewels in the string, or remove old ones. I may even reverse a consecutive part of the string. At

7529
上一篇: AC自动机
下一篇: upc 2555: Longest Non-decreasing Substring 区间合并线段树
小小小小葱
博客等级 码龄14年 22粉丝 266原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址