zoukankan      html  css  js  c++  java
  • 【ST表】Zoning Houses

    题目描述

    Given a registry of all houses in your state or province, you would like to know the minimum size of an axis-aligned square zone such that every house in a range of addresses lies in the zone or on its border. The zoning is a bit lenient and you can ignore any one house from the range to make the zone smaller.
    The addresses are given as integers from 1..n. Zoning requests are given as a consecutive range of houses. A valid zone is the smallest axis-aligned square that contains all of the points in the range,ignoring at most one.
    Given the (x, y) locations of houses in your state or province, and a list of zoning requests, you must figure out for each request: What is the length of a side of the smallest axis-aligned square zone that contains all of the houses in the zoning request, possibly ignoring one house?

    输入

    Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. Each test case will begin with a line containing two integers n and q (1 ≤ n, q ≤ 105 ), where n is the number of houses, and q is the number of zoning requests.
    The next n lines will each contain two integers, x and y (−109 ≤ x, y ≤ 109 ), which are the (x,y) coordinates of a house in your state or province. The address of this house corresponds with the order in the input. The first house has address 1, the second house has address 2, and so on. No two houses will be at the same location.
    The next q lines will contain two integers a and b (1 ≤ a < b ≤ n), which represents a zoning request for houses with addresses in the range [a..b] inclusive.

    输出

    Output q lines. On each line print the answer to one of the zoning requests, in order: the side length of the smallest axis-aligned square that contains all of the points of houses with those addresses, if at most one house can be ignored.

    样例输入

    3 2
    1 0
    0 1
    1000 1
    1 3
    2 3
    

    样例输出

    1
    0

    思路:

    代码如下:

      1 #include <iostream>
      2 #include <bits/stdc++.h>
      3 using namespace std;
      4 const int maxn = 1e5+50;
      5 int n,q,x,y;
      6 int l,r;
      7 struct node
      8 {
      9     int val;
     10     int no;
     11 };
     12 node a[maxn],b[maxn];
     13 node maxx[maxn][25],maxy[maxn][25],minx[maxn][25],miny[maxn][25];
     14 inline int read()                                //读入优化
     15 {
     16     int x=0,f=1;
     17     char c=getchar();
     18     while (!isdigit(c))
     19         f=c=='-'?-1:1,c=getchar();
     20     while (isdigit(c))
     21         x=(x<<1)+(x<<3)+(c^48),c=getchar();
     22     return x*f;
     23 }
     24 node cmp1(node a,node b)
     25 {
     26     if(a.val>b.val)
     27         return a;
     28     else
     29         return b;
     30 }
     31 node cmp2(node a,node b)
     32 {
     33     if(a.val<b.val)
     34         return a;
     35     else
     36         return b;
     37 }
     38 void ST_prework()
     39 {
     40     int t=log(n)/log(2)+1;
     41     for(int j=1;j<t;j++)
     42     {
     43         for(int i=1;i<=n-(1<<j)+1;i++)
     44         {
     45             maxx[i][j]=cmp1(maxx[i][j-1],maxx[i+(1<<(j-1))][j-1]);
     46             maxy[i][j]=cmp1(maxy[i][j-1],maxy[i+(1<<(j-1))][j-1]);
     47             minx[i][j]=cmp2(minx[i][j-1],minx[i+(1<<(j-1))][j-1]);
     48             miny[i][j]=cmp2(miny[i][j-1],miny[i+(1<<(j-1))][j-1]);
     49         }
     50     }
     51 }
     52 int delt(int l,int r,int u)
     53 {
     54     if(l==u)
     55     {
     56         l++;
     57         int k=log(r-l+1)/log(2);
     58         int bx,by,sx,sy;
     59         bx=cmp1(maxx[l][k],maxx[r-(1<<k)+1][k]).val;
     60         sx=cmp2(minx[l][k],minx[r-(1<<k)+1][k]).val;
     61         by=cmp1(maxy[l][k],maxy[r-(1<<k)+1][k]).val;
     62         sy=cmp2(miny[l][k],miny[r-(1<<k)+1][k]).val;
     63         return max(bx-sx,by-sy);
     64     }
     65     if(r==u)
     66     {
     67         r--;
     68         int k=log(r-l+1)/log(2);
     69         int bx,by,sx,sy;
     70         bx=cmp1(maxx[l][k],maxx[r-(1<<k)+1][k]).val;
     71         sx=cmp2(minx[l][k],minx[r-(1<<k)+1][k]).val;
     72         by=cmp1(maxy[l][k],maxy[r-(1<<k)+1][k]).val;
     73         sy=cmp2(miny[l][k],miny[r-(1<<k)+1][k]).val;
     74         return max(bx-sx,by-sy);
     75     }
     76     int tempr,templ;
     77     tempr=u-1,templ=u+1;
     78     int k1=log(tempr-l+1)/log(2);
     79     int k2=log(r-templ+1)/log(2);
     80     int bx,by,sx,sy;
     81     bx=max(cmp1(maxx[l][k1],maxx[tempr-(1<<k1)+1][k1]).val,cmp1(maxx[templ][k2],maxx[r-(1<<k2)+1][k2]).val);
     82     sx=min(cmp2(minx[l][k1],minx[tempr-(1<<k1)+1][k1]).val,cmp2(minx[templ][k2],minx[r-(1<<k2)+1][k2]).val);
     83     by=max(cmp1(maxy[l][k1],maxy[tempr-(1<<k1)+1][k1]).val,cmp1(maxy[templ][k2],maxy[r-(1<<k2)+1][k2]).val);
     84     sy=min(cmp2(miny[l][k1],miny[tempr-(1<<k1)+1][k1]).val,cmp2(miny[templ][k2],miny[r-(1<<k2)+1][k2]).val);
     85     return max(bx-sx,by-sy);
     86 }
     87 int main()
     88 {
     89     n=read(),q=read();
     90     for(int i=1;i<=n;i++)
     91     {
     92         x=read(),y=read();
     93         a[i].val=x,a[i].no=i;
     94         b[i].val=y,b[i].no=i;
     95         maxx[i][0]=a[i],maxy[i][0]=b[i];
     96         minx[i][0]=a[i],miny[i][0]=b[i];
     97     }
     98     ST_prework();
     99     while(q--)
    100     {
    101         l=read(),r=read();
    102         int k=log(r-l+1)/log(2);
    103         int bx,by,sx,sy;
    104         bx=cmp1(maxx[l][k],maxx[r-(1<<k)+1][k]).no;
    105         sx=cmp2(minx[l][k],minx[r-(1<<k)+1][k]).no;
    106         by=cmp1(maxy[l][k],maxy[r-(1<<k)+1][k]).no;
    107         sy=cmp2(miny[l][k],miny[r-(1<<k)+1][k]).no;
    108         int mi=delt(l,r,bx);
    109         mi=min(mi,delt(l,r,sx));
    110         mi=min(mi,delt(l,r,by));
    111         mi=min(mi,delt(l,r,sy));
    112         printf("%d
    ",mi);
    113     }
    114     //cout << "Hello world!" << endl;
    115     return 0;
    116 }
    View Code
     
  • 相关阅读:
    luoguP1160 队列安排 x
    luoguP3366 【模板】最小生成树 x
    cogs服务点设置(不凶,超乖) x
    codevs3269 混合背包 x
    [kuangbin带你飞]专题一 简单搜索 x
    [SWUST1744] 方格取数问题(最大流,最大独立集)
    [SWUST1738] 最小路径覆盖问题(最大流,最小路径覆盖)
    [SWUST1742] 试题库问题(最大流)
    [HDOJ5676]ztr loves lucky numbers(状压枚举,打表,二分)
    [swustoj1739] 魔术球问题 (最大流,最小路径覆盖)
  • 原文地址:https://www.cnblogs.com/SoulSecret/p/9575884.html
Copyright © 2011-2022 走看看