Mayor's posters POJ - 2528 (discretization + segment update + interval statistics)

Mayor's posters

POJ - 2528

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules:
  • Every candidate can place exactly one poster on the wall.
  • All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown).
  • The wall is divided into segments and the width of each segment is one byte.
  • Each poster must completely cover a contiguous number of wall segments.

They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections.
Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall.
Input
The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers l i and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i , l i +1 ,... , ri.
Output
For each input data set print the number of visible posters after all the posters are placed.

The picture below illustrates the case of the sample input.
Sample Input
1
5
1 4
2 6
8 10
3 4
7 10
Sample Output
4

I saw that the blog is very well written on the Internet and directly stuck it.

Click to open the link

The meaning of the question: first enter the number of cases, and enter n for each case, indicating that there are n posters below, each line is the left and right coordinates of the poster, and the color of the i-th poster is i. The length of one wall is fixed at 10000000, how many colors can be seen after these posters are pasted

The difficulty of this problem is actually how to discretize (that is, mapping, after the mapping is a simple update of the entire segment of the line segment tree, and finally the interval query).

int s[MAXN][2]; Save the original data, [0] is the starting point coordinate, [1] is the end point coordinate, then a total of 2*n endpoints are generated

struct point
{
int a,n,f; //Endpoint coordinates, which line segment belongs to, starting point or end point
}p[2*MAXN];

So put the 2*n endpoints into the p array one by one, and record which line segment (n this field) this endpoint comes from, and whether it is the starting point or the end point in this line segment (f this field)

Then sort the p array by the size of the endpoint coordinates (a field)

 

Next is the mapping, for example the sorted result is

10,21,38,40,40,59

maps to

1,2,3,4,4,5

That is to say, it is mapped according to the size of the number, and it can also be found that the last 5 actually represents how many different numbers there are.

The result of this mapping must contain all integers in [1,m]. When we want to build a segment tree, the length of the total interval is [1,m]

m can go up to 80000 (60000 is also fine), how did 80000 come from, it is 2*10000*4

Because the number of posters is at most 10,000, and there are 20,000 endpoints. In extreme cases, these 20,000 points are different. If they are mapped in the past, it is [1, 20,000], that is, the maximum m can be m

Recall that in general, if the total interval of the line segment tree is [1,m], the array we open up the line segment tree is generally 4*m (in fact, it is enough to open 3*m), so this is the reason why 80000

 

Speaking of which, when we scan the p array, every time we get a point, we first map its coordinates (whether it is different from the coordinates of the endpoint in front of it, if it is different, it is a new point, see the code for details), and then look at it Which line segment is it from (the same line segment after mapping), and then see whether it is the starting point or the end point in the original line segment, and then record the starting point and end point of the current line segment accordingly

 

The last mapped line segment information is all in the following array

struct interval //Discrete line segment
{
int l,r,col;
}in[MAXN];

 

Once you get it, you can build a tree, update the entire segment, and query


code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 10010
using namespace std;
bool used[MAXN];//Record which colors have been counted when querying
int s[MAXN][2],N;//s array records the original array
struct point{
    int a,n,f;//Endpoint coordinates, which line segment belongs to, starting point or end point
}p[2*MAXN];
struct interval{//The line segment after discretization
    int l,r,col;
}in[MAXN];
struct segment{//Line segment tree node
    int l,r,col,val;//val indicates whether this interval has been completely covered by a poster
}Tree[MAXN*8];

int query(int l,int r,int rt){
    int col = Tree[rt].col;
    if(Tree[rt].val){
        if(!used[col]){
            used[col] = 1;
            return 1;
        }
        else
            return 0;
    }
    int mid = Tree[rt].l + Tree[rt].r >> 1;
    return query(l,mid,rt<<1) + query(mid+1,r,rt<<1|1);
}

void update(int l,int r,int col,int rt){
    if(Tree[rt].val && Tree[rt].col == col)
        return;
    if(Tree[rt].l == l && Tree[rt].r == r){
        Tree[rt].col = col;
        Tree[rt].val = 1;
        return;
    }
    if(Tree[rt].val){
        Tree[rt<<1].val = Tree[rt<<1|1].val = Tree[rt].val;
        Tree[rt<<1].col = Tree[rt<<1|1].col = Tree[rt].col;
        Tree[rt].val = 0;
    }
    int mid = Tree[rt].l + Tree[rt].r >> 1;
    if(l > mid)
        update(l,r,col,rt<<1|1);
    else if(r <= mid)
        update(l,r,col,rt<<1);
    else{
        update(l,mid,col,rt<<1);
        update(mid+1,r,col,rt<<1|1);
    }
}

void build(int l,int r,int rt){
    Tree[rt].l = l;
    Tree[rt].r = r;
    Tree[rt].val = 0;
    Tree[rt].col = 0;
    if(l == r) return;
    int mid = l + r >> 1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
}

bool cmp(point x,point y){
    return x.a < y.a;
}

int main(){
    int Case;
    scanf("%d",&Cas);
    while(Cas--){
        scanf("%d",&N);
        // discretize
        for(int i = 1; i <= N; i++){
            scanf("%d%d",&s[i][0],&s[i][1]);
            p[2*i-1].a = s[i][0];
            p[2*i-1].n = i;
            p[2*i-1].f = 0;
            p[2*i].a = s[i][1];
            p[2*i].n = i;
            p[2*i].f = 1;
        }
        sort(p+1,p+2*N+1,cmp);
        p[0].a = p[1].a;
        int m = 1,n,f;
        for(int i = 1; i <= 2*N; i++){
            n = p[i].n;
            f = p[i].f;
            if(p[i].a != p[i-1].a) m++;
            if(!f){
                in[n].l = m;
                in[n].col = n;
            }
            else{
                in[n].r = m;
                in[n].col = n;
            }
        }
        // end of discretization
        build(1,m,1);
        for(int i = 1; i <= N; i++){
            update(in[i].l,in[i].r,in[i].col,1);
        }
        memset(used,0,sizeof(used));
        printf("%d\n",query(1,m,1));
    }
    return 0;
}


Guess you like

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