Paint The Wall

Problem Description
As a amateur artist, Xenocide loves painting the wall. The wall can be considered as a line consisting of n nodes. Each node has its own color.
Xenocide spends all day in front of the wall. Sometimes, he paints some consecutive nodes so that these nodes have the same color. When he feels tired, he focuses on a particular color and counts the number of nodes that have this color within a given interval.
Now Xenocide is tired of counting, so he turns to you for help.
 
Input
The input consists of several test cases. The first line of each test case contains two integer n, m(1<=n, m<=100000) indicating the length of the wall and the number of queries. The following line contains N integers which describe the original color of every position. Then m lines follow. Each line contains 4 non-negative integers a, l, r, z(1<=a<=2, 0<=l<=r<n ,0<=z<231). a = 1 indicates that Xenocide paints nodes between l and r and the resulting color is z. a = 2 indicates that Xenocide wants to know how many nodes between l and r have the color z.
 
Output
Print the corresponding answer for each queries.
#include<stdio.h>
struct node
{
    int l,r;
    int c;
    int mi,ma;
}st[100000*4];
int color[100005];
int max(int a,int b);
int min(int a,int b);
void create(int l,int r,int k);
void update(int k);
void change(int l,int r,int z,int k);
int query(int l,int r,int z,int k);
int main()
{
    int n,m,i,a,l,r,z;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=1;i<=n;++i)
        scanf("%d",&color[i]);
        create(1,n,1);
        while(m--)
        {
            scanf("%d%d%d%d",&a,&l,&r,&z);
            ++l,++r;
            if(l>r)
            {
                int temp;
                temp=l;
                l=r;
                r=temp;
            }
            if(a==1)
            {
                change(l,r,z,1);
            }
            else
            printf("%d\n",query(l,r,z,1));
        }
    }
}
int max(int a,int b)
{
    if(a>b)
    return a;
    return b;
}
int min(int a,int b)
{
    if(a<b)
    return a;
    return b;
}
void create(int l,int r,int k)
{
    st[k].l=l;
    st[k].r=r;
    st[k].c=-1;
    if(l==r)
    {
        st[k].c=color[l];
        st[k].mi=st[k].ma=color[l];
        return;
    }
    int mid=(l+r)/2;
    create(l,mid,2*k);
    create(mid+1,r,2*k+1);
    if(st[2*k].c==st[2*k+1].c)
    st[k].c=st[2*k].c;
    st[k].mi=min(st[k*2].mi,st[2*k+1].mi);
    st[k].ma=max(st[2*k].ma,st[2*k+1].ma);
}
void change(int l,int r,int z,int k)
{
    if(st[k].l==l&&st[k].r==r)
    {
        if(st[k].c==z)
        return;
        st[k].c=z;
        st[k].mi=st[k].ma=z;
        return;
    }
    int mid=(st[k].l+st[k].r)/2;
    if(st[k].c!=-1)
    update(k);
    if(r<=mid)
    change(l,r,z,2*k);
    else if(l>mid)
    change(l,r,z,2*k+1);
    else
    {
        change(l,mid,z,2*k);
        change(mid+1,r,z,2*k+1);
    }
    if(st[2*k].c==st[2*k+1].c)
    st[k].c=st[2*k].c;
    st[k].mi=min(st[k*2].mi,st[2*k+1].mi);
    st[k].ma=max(st[2*k].ma,st[2*k+1].ma);
}
void update(int k)
{
    st[2*k].c=st[k].c;
    st[2*k+1].c=st[k].c;
    st[2*k].mi=st[2*k+1].mi=st[k].mi;
    st[2*k+1].ma=st[2*k].ma=st[k].ma;
    st[k].c=-1;
}
int query(int l,int r,int z,int k)
{
    if(st[k].c!=-1)
    {
        if(st[k].c==z)
        return r-l+1;
    }
    if(st[k].mi>z||st[k].ma<z)
    return 0;
    int mid=(st[k].l+st[k].r)/2;
    if(l>mid)
    return query(l,r,z,2*k+1);
    else if(r<=mid)
    return query(l,r,z,2*k);
    else
    return query(l,mid,z,2*k)+query(mid+1,r,z,2*k+1);
}

 

posted on 2013-05-01 15:28  耶稣爱你  阅读(374)  评论(0编辑  收藏  举报