HDU 5107 线段树扫描线

给出N个点(x,y),每个点有一个高度h

给出M次询问,问在(x,y)范围内第k小的高度是多少,没有输出-1 (k<=10)

线段树扫描线

首先离散化Y坐标,以Y坐标建立线段树

对所有的点和询问进行离线操作,将询问和点按照x,y的大小排序,从左向右,从下向上,对于相同的(x,y)插入点在询问点之前

线段树的每个节点维护10个高度,每次询问[0,mark[i].y]的第mark[i].h高的值即可

#include "stdio.h"
#include "string.h"
#include "algorithm"
#include "map"
using namespace std;

map<int,int>mp;
struct Mark
{
    int x,y,h,op,id;
}mark[60010];
struct Ans
{
    int w; // 记录共有多少个
    int h[11];
}ans;
struct node
{
    int l,r;
    Ans x;
}data[300010];
int y[60010],pri[30010];
bool cmp_mark(Mark a,Mark b)
{
    if (a.x!=b.x) return a.x<b.x;
    else
        if (a.y!=b.y) return a.y<b.y;
    else
        return a.op<b.op;
}

Ans Merge(Ans a,Ans b)
{
    int i,j,k;
    Ans c;
    i=j=k=1;
    while ((i<=a.w || j<=b.w) && k<=10)
    {
       if (j > b.w || (i <= a.w && a.h[i] < b.h[j]) )
            c.h[k++] = a.h[i++];
        else
            c.h[k++] = b.h[j++];
    }
    c.w=k-1;
    return c;
}

void build(int l,int r,int k)
{
    int mid;
    data[k].l=l;
    data[k].r=r;
    data[k].x.w=0;

    if(l==r) return ;

    mid=(l+r)/2;

    build(l,mid,k*2);
    build(mid+1,r,k*2+1);
}

void updata(int n,int op,int k)
{
    int mid;
    if (data[k].l==n && data[k].r==n)
    {
        data[k].x.w=1;
        data[k].x.h[1]=op;
        return ;
    }

    mid=(data[k].l+data[k].r)/2;

    if (n<=mid) updata(n,op,k*2);
    else if (n>mid) updata(n,op,k*2+1);

    data[k].x=Merge(data[k*2].x,data[k*2+1].x);
}

Ans query(int l,int r,int k)
{
    int mid;
    if (data[k].l==l && data[k].r==r)
        return data[k].x;

    mid=(data[k].l+data[k].r)/2;

    if (r<=mid) return query(l,r,k*2);
    else
        if (l>mid) return query(l,r,k*2+1);
    else
        return Merge(query(l,mid,k*2),query(mid+1,r,k*2+1));
}
int main()
{
    int n,m,i,cnt,temp;
    while (scanf("%d%d",&n,&m)!=EOF)
    {
        for (i=0;i<n;i++)
        {
            scanf("%d%d%d",&mark[i].x,&mark[i].y,&mark[i].h);
            y[i]=mark[i].y;
            mark[i].op=1;
        }
        for (i=n;i<n+m;i++)
        {
            scanf("%d%d%d",&mark[i].x,&mark[i].y,&mark[i].h);
            mark[i].id=i-n;
            y[i]=mark[i].y;
            mark[i].op=2;
        }
        cnt=n+m;

        sort(y,y+cnt);
        sort(mark,mark+cnt,cmp_mark);

        temp=unique(y,y+cnt)-y; // 离散化

        for (i=0;i<temp;i++)
            mp[y[i]]=i;

        build(0,temp-1,1);

        for (i=0;i<cnt;i++)
        {
            if (mark[i].op==1)
                updata(mp[mark[i].y],mark[i].h,1);
            else
            {
                ans=query(0,mp[mark[i].y],1); // 询问返回该区间内前10个最小高度
                if (mark[i].h<=ans.w)
                pri[mark[i].id]=ans.h[mark[i].h];
                else
                    pri[mark[i].id]=-1;
            }
        }
        for (i=0;i<m;i++)
            printf("%d\n",pri[i]);
    }
    return 0;
}
时间: 2024-08-29 08:14:44

HDU 5107 线段树扫描线的相关文章

hdu 1255(线段树 扫描线) 覆盖的面积

http://acm.hdu.edu.cn/showproblem.php?pid=1255 典型线段树辅助扫描线,顾名思义扫描线就是相当于yy出一条直线从左到右(也可以从上到下)扫描过去,此时先将所有的横坐标和纵坐标排序 因为是从左到右扫描,那么横坐标应该离散化一下 当扫描线依次扫描的时候,依次扫描到的纵区间在线段树中查找,依据是上边还是下边记录,上边就是-1,下边就是+1, 如果某区间记录值为0的时候,代表没有被覆盖,为1的时候代表覆盖一次,为2代表覆盖两次(不会出现为负数的情况) 最后将依

hdu 1542 线段树+扫描线

啦啦啦~继续学算法 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7349    Accepted Submission(s): 3231 Problem Description There are several

HDU 5091 线段树扫描线

给出N个点,和一个w*h的矩形 给出N个点的坐标,求该矩形最多可以覆盖多少个点 对每个点point(x,y)右边生成对应的点(x+w,y)值为-1: 纵向建立线段树,从左到右扫描线扫一遍,遇到点则用该点的权值更新区间(y,y+h) #include "stdio.h" #include "string.h" #include "algorithm" using namespace std; struct Mark { int x,y,s; }ma

HDU 1542 线段树+扫描线+离散化

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8327    Accepted Submission(s): 3627 Problem Description There are several ancient Greek texts that contain descriptions of the fabled is

hdu 1264 线段树+扫描线

说实话  真是水题   题目给的数据太小  都不用离散化      之前用离散化处理一个类似的题 这道题是给你几个矩形    让你求覆盖面积    重复的只算一次 做完之后被坑了        题目说输入左下角和右下角坐标    但是     左上角和右下角也有可能   只要是对角就行    所有得线处理一下 #include<stdio.h> #include<iostream> #include<algorithm> #include<string.h>

HDU 2461 线段树扫描线

给出N个矩形,M次询问 每次询问给出R个,问这R个矩形围成的面积 经典扫面线求面积并,对每次询问的R个点离散化一下 #include "stdio.h" #include "string.h" #include "algorithm" #include "map" using namespace std; map<int,int>mp; struct P { int x1,y1,x2,y2; }p[21]; str

HDU 3642 线段树+离散化+扫描线

题意:给你N个长方体的左下角和右上角坐标,问你空间中有多少体积是被大于两个不同的立方体覆盖的.x,y~10^6 z~500 考虑到给的z比较小,所以可以直接枚举z,然后跑二维的扫描线就好. 关于处理被不同的线段覆盖三次的问题,可以维护四个信息,cnt,once,twice,more,然后相互推出结果就好. #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #

HDU 1828 Picture 线段树+扫描线

题意:给你一些矩形的左上角点的坐标和右下角点的坐标,求周长并 最显而易见的思路就是对于x轴和y轴做两次扫描线,对于负数的坐标进行离散化.每次增加的值是线段变化量的绝对值.具体写法和求面积并的差不多. #include <cstdio> #include <algorithm> #include <cstring> #include <vector> using namespace std; #define lson rt << 1 , l , m

HDU 3642 Get The Treasury 线段树+扫描线

反向标记是错的,要对矩形进行拆分 #include <cstdio> #include <algorithm> #include <cstring> #include <vector> typedef long long LL; using namespace std; #define lson rt << 1,l,mid #define rson rt << 1 | 1,mid + 1,r const int maxn = 5e4