POJ 2528 discretization + segment Tree

Source: Internet
Author: User

This essay me WA, and I can't find my place wrong.

Test instructions: There was a wall, a newspaper on the wall, and finally a few papers to see

In fact, it is very easy to segment the tree, the place is not easy to discretization

Discretization to save all the values that need to be used, after sorting, mapping to 1~n, so the complexity is much smaller a lot of the problem is that each number actually represents a unit length (and a point), so that ordinary discretization will cause a lot of errors (including my previous code, the data is very weak) The following two simple examples should be able to reflect the defects of common discretization:
1-10 1-4 5-10
1-10 1-4 6-10 to solve this flaw, we can add some processing to the sorted array, for example [1,2,6,10] If the adjacent number spacing is greater than 1, add any number, such as add to [1,2,3,6,7,10], and then do the line segment tree.

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm>using namespace Std;int x[21111<<3];int tree[21111<<4];int all;int mark[21111<<4];int Fun (int key,int l,int r) {int Mid= (l+r)/2;while (l<=r) {mid= (l+r)/2; if (Key==x[mid]) return mid;if (Key<x[mid]) r=mid-1;elsel=mid+1;} return-1;} int insert (int l,int r,int color,int l,int r,int index) {if (l<=l&&r>=r) {Tree[index]=color;return 0;} if (tree[index]>0) {tree[index*2]=tree[index];tree[index*2+1]=tree[index];tree[index]=0;} int mid= (r+l)/2;if (l<=mid) {insert (l,mid,color,l,r,index*2);} if (r>mid) {insert (mid+1,r,color,l,r,index*2+1);}} int query (int l,int R,int index) {if (l==r) {if (mark[tree[index]]==0) {mark[tree[index]]=1;all++;} return 0;} if (tree[index]>0) {tree[index*2]=tree[index*2+1]=tree[index];tree[index]=0;} int mid= (L+R)/2;query (l,mid,index*2); query (mid+1,r,index*2+1);} int main () {int n,t;int nn;int i;scanf ("%d", &t); int A[21111*4],b[21111*4];while (t--) {MeMset (Mark,0,sizeof (Mark)), memset (tree,0,sizeof (tree)), scanf ("%d", &n), Nn=1;for (i=1;i<=n;i++) {scanf ("%d%d ", &a[i],&b[i]); x[nn++]=a[i];x[nn++]=b[i];} Sort (x+1,x+nn); int m=2;for (i=2;i<nn;i++) {if (x[i]!=x[i-1]) x[m++]=x[i];} for (i=m;i>0;i--) {if (x[i]-x[i-1]>1) x[m++]=x[i]-1;} Sort (x+1,x+m); M--;for (i=1;i<=n;i++) {int temp1=fun (a[i],1,m); int Temp2=fun (b[i],1,m); Insert (1,M,I,TEMP1,TEMP2, 1);} all=0;//printf ("\nn"), query (1,m,1);p rintf ("%d\n", All);} return 0;}


POJ 2528 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.