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);
}

 

转载于:https://www.cnblogs.com/liyakun/archive/2013/05/01/3053235.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值