poj2528 Segment Tree Problem solving report, discretization + segment Tree

Source: Internet
Author: User

Topic website: http://poj.org/problem?id=2528

Test instructions

N (n<=10000) individuals posted posters in turn, giving each poster the range Li,ri (1<=li<=ri<=10000000).

Find out how many posters you can see at the end.

Input:

1 5 1 4 2 6 8 10 3 4 7 10
This problem with conventional thinking problem solving must tle,l,r too big;

The popular point says that discretization is the compression interval, so that the original long interval is mapped to a new short interval, but the coverage relationship is not changed before and after the interval compression. As an example:

There is a 1 to 10 axis (length 9), given a 4 interval [2,4] [3,6] [8,10] [6,9], the covering relationship is the latter covering the former, each interval is dyed 1 2 3 4.

Now we take the 8 endpoints of these 4 intervals, 2 4 3 6 8 10 6 9

Then delete the same endpoint, where the same endpoint is 6, leaving 2 4 3 6 8 10 9

Sort it in ascending order, get 2 3 4 6 8 9 10

And then build the mapping

2 3 4 6 8 9 10

↓↓↓↓↓↓↓

1 2 3 4 5 6 7

The new 4 range is [1,3] [2,4] [5,7] [4,6], and the coverage relationship has not been changed. The new axis is 1 to 7, that is, the length of the original axis is compressed from 9 to 6, it is obvious that the construction [1,7] segment tree is more space-saving and faster to search than the segment tree that constructs [1,10], but the results of the solution are consistent.

One thing to be aware of when discretization is that you must first remove the same endpoint before sorting, which reduces the number of elements involved in sorting and saves time.

1#include <iostream>2#include <stdio.h>3#include <algorithm>4#include <string.h>5 using namespacestd;6 #defineN 101007 #defineM 100000008 BOOLvis[2*n];//Mark appeared poster9 intx[2*N];Ten structT One { A      intNode,add,l,r; -}tree[m*4]; - voidCreat (intLintRintk)//achievements the { -Tree[k].node=0; -Tree[k].l=l; -Tree[k].r=R; +Tree[k].add=0; -      if(l==R) +          return; A      intMid= (l+r) >>1; atCreat (l,mid,k<<1); -Creat (mid+1,r,k<<1|1); - } - voidPushdown (intKintcolor)//delay Mark - { -      intx=k<<1; inTree[x].add=1; -tree[x+1].add=1; toTree[x].node=color; +tree[x+1].node=color; -Tree[k].add=0; theTree[k].node=0; * } $ voidSearch (intLintRintColorintk)//update segment TreePanax Notoginseng { -      if(r<tree[k].l| | L>TREE[K].R) the          return ; +      if(l<=tree[k].l&&r>=TREE[K].R) A      { theTree[k].node=color; +Tree[k].add=1; -          return ; $      } $      if(Tree[k].add) - pushdown (k,tree[k].node); -      intMid= (TREE[K].L+TREE[K].R) >>1; the      if(r<=mid) -Search (l,r,color,k<<1);Wuyi      Else if(l>mid) theSearch (l,r,color,k<<1|1); -         Else Wu         { -Search (l,mid,color,k<<1); AboutSearch (mid+1,r,color,k<<1|1); $         } - } - intans; - voidQintLintRintk)//find areas of different colors A { +      if(Tree[k].add) the      { -          if(!Vis[tree[k].node]) $         { theans++; thevis[tree[k].node]=1; the          } the          return ; -      } in      intMid= (l+r) >>1; theQ (l,mid,k<<1); theQ (mid+1,r,k<<1|1); About } the intSintLintRintk)//Two-point search the { the     intmid; +       while(l<=R) -      { theMid= (l+r) >>1;Bayi          if(K<x[mid]) r=mid-1; the          Else if(K>x[mid]) l=mid+1; the          Else returnmid; -      } -      return-1; the } the intMain () the { the     intt,n,i,j; -     intL[n],r[n]; thescanf"%d",&t); the      while(t--) the     {94j=0; thememset (Vis,0,sizeof(Vis)); thescanf"%d",&n); the           for(i=0; i<n;i++)98          { Aboutscanf"%d%d",&l[i],&r[i]); -x[j++]=L[i];101x[j++]=R[i];102          }103Sort (x,x+j);104          intm=1; the           for(i=0; i<j-1; i++)106          {107              if(x[i]==x[i+1])108             {109m--; the             }111             Else the             {113x[i+m]=x[i+1]; the             } the          } thej=j+m-1;117Sort (x,x+j);118          /*For (i=0;i<n;i++)119          { - printf ("%d%d", S (0,j-1,l[i]) +1,s (0,j-1,r[i]) +1);121 cout<<endl;122          }*/123Creat (1J1);124           for(i=0; i<n;i++) the          {126Search (S (0, J-1, L[i]) +1, S (0, J-1, R[i]) +1, i+1,1);127          } -ans=0;129Q1J1); theprintf"%d\n", ans);131     } the     return 0;133}

poj2528 Segment Tree Problem solving report, discretization + segment Tree

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.