ZOJ1610_Count the Colors (line segment tree/segment update), line segment tree segment update

Source: Internet
Author: User

ZOJ1610_Count the Colors (line segment tree/segment update), line segment tree segment update

Solution report

Question:

A 8000-length line segment is colored to determine the number of consecutive intervals in each color segment after dyeing.

Ideas:

For the interval problem, we use a line segment tree to update the segments. At last, we press all the segments to the leaf node to calculate the color of the leaf node.

#include <iostream>#include <cstring>#include <cstdio>using namespace std;int lz[32000],_hash[10000],color[10000],cnt;void push_down(int rt){    if(lz[rt])    {        lz[rt*2]=lz[rt*2+1]=lz[rt];        lz[rt]=0;    }}void update(int rt,int l,int r,int ql,int qr,int v){    if(ql>r||qr<l)return ;    if(ql<=l&&r<=qr)    {        lz[rt]=v;        return ;    }    push_down(rt);    int mid=(l+r)/2;    update(rt*2,l,mid,ql,qr,v);    update(rt*2+1,mid+1,r,ql,qr,v);}void bin(int rt,int l,int r){    if(l==r)    {        color[cnt++]=lz[rt];        return ;    }    push_down(rt);    bin(rt*2,l,(l+r)/2);    bin(rt*2+1,(l+r)/2+1,r);}int main(){    int n,m,i,j,ql,qr,a;    while(~scanf("%d",&n))    {        cnt=0;        memset(lz,0,sizeof(lz));        memset(_hash,0,sizeof(_hash));        m=8000;        int cmax=-1;        for(i=0; i<n; i++)        {            scanf("%d%d%d",&ql,&qr,&a);            update(1,1,m,ql+1,qr,a+1);        }        bin(1,1,m);        for(i=0; i<cnt;)        {            j=i+1;            _hash[color[i]]++;            while(color[j]==color[i]&&j<cnt)                j++;            i=j;        }        for(i=1; i<=m+1; i++)        {            if(_hash[i])                printf("%d %d\n",i-1,_hash[i]);        }        printf("\n");    }    return 0;}
Attached manual random data

input:102 4 6 4 6 21 9 34 6 21 20 32 4 3 6 7 13 7 94 6 92 6 41043 54 8000323 4342 123234 2332 3212 6 2354 546 12843 8888 80003000 8000 023 4329 9923 2323 82390 3293 1101 34 8000 43 343 99341 3414 80007999 8000 8000344 345 1434 3455 034 45 800043 56 4556 64 0898 4599 8000output:3 24 19 10 11 18 19 323 10 21 145 199 18000 5

Count the Colors Time Limit: 2 Seconds Memory Limit: 65536 KB Painting some colored segments on a line, some previusly painted segments may be covered by some of the subsequent ones.

Your task is counting the segments of different colors you can see at last.


Input

The first line of each data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored segments.

Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:

X1 x2 c

X1 and x2 indicate the left endpoint and right endpoint of the segment, c indicates the color of the segment.

All the numbers are in the range [0, 8000], and they are all integers.

Input may contain several data set, process to the end of file.


Output

Each line of the output shoshould contain a color index that can be seen from the top, following the count of the segments of this color, they shocould be printed according to the color index.

If some color can't be seen, you shouldn't print it.

Print a blank line after every dataset.


Sample Input

5
0 4 4
0 3 1
3 4 2
0 2 2
0 2 3
4
0 1 1
3 4 1
1 3 2
1 3 1
6
0 1 0
1 2 1
2 3 1
1 2 0
2 3 0
1 2 1


Sample Output

1 1
2 1
3 1

1 1

0 2
1 1




Zoj 1610 line segment tree question: Count the Colors why is it always segment fault? Under what circumstances will the segment fault appear?

For example, if the array is small, the subscript of the array is negative, or the stack is exposed recursively, It is deleted by 0 ..

ACM line segment tree coloring

I changed some, but I didn't change it. Please refer to my code.
# Include <cstdio>
# Include <cstring>
# Define MAXN 8100 // maximum range []
Int answer [MAXN * 4]; // It is used to store the "answer" that is, answer [I] represents the count of segments of color I;
Struct node
{
Int left, right; // The range represented by this node
Int col; // The color covered in this interval
} Tree [4 * MAXN]; // use a tree array to simulate a line segment tree. Pay attention to the number of nodes.
Void init ()
{
Memset (answer, 0, sizeof (answer ));
}
Void build (int step, int s, int e) // uses Recursion TO build
{
Tree [step]. col =-1; // initialize the color attribute
Tree [step]. left = s;
Tree [step]. right = e;
If (s = E-1) return; // constructed
Int mid = (s + e)/2;
Build (step * 2, s, mid );
Build (step * 2 + 1, mid, e );
}
Void insert (int s, int e, int c, int step) // step is the subscript of the tree array. [s, e] is colored c.
{
/*
1
0 8000 1
The above data is abnormal.
*/
If (tree [step]. left> = s & tree [step]. right <= e) // change it here,
{
Tree [step]. col = c;
Return;
}
Int mid = (tree [step]. left + tree [step]. right)/2;
If (e <= mid)
Insert (s, e, c, step * 2 );
Else if (s> = mid)
Insert (s, e, c, step * 2 + 1 );
Else
{
Insert (s, mid, c, step * 2 );
Insert (s, mid, c, step * 2 + 1 );
}
}
Void count (int step)
{
If (tree [step]. col! =-1)
Answer [tree [step]. col] ++;
If (tree [step]. left = tree [step]. right-1)
Return;
Count (step * 2 );
Count (step * 2 + 1 );
}
Int main ()
{
Int t, a, B, c;
While (scanf ("% d", & t )! = EOF)
{
Init ();
Build (1, 0, MAXN );
While (t --)
{
Scanf ("% d", & a, & B, & c );
Insert (a, B + 1, c, 1); // you are left closed and right open. Add 1 here.

}
Count (1); ...... the remaining full text>

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.