hihocoder#1079 (segment tree + coordinate discretization)

Source: Internet
Author: User

Time limit: 10000ms single point time limit: 1000ms memory limit: 256MB description

Small hi and small ho after returning home, back to the 7-night 5 student life, of course, they are still learning all kinds of algorithms ~

This day little hi and Small Ho's school held a Community culture festival, the major societies in the publicity bar posted posters, but stickers to paste, some posters will be blocked by other community posters. Seeing this scene, little hi raises the question-how many posters can be seen at the end?

So small ho shoulder the responsibility to solve this problem: because the propaganda column and the height of the poster is the same, so the propaganda column can be regarded as the length of an interval of L, and the n Zhang Hai newspaper in order sequentially affixed to the propaganda bar, where I poster affixed to the scope of a section can be used in a section [A_i, B_i], where a _i, b_i are integers that belong to [0, L], and a poster can be seen when and only if there is a part of the length greater than 0 that is not obscured by the poster that was later posted. So the question is: how many posters can be seen?

Tip One: Correct understanding of the amount of information

Hint two: small Hi big lecture Hall the node meaning of the segment tree

Input

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

The 1th behavior of each set of test data is two integers n and l, respectively, representing the total number of posters posted and the width of the publicity bar.

Each group of test data in line 2-n+1, according to the order of the paste, each line describes a poster, wherein the act of i+1 two integers a_i, b_i, indicating that the section I Zhang Hai newspaper posted is [A_i, B_i].

For 100% of data, meet N<=10^5,l<=10^9,0<=a_i<b_i<=l.

Output

For each set of test data, output an integer ans, indicating how many Zhang Hai can be seen in total.

Sample input
5 104 100 21 65 93 4
Sample output
5
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intmaxn=200005;structnode{intColor//0 means no poster, 1 for a mixed poster    intL,r;} A[MAXN*3];structquery{intL,r;} QER[MAXN];intn,l;inthax[maxn],cnt;voidBuildintRtintLintR) {A[RT].L=l; A[RT].R=R; if(l==r) {A[rt].color=0; return ; }    intMid= (l+r) >>1; Build (Rt<<1, L,mid); Build (Rt<<1)|1, mid+1, R); if(a[rt<<1].color==a[(rt<<1)|1].color) a[rt].color=a[rt<<1].color; Elsea[rt].color=-1;}voidUpdateintRtintLintRintval) {    if(a[rt].l==l&&a[rt].r==r) {A[rt].color=Val; return ; }    if(a[rt].color!=0&&a[rt].color!=-1) {A[rt<<1].color=A[rt].color; a[(Rt<<1)|1].color=A[rt].color; }    intMid= (A[RT].L+A[RT].R) >>1; if(r<=mid) {update (RT<<1, L,r,val); }    Else if(mid<l) {update (RT<<1)|1, L,r,val); }    Else{update (RT<<1, L,mid,val); Update (RT<<1)|1, mid+1, R,val); }    if(a[rt<<1].color==a[(rt<<1)|1].color) a[rt].color=a[rt<<1].color; Elsea[rt].color=-1;}intVis[maxn],res;voidQueryintRtintLintR) {    if(a[rt].color==0)    {        return ; }    if(a[rt].l==l&&a[rt].r==r) {if(a[rt].color!=-1)        {            if(!Vis[a[rt].color]) {Vis[a[rt].color]=1; Res++; }            return ; }    }    if(a[rt].color!=0&&a[rt].color!=-1) {A[rt<<1].color=A[rt].color; a[(Rt<<1)|1].color=A[rt].color; }    intMid= (A[RT].L+A[RT].R) >>1; Query (RT<<1, L,mid); Query (RT<<1)|1, mid+1, R);}intMain () { while(SCANF ("%d%d", &n,&l)! =EOF) {CNT=0; Res=0; memset (Vis,0,sizeof(VIS));  for(intI=0; i<n;i++) {scanf ("%d%d",&qer[i].l,&QER[I].R); Hax[cnt++]=QER[I].L; Hax[cnt++]=QER[I].R; } sort (Hax,hax+CNT); CNT=unique (HAX,HAX+CNT)-Hax; Build (1,1, CNT); intCol=1;  for(intI=0; i<n;i++)        {            intL=lower_bound (HAX,HAX+CNT,QER[I].L)-hax+1; intR=lower_bound (HAX,HAX+CNT,QER[I].R)-hax;//no need to add 1Update1, L,r,col); Col++; } query (1,1, CNT); printf ("%d\n", RES); }        return 0;}

hihocoder#1079 (segment tree + coordinate discretization)

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.