pku2528----interval staining to segment update

Source: Internet
Author: User

creat by Guo Chai March 29, 2012 10:16:59


Interval staining is more difficult than interval staining.

Using interval dyeing to update, hash, discretization, egg pain

Test instructions: Posters on the wall, posters can cover each other, ask to see a few posters at the end
Idea: This data range is very large, directly engage in Timeout + hyper-memory, need discretization:
Discretization is simply to use the values we need , such as interval [1000,2000],[1990,2012] We don't use [-∞,999][1001,1989][1991,1999][2001,2011][2013, +∞] These values, so I just need to 1000,1990,2000,2012 enough, to map it to 0,1,2,3, the complexity is greatly lowered
So discretization to save all the values that need to be used, after sorting, mapping to 1~n, so the complexity will be much smaller
The difficulty of this problem is that each number actually represents a unit length (and a point), so that ordinary discretization will cause many errors POJ the data is very weak)
The following two simple examples should be presented to illustrate the drawbacks 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 digit spacing is greater than 1, add any number to it, such as add [1,2,3,6,7,10], and then do the line segment tree.

I still don't know why this is happening.

————————————————————————————————————————————————————————
Assuming that the given interval is [1,3],[6,1000], we can do the following discrete
Discrete pre-coordinates: 1 3 6 1000
Post-Discrete coordinates: 1 2 3 4
So we reduced the unnecessary search process and memory space. There is a small skill in the achievement, because each node that I build is open interval to closed interval, namely [A, b]. So when I read the data, I can add one to the value of B, so it's good to deal with the range of [a,a] equal values (that is, the processing of a point) that may appear in the topic.

Depending on this, you can change the cout of the program below, without adding 1, and the interval right boundary plus 1 on the input value.

A moment from http://chenjiapeng.blogbus.com/logs/72973396.html

Now let's make a little summary:

1. The segment tree does not have a fixed template, but it is thought that it is generally possible to discretization.

2. Divided into achievements, dyeing, judging statistics 3 steps, perhaps other roughly also 3 similar steps.

3. If there is a single point, the right endpoint +1 can be processed when the interval is built.

There are a lot of not very clear, and so on after the specific study to explore it.

/* Line Tree */
#include <iostream>
#include <algorithm>
using namespace Std;
const int max=20010;
struct point//Records n intervals
{
int l;
int R;
}a[max];
struct Node
{
int l;
int R;
int mid;
int color;
}TREE[MAX*4];
int t,n;
int hash[max*2];//Hash is a kind of attitude = =
BOOL used[max];//The interval of the last statistic single color
void make (int l,int r,int num)//Build Segment Tree
{
TREE[NUM].L = l;
TREE[NUM].R = R;
Tree[num].mid= (L+R)/2;
tree[num].color=0;
if (l+1!=r)//Not leaf interval
{
Make (l,tree[num].mid,num*2);
Make (tree[num].mid,r,num*2+1);
Return
}
}
/* Dye function */
void Insert (int num,int l,int r,int c)//node number num. The interval to be dyed
{//Left and right end of l,r and color C
if (tree[num].color!=c)//Interval color is not to dye the color C
{//If C, the description is already full C
if (tree[num].l==l&&tree[num].r==r)//interval is completely covered, the interval is infected with C
{
Tree[num].color=c;
Return
}

Here are the cases where the interval is not fully covered

if (tree[num].color>=0)//interval is not shaded or above a color (this color is full)
{//Then its child nodes are also this color
Tree[2*num].color=tree[num].color;
Tree[2*num+1].color=tree[num].color;
tree[num].color=-1;//because of the color at first (or not)
The shaded C occupies only part of the interval, so it is the noise
}
if (r<=tree[num].mid)//staining interval in the left sub-range of the interval
Insert (2*NUM,L,R,C);
else if (l>=tree[num].mid)//dyeing interval in the right sub-range of the interval
Insert (2*NUM+1,L,R,C);
Interval of else//staining interval across interval
{
Insert (NUM*2,L,TREE[NUM].MID,C);
Insert (NUM*2+1,TREE[NUM].MID,R,C);
}
}
}
/* Statistical functions
Statistics the last color range */
void count (int num)
{
if (tree[num].color>0)
{
Used[tree[num].color]=true;
return;
}
if (TREE[NUM].L+1!=TREE[NUM].R)
{
Count (2*num);
Count (2*num+1);
}
}
/* Lookup function
Val is the value before the discretization.
The function returns the number of the interval after it has been discretized */
int b_search (int l,int r,int val)
{
while (L&LT;=R)
{
int mid= (L+R)/2;
if (hash[mid]==val)
return mid;
else if (hash[mid]>val)
R=mid-1;
Else
l=mid+1;
}
return-1;
}
int main ()
{
scanf ("%d", &t);
for (int i=1;i<=t;i++)
{
scanf ("%d", &n);
memset (used,false,sizeof (used));
for (int j=1;j<=n;j++)
{
scanf ("%d%d", &AMP;A[J].L,&AMP;A[J].R);
++A[J].R;
HASH[2*J-1]=A[J].L;
HASH[2*J]=A[J].R;
}
Sort (hash+1,hash+1+2*n);
int index=2;
for (int j=1;j<2*n;j++)
if (hash[j]!=hash[j+1])
HASH[INDEX++]=HASH[J+1];
Make (1,index-1,1);
for (int j=1;j<=n;j++)
{
int Lset=b_search (1,INDEX-1,A[J].L);
int Rset=b_search (1,INDEX-1,A[J].R);
Insert (1,LSET,RSET,J);
}
Count (1);
int ans=0;
for (int j=0;j<max;j++)
if (Used[j])
ans++;
cout<<ans<<endl;
}
return 0;
}


pku2528----interval staining to segment update

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.