Zoj 1610 count the colors (line segment tree, segment update coloring)

Source: Internet
Author: User

Link:

Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 1610.

Question:

Dye a line segment with a length of 8000, and dye the interval [a, B] into the C color each time. Obviously, the color after the link will overwrite the previous color.

Calculate the number of intervals on each color online segment after dyeing.



Analysis and Summary:

It is not difficult to update the lazy mark in segments, but it has been difficult for me for some reasons...

1. I did this question yesterday, but I have been connecting to the samples for a long time. = ~! When I got up in the morning, I suddenly realized that the problem was located. The dyeing section for each question was to dye [a, B] into C. I thought it was just to dye ~ All vertices of B are dyed as C. In fact, this is not the case. Instead, we need to dye not vertices, but intervals. For example, we need to dye [0, 1] instead of dyeing 0, 1, 2, instead, the unit interval [0, 1] is colored. Suppose there is an example:

1 2 1

3 4 1

So this example should output 1 2. Just draw a picture on the paper and we can see that the interval [2, 3] is not stained, so there are two intermittent intervals where the color is 1.

To solve this problem, build a line segment tree (8000,), representing the range of 1 ~ 8000, then update (8000, A + 1, B, c );


2. Because I didn't take a closer look at the question, I got a wrong place, so I 've been Re (segmentation fault on zoj ):

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

This sentence means that only N line segments are colored, and I have always understood that it is colored on a line segment with N unit intervals ~ This is the tragedy. The positive solution is: all the numbers are in the range [0, 8000], and they are all integers.


Code:

#include<iostream>#include<cstdio>#include<cstring>#define mem(str,x) memset(str,(x),sizeof(str))#define FOR(i,s,t) for(int i=(s); i<(t); ++i)#define FF(i,n) for(int i=0; i<(n); ++i)#define mid ((left+right)>>1)#define len (right-left+1)#define lson rt<<1, left, m#define rson rt<<1|1, m+1, right#define STOP puts("Stop Here~");using namespace std;const int MAXN = 8005;int n,col[MAXN<<2],vis[MAXN<<2],ans[MAXN<<2];inline void push_down(int rt){    if(col[rt] != -1){        col[rt<<1] = col[rt<<1|1] = col[rt];        col[rt] = -1;    }}void update(int rt,int left,int right,int l,int r,int data){    if(l<=left && right<=r){        col[rt] = data;        return;    }    if(col[rt] == data) return;    if(col[rt]!=-1)push_down(rt);    int m = mid;    if(l <= m)update(lson,l,r,data);    if(r > m)update(rson,l,r,data);}void query(int rt,int left,int right){    if(col[rt]>=0){        for(int i=left; i<=right; ++i)            vis[i] = col[rt];        return;    }    if(left!=right && col[rt] == -1){        int m = mid;        query(lson);        query(rson);    }}int main(){    int a,b,c;    while(~scanf("%d",&n)){        memset(col,-1,sizeof(col));        for(int i=0; i<n; ++i){            scanf("%d%d%d",&a,&b,&c);            if(a>=b)continue;            update(1,1,8000,a+1,b,c);        }        mem(vis,-1);         query(1,1,8000);        int i = 1;        mem(ans,0);        while(i<MAXN){            int color=vis[i], j=i+1;            if(color==-1){++i; continue;}            while(vis[j]!=-1 && vis[j]==color && j<MAXN) ++j;            ++ans[color];            i=j;        }        for(int i=0; i<MAXN; ++i)if(ans[i])            printf("%d %d\n",i,ans[i]);        puts("");     }    return 0;}


 -- The significance of life is to give it a meaningful person.

Original Http://blog.csdn.net/shuangde800,
D_double (reprinted please mark)

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.