Zju1610 Count The Colors (the lazy tag is detailed)

Source: Internet
Author: User

Description Draw some color segments on one line, some older colors will be covered by the later color.
Your task is to count the number of different color segments you can see later.
Input Each set of test data the first line has only one integer n, 1 <= n <= 8000, equal to the number of fills
The next n rows have three non-negative integers per line, separated by a space between them.
X1 X2 C
X1 and X2 represent the leftmost point of the fill segment and the rightmost point, and C indicates the color filled in.
All numbers are integers in the range [0..8000].
The input has multiple sets of test data and ends with a file terminator.
each line of output outputs is the number of the last visible color, and the number of segments of this color.
If this color is not visible at last, do not print it.
Each group of data is followed by a blank line.
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 
Sample Output
1 1 
2 1 
3 1 

1 1 

0 2 
1 1

Depressed, incredibly did not find a good question to when the line tree of the board

Then lose this water a bit of the lazy mark, do not understand, do not look, it is not an entry question

Lazy Tags:

Like the name, lazy mark, just want to work less (so fast)

The simplest method of interval modification is, of course, to enumerate each point in the interval to a single point of modification, so congratulations to tle.

So what's the point, is it possible to think that some of our updated values don't necessarily need to be used immediately

Maybe someone doesn't understand, so I'll give you an example:

If we want to modify 1-5, and then add to 3-5, then modify 1-2, and finally sum 1-5, then the first sum of 1-2 this paragraph of the modification is not useless, we can temporarily do not modify them, only a mark, and then the second time to modify the 1-2, then we modify the mark is good, The final sum is marked down, which saves the time of a modification.

This method can greatly optimize the time complexity, but also easy to write hang, beginners carefully act

Well, suddenly found that I choose the problem of poison, nothing, this is to let everyone learn lazy

Exercises

In this case, I still think this is a good question, this is my pure handwriting code, line tree + violent AC This problem

For details, see the code:

Call everybody don't learn me, must write pushdown, otherwise the code is too ugly, I was sprayed many times

#include <cstdio> #include <algorithm> #include <cstring> using namespace std; struct Oo{int a,b,num,lazy;}
S[50001];
int n,m,ans[50001],v,sum[30001];
    void build (int now,int x,int y) {s[now].a=x,s[now].b=y;s[now].lazy=0;ans[now]=-10;
        if (x==y) {s[now].num=-100;
    return;
        } else {build (now*2,x,x+y>>1);
        Build (Now*2+1, (x+y>>1) +1,y);
    S[now].num=s[now*2].num+s[now*2+1].num;
        }} void Change (int now,int x,int Y,int z) {if (x<=s[now].a&&y>=s[now].b) {s[now].lazy=z;
        S[now].num=z;
    return; } if (s[now].lazy!=0) {if (s[now].a!=s[now].b) s[now<<1].lazy=s[now].lazy,s[(now<<1 ) +1].lazy=s[now].lazy,s[now<<1].num=s[now<<1].lazy,s[(now<<1) +1].num=s[(now<<1) +1].
        Lazy
    s[now].lazy=0;
    } int mid=s[now].a+s[now].b>>1;
    if (x<=mid) change (now<<1,x,y,z); if (y> mid) Change ((now<<1) +1,x,y,z); if (s[now<<1].num==-1| |
    s[(now<<1) +1].num==-1) s[now].num=-1;
    else if (s[now<<1].num!=s[(now<<1) +1].num) s[now].num=-1;
else s[now].num=s[now<<1].num; } void get (int now,int x,int y) {if (x==y| |
        S[now].num!=-1) {if (s[now].num>=0) ans[++v]=s[now].num; 
    return;
        } if (S[now].num==-1) {get (now<<1,x,x+y>>1);
    Get ((now<<1) +1, (x+y>>1) +1,y);
        }} int main () {while (scanf ("%d", &n)!=eof) {memset (sum,0,sizeof (sum));
        V=0;int maxx=0;
        Build (1,0,n*2+1);
            for (int i=1,k,a,b;i<=n;i++) {scanf ("%d%d%d", &k,&a,&b);
        Change (1,K,A-1,B);
        } get (1,0,n*2+1);
        for (int i=1;i<=v;i++) if (ans[i]==ans[i+1]) ans[i]=-10;
 for (int i=1;i<=v;i++) if (ans[i]>=0)               Sum[ans[i]]++,maxx=max (Ans[i],maxx);
        for (int i=0;i<=maxx;i++) if (sum[i]!=0) printf ("%d%d\n", i,sum[i]);
    printf ("\ n"); }
}


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.