Hihocoder #1079: discretization-line segment tree Application

Source: Internet
Author: User
#1079: discretization time limit: milliseconds ms single-point time limit: Ms memory limit: MB description

After returning to China, Xiao hi and Xiao ho started a new life for 7 to 5 students. Of course, they are still learning various algorithms ~

On this day, Xiao hi and Xiao ho held community culture festivals in their schools. All major community organizations posted posters on the billboards, but pasted them, some posters will be blocked by posters of other communities. When we saw this scenario, Xiao Hi had such a question: how many posters can be seen at the end?

Therefore, Xiao ho shoulder the responsibility to solve this problem: Because the height of the billboards and posters is the same, the billboards can be considered as one segment with a length of L, N posters are pasted on the billboards in sequence. The range of the I posters can be represented by [a_ I, B _ I], of which a_ I, B _ I is an integer belonging to [0, L], and a poster can be seen only when a part with a length greater than 0 is not blocked by a later poster. The question is: How many posters can be seen?

Tip 1: Correct understanding of information

 

Tip 2: Significance of the node of the Line Segment tree in xiaohi lecture hall

 

Input

Each test point (input file) has only one set of test data.

Two integers (N and L) are the 1st actions of each group of test data, indicating the total number of posters and the width of the billboards.

The row 2-N + 1 of each group of test data describes a poster in the order in which it is pasted. The row I + 1 contains two integers a_ I, B _ I, the range of the I-th poster is [a_ I, B _ I].

For 100% of the data, n <= 10 ^ 5, L <= 10 ^ 9, 0 <= a_ I <B _ I <= L.

Output

For each group of test data, an integer ANS is output, indicating the total number of posters to be viewed.

Sample Input
5 104 100 21 65 93 4
Sample output
5
Notes
A section of a continuous line segment tree is decomposed into [l, m], [M + 1, R]
#include <iostream>#include<cstdio>#include<ctime>#include<cstring>#include<set>#include<map>using namespace std;const int max_N = 100000;int a[max_N+1], b[max_N+1];int value[max_N<<2] = {0};int N, L, left_bound = 1, right_bound = 0;void read_and_compress(){    set<int> s;    map<int, int> m;    scanf("%d %d", &N, &L);    for(int i = 1; i <= N; ++i){        scanf("%d %d", &a[i], &b[i]);        s.insert(a[i]);        s.insert(b[i]);    }    right_bound = 0;    for(set<int>::iterator it = s.begin(); it != s.end(); ++it){        m[*it] = ++right_bound;    }    for(int i = 1; i <= N; ++i){        a[i] = m[a[i]];        b[i] = m[b[i]];    }}void push_down(int value[], int idx){    if(value[idx] > 0){        value[idx<<1] = value[idx];        value[(idx<<1)|1] = value[idx];        value[idx] = 0;    }}void update(int value[], int idx, int left, int right, int l, int r, int v){    if(l >= r || r <= left || l >= right || left >= right) return;    if(l <= left && right <= r){        value[idx] = v;    }else{        push_down(value, idx);        int mid = (left + right) >> 1;        if(l < mid) update(value, idx<<1, left, mid, l, r, v);        if(mid < r) update(value, (idx<<1)|1, mid, right, l, r, v);    }}void query(int value[], int idx, int left, int right, int l, int r, set<int> &s){    if(l >= r || left >= right || r <= left || l >= right) return;    if(value[idx] > 0){        s.insert(value[idx]);    }else if(left + 1 < right){        int mid = (left + right) >> 1;        query(value, idx<<1, left, mid, l, r, s);        query(value, (idx<<1)|1, mid, right, l, r, s);    }}void print_tree(int[], int, int, int);int main(){    read_and_compress();    for(int i = 1; i <= N; ++i){        update(value, 1, left_bound, right_bound, a[i], b[i], i);    }    set<int> s;    query(value, 1, left_bound, right_bound, left_bound, right_bound, s);    int ans = s.size();    printf("%d\n", ans);    return 0;}

 

Hihocoder #1079: discretization-line segment tree Application

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.