POJ 2528 (segment tree + discretization)

Source: Internet
Author: User

Test instructions: Put a poster on the wall, give each of the intervals posted, ask the last to see a few posters. idea: Segment tree, but the given interval length is 1000000, must be discretized processing. discretization: The order of all points;Remove duplicate points, add a number between two numbers that are greater than one (avoid errors), sort, and find the position of the left and right intervals (i.e., the values corresponding to the discretization)segment tree: Can be solved with a template (a variable name is wrong, debugging a noon, whining ~ ~ Temperature failure)
AC Code:
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm>using namespace STD; #define Lson l,mid,rt<<1#define Rson mid+1,r,rt<<1|1#define Maxx 10005int X[maxx<<2],col[maxx  <<4],li[maxx],ri[maxx];bool flag[maxx];int n,m,ans;int bsearch (int key) {int left=0,right=m-1,mid;   while (left<=right) {mid= (left+right) >>1;   if (X[mid]==key) return mid;   if (X[mid]<key) left=mid+1;  else right=mid-1; } return-1;}    void pushdown (int rt) {if (col[rt]!=-1) {col[rt<<1]=col[rt];    col[rt<<1|1]=col[rt];  Col[rt]=-1;    }}void Update (int left,int right,int c,int l,int r,int RT) {if (left<=l&&right>=r) {col[rt]=c;  Return  } pushdown (RT);  int mid= (L+R) >>1;  if (left<=mid) update (Left,right,c,lson); if (right>mid) update (Left,right,c,rson);}     void query (int l,int r,int RT) {if (col[rt]!=-1) {if (!flag[col[rt]]) ans++;     Flag[col[rt]]=1;   Return } if (L==R) return;   int mid= (L+R) >>1;   Query (Lson); Query (Rson);}  int main () {int t;  scanf ("%d", &t);    while (t--) {Ans=0;memset (col,-1,sizeof (col)); memset (flag,0,sizeof (flag));    scanf ("%d", &n);    for (int i=0;i<n;i++) scanf ("%d%d", &li[i],&ri[i]); int nn=0;     for (int i=0;i<n;i++) {x[nn++]=li[i];    X[nn++]=ri[i];    } sort (x,x+nn); m=1;   for (int i=1;i<nn;i++) {if (x[i]!=x[i-1]) x[m++]=x[i];  } for (int i=m-1;i>=0;i--) {if (x[i]!= (x[i-1]+1)) x[m++]=x[i-1]+1;  } sort (x,x+m);   for (int i=0;i<n;++i) {int l=bsearch (li[i]);   int R=bsearch (ri[i]); Update (l,r,i,0,m-1,1);   } query (0,m-1,1);  printf ("%d\n", ans); } return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2528 (segment tree + 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.