Discretization of POJ 2528 segment tree

Source: Internet
Author: User
Tags sort

"Topic link"
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=14608

[Problem Solving report]
Main topic:
Given the length range (l,r)//l,r<=1e7,
Give the N interval operation, put the Li-ri interval poster//n "=1e5
How many Zhang Hai reports have not been completely covered

The key to this problem is that directly open 1e7 line segment tree will be mle, but we observed that only a total of not more than 1e5 Zhang Hai, if each poster we only take its two endpoints, only the end of its processing, the space complexity will be greatly reduced.
So how to deal with it.
For example, the interval [1,4],[5,6] has four different endpoints, which means that we need to map him to four different positions on the segment tree.
We mark the leaf nodes of the segment tree starting from 1, so the corresponding segment tree interval mapped to is:
[1,4]->[1,2]
[5,6]->[3,4]
A problem arises:
We have labeled [1,10],[1,4],[5,10], the map is: [1,4],[1,2],[3,4] can only see two posters
If [1,10],[1,4],[7,10] is affixed, the map is: [1,4],[1,2],[3,4], only two posters can be seen.
There's something wrong.
The problem is:
If two adjacent segments do not intersect, after discretization, it becomes intersect.
So maintain this nature: if the two vertices are not next to each other, then after we have discretized, they are still not close. The method is to insert a vertex between the two vertices.

When we get here, most of the problems are solved.
There is also a question to consider: How to find the number of posters that are not completely covered.
It is easy to think, when we update, to the interval (l,r) labeled I to indicate the current position of the poster is I, so the last count of how many different I can.
In order not to duplicate the query the same, not completely covered by I, such as: (1,10), (3,4), (7,8) after the update of the tag is 1,2,1,3,1, there will be more than the 1th Zhang Hai newspaper tag, we need to set a separate hash array, if a Zhang Hai newspaper has been counted, The statistics are no longer repeated.

So, the rest is the content of the line tree template.
The rest of the details can be referenced in the code. My Line tree style refers to the training guide of Rujia.

"Reference Code"

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace

Std

const int MAXN=1E5+50;
int n,m,cnt;
int L[MAXN],R[MAXN],VIS[MAXN],X[MAXN];


int cover[maxn*4];
    void uniq (int nn) {m=0;
    X[++M]=X[1];
    for (int i=2; i<=nn; i++) {if (x[i]!=x[i-1]) x[++m]=x[i];
    } sort (x+1,x+1+m);
    int t=m;
    for (int i=1;i<t;i++) {if (x[i]!=x[i+1]-1) x[++m]=x[i]+1;
} sort (x+1,x+1+m);
    } void Build (int O, int L, int R) {if (l==r) cover[o]=-1;
        else {int mid= (L+R)/2;
        Build (O*2+1,mid+1,r);
        Build (O*2,l,mid);
    Cover[o]=-1;
        }} void Pushdown (int O) {if (cover[o]!=-1) {Cover[o*2]=cover[o*2+1]=cover[o];
    Cover[o]=-1; }} void Update (int O, int L, int R, int qL, int qR, int c) {if (ql<=l && r<=qr) {Cove
    R[o]=c;
        } else {pushdown (O); int mid= (L+R)/2;
        if (ql<=mid) update (O*2,l,mid, ql,qr,c);
    if (qr>mid) update (o*2+1, mid+1,r,ql,qr,c);
        }} void query (int O, int L, int R) {if (l==r) {if (Cover[o]!=-1 &&!vis[cover[o])
            {vis[cover[o]]=1;
        cnt++;
        }} else {pushdown (O);
        int mid= (L+R)/2;
        Query (o*2, l,mid);
    Query (O*2+1,MID+1,R);
    }} int main () {//freopen ("2528", "R", stdin); int T;
    cin>>t;
        while (t--) {scanf ("%d", &n);
        int nn=0;
             for (int i=1; i<=n; i++) {scanf ("%d%d", &l[i],&r[i]); X[++nn]=l[i];
        X[++nn]=r[i];
        } sort (X+1,X+1+NN);
        Uniq (NN);
        Build (1,1,M);
            for (int i=1; i<=n; i++) {int left=lower_bound (x+1,x+1+m,l[i])-X;
            int Right=lower_bound (X+1,x+1+m,r[i])-X; Update (1,1,M,LEFT,RIGHT,I);
        } memset (vis,0,sizeof vis);
        cnt=0;
        Query (1,1,M);
    printf ("%d\n", CNT);
} return 0; }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.