Learn the discretization of coordinates every day (4. 24)

Learning a little every day means learning a little and losing a little every day.

And then the two words live and die every day, and I still insist on learning every day, but the things I learn every day are not enough to summarize in a single article.

In fact, the second one originally planned to write LCA, but LCA has not yet learned to understand, and I just talked about the discretization of coordinates again today, so let's summarize.


Coordinate discretization is suitable for a larger graph, and there are many useless parts. At this time, we use the discretization method to reduce them and keep the original relationship unchanged.

First, an example of one-dimensional discretization is given. Given a number line N (N<1e5, val(N)<1e15) points, let's say 1, 10, 500, 3700, 3701, 4200, 80000, 10000000000. If we open a Boolean array and use its true or false to indicate whether this point is the point we want, the array to be opened cannot be so large. So what do we generally do?

Open an array or vector, and then store the N points in the order of size. We basically express the size relationship of these numbers. At this time, let's change the way of understanding, do we put the N values? The large points are scattered to the first N points on the number line, and correspond one-to-one according to the size relationship.

Some abstract, okay, let's give a two-dimensional visual example


The original title of this picture is to find the number of white four-connected blocks, then we only need to record the approximate shape of the original picture, and we do not need to know the size of each block. Then let's compare the one-dimensional discretization, "We don't need all the points on the number line, we only need the given N points", then in such a two-dimensional graph, we don't need all the grids, we only need The approximate shape of the white block divided by the black line, that is, we only need to keep the black line and one line before and after the black line. Then it is transformed into that we keep the row and column where the endpoint is located and the upper, lower, left, and right rows and columns. Then we get the second graph after our discretization. If I still don't know why, and I don't know how I should explain it, so be it. I understand just fine.

Then give a code for coordinate discretization, the code is to discretize N points in two-dimensional space.

const int maxn = 205;
int x[maxn], y[maxn]; //Used to store the horizontal and vertical coordinates of N points

int LSH(int *x, int N, int R) //N points x[] store abscissa information The original image boundary is [1, R]
{

    vector<int > xs;

    for(int i=0; i<N; i++)
        for(int d=-1; d<=1; d++)
            if(x[i]+d>=1 && x[i]+d<=R) //Reserved in the original image range, the line where the point is located and the information of the upper and lower lines (the columns are also the same)
                xs.push_back(x[i]+d);

    sort(xs.begin(), xs.end()); //Sort xs to prepare for unique later
    xs.erase(unique(xs.begin(), xs.end()), xs.end()); //First de-reorder xs, and then delete the repeated part

    for(int i=0; i<N; i++)
        x[i] = find(xs.begin(), xs.end(), x[i]) - xs.begin(); //find returns an iterator, and what is obtained after subtraction is discretized coordinate
                                                                //That is: convert the original coordinates into subscript information and store them, preserving the size relationship between the original coordinates

    return xs.size(); //returns the boundary of the graph after discretization
}

From here, we can see that the discretization of coordinates is actually that we process the coordinate information we need to keep (the abscissa and the surrounding two lines in this example), so that the original size relationship is retained in the coordinates after discretization, and omitted All unnecessary coordinate information.

Another example of weighted discretization in two-dimensional space is given. Weighted discretization can record the size relationship of each part of the original image, that is, the corresponding point after discretization is equivalent to the "condensation" of the original number of points.

int LSH (int * x, int * xx, int R)
{
    vector<int >xs;
    for(int i=0; i<n; i++)
        for(int d=-1; d<=1; d++)
            if(x[i]+d>0 && x[i]+d<=R)
                xs.push_back(x[i]+d); ///Read the corresponding coordinates and left and right into xs, that is, xs stores all the coordinate values ​​we need to keep

    sort(xs.begin(), xs.end()); ///You can only use unique after sort
    xs.erase(unique(xs.begin(), xs.end()), xs.end()); ///unique puts the repeated elements in the interval into the last part of the array, and returns the first repeated element pointer (iterator)
                                                                        ///Then use erase to delete the repeated part, that is, all the coordinates to be used are stored in xs in ascending order, and the subscript of xs is the corresponding coordinate after discretization
    for(int i=0; i<n; i++){
        int sign = find(xs.begin(), xs.end(), x[i]) - xs.begin() + 1; ///New subscript [1, xs.size()]
        x[i] = sign; ///x[i] corresponds to the new subscript
    }

    for(int i=0; i<xs.size(); i++){ ///The original value corresponding to the new subscript is used to judge the weight corresponding to the node after discretization
        xx[i+1] = xs[i];
    }

    xx[0] = 0; ///The minimum boundary is 0
    xx[xs.size()+1] = R+1; ///Maximum line R+1 line
    return xs.size();
}

The difference from the previous example is that in this example, we store the discretized line information in the xx[] array corresponding to how many lines in the original image, then we can use the original number of lines represented by two adjacent lines. To find out how many lines are omitted between two lines , then for any grid, we can correspondingly get how many grids it represents in the original image.

LL x1 = nowx==1?xx[nowx+1]-xx[nowx-1]-1:xx[nowx+1]-xx[nowx]; /// Give the omitted part to a smaller line ( column) so special treatment is required for the first row (column)
LL y1 = new == 1? Yy [new + 1] -yy [new-1] -1: yy [new + 1] -yy [new];
cnt = x1*y1; ///cnt represents the row and column product represented by the current cell, that is, the number of grid points represented in the original image

In this example, all the parts that we artificially omitted are given to the grid points on the left, then the grid points omitted in the most front position are given to the parts outside the figure, so it needs to be specially judged whether it is the first row or the first column, and then Calculate again.

The above is what I have learned about coordinate discretization. If there are more advanced applications, the ghost knows if I will update it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324954852&siteId=291194637