F-count the Colors-zoj 1610 (interval coverage)

Source: Internet
Author: User

There is a long canvas, now want to draw some color on this canvas, but the color of the painting will cover the color of the front, now want to know the color distribution of the canvas after the painting, such as the number 1th color has several pieces, 2nd color has a few pieces .... Analysis: Basically the poster is the same, but the final request for the output is the color of the painting has a few pieces, Can follow the way of the poster to do it first, and then to each point of inquiry, do not know whether the complexity is too high. But let's try it first. Note: If you see
Segmentation Fault
You can see if the tree is small, and you can't build it.#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
usingnamespaceStd
#defineLson Root<<1,l,tree[root]. Mid ()
#defineRson Root<<1|1,tree[root]. Mid () +1,r

ConstintMAXN =80005;

structhook{intL, R, C;} P[MAXN];
structtree{
intL, R;
intColor//Record Color
intMid () {return(L+R)/2;}
}tree[maxn*4];
intCOLOR[MAXN];

voidCovercolor (intLintRintE//Paint the interval LR color e
{
for(intI=l; i<=r; i++)
Color[i] = e;
}

voidBuild (intRootintLintR
{
Tree[root]. L = L, Tree[root]. R = r;
//0 is not covered by color, 1 is not covered, and 2 means the subtree is covered by another color
Tree[root].color =0;

if(L = = R)return;

Build (Lson);
Build (Rson);
}
voidUp (intRoot
{
if(Tree[root]. L! = Tree[root]. R
if(tree[root<<1].color = =1&& tree[root<<1|1].color = =1)
Tree[root].color =1;
}
voidInsert (intRootintLintRintE
{
if(Tree[root].color = =1)return;

if(Tree[root]. L = = L && tree[root]. r = = R &&!tree[root].color)
{
Tree[root].color =1;
Covercolor (L, R, E);
return;
}
Tree[root].color =2;

if(R <= Tree[root]. Mid ())
Insert (root<<1, L, R, E);
Elseif(L > Tree[root]. Mid ())
Insert (root<<1|1, L, R, E);
Else
{
Insert (Lson, E);
Insert (Rson, E);
}

Up (root);
}
intMain ()
{
intN

while(SCANF ("%d", &n)! = EOF)
{
intI, ans[maxn] = {0};

for(i=1; i<=n; i++)
scanf"%d%d%d", &AMP;P[I].L, &AMP;P[I].R, &p[i].c);

Build (1,0, maxn-1);
memset (Color,-1,sizeof(Color));

for(I=n; i>0; i--)//because a tight interval is given, and the tree points are built, so the left +1 is turned into a dot covering the
Insert (1, p[i].l+1, P[I].R, P[I].C);

for(i=0; i<maxn; i++)
{
if(color[i]!=-1&& (!i | | color[i]!=color[i-1]) )
ans[color[i]]++;
}

for(i=0; i<maxn; i++)if(Ans[i])
printf"%d%d\n", I, ans[i]);
printf"\ n");
}

return0;

F-count the Colors-zoj 1610 (interval coverage)

Related Article

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.