HDU-time challenge tree Array

Source: Internet
Author: User

This question seems to be an original question of poj... first, we can determine that if a line segment is included by another line segment, the left endpoint of the line segment containing it must be smaller than or equal to this line segment. Therefore, we sort the left endpoint from small to large, and the left endpoint is the same as the right endpoint from large to small, in this way, all the line segments that contain the I-th line segment must be updated before.

Then we directly require that the right section of the preceding line segment be greater than the line segment. Because the tree array is used, we use a number to subtract the right endpoint, so the right endpoint is closer to the left, so we can make the tree-like array. When the attributes of the two line segments are the same, we need to assign the previous answer to the next line segment and then update it.

The Code is as follows:

#include <cstring>#include <cstdio>#include <algorithm>#include <cstdlib>#include <map>using namespace std;int N, c[400005], ret[200005], idx;map<int,int>mp;struct Node{    int l, r, NO;    bool operator < (Node temp) const    {        if (l != temp.l) return l < temp.l;        return r > temp.r;    }}e[200005];struct High{    int h;    bool operator < (High temp) const    {        return h < temp.h;    }     bool operator == (High temp) const    {        return h == temp.h;    }}H[400005];int lowbit(int x){    return x & -x;}void add(int x, int val){    for (int i = x; i <= 400000; i += lowbit(i)) {        c[i] += val;    }}int sum(int x){    int ret = 0;    for (int i = x; i > 0; i -= lowbit(i)) {        ret += c[i];    }    return ret;}int main(){    while (scanf("%d", &N) == 1) {        memset(c, 0, sizeof (c));        memset(ret, 0, sizeof (ret));        mp.clear();        idx = -1;        for (int i = 0; i < N; ++i) {            scanf("%d %d", &e[i].l, &e[i].r);            e[i].NO = i;            H[++idx].h = e[i].l, H[++idx].h = e[i].r;        }        sort(H, H+idx+1);        idx = unique(H, H+idx+1)-H;        for (int i = 0; i < idx; ++i) {            mp[H[i].h] = idx - i;        }        sort(e, e + N);        for (int i = 0; i < N; ++i) {            int pos = mp[e[i].r];            if (i > 0 && e[i].l == e[i-1].l) {                if (e[i].r == e[i-1].r) {                    ret[e[i].NO] = ret[e[i-1].NO];                }                else {                    ret[e[i].NO] = sum(pos);                }            }            else if (i > 0 && e[i].l > e[i-1].l) {                ret[e[i].NO] = sum(pos);            }            else {                ret[e[i].NO] = 0;            }            add( pos, 1 );        }        for (int i = 0; i < N; ++i) {            if (i == 0) {                printf("%d", ret[i]);            }            else {                printf(" %d", ret[i]);            }        }        puts("");    }    return 0;}

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.