首页 > 其他 > 详细

HDU4391(线段树+剪枝)

时间:2016-01-19 17:09:20      阅读:139      评论:0      收藏:0      [点我收藏+]

Paint The Wall

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3114    Accepted Submission(s): 846


 

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.
 

 

Sample Input
5 5
1 2 3 4 0
2 1 3 3
1 1 3 1
2 1 3 3
2 0 3 1
2 3 4 1
 
Sample Output
1
0
4
1
剪枝线段树。
#include"cstdio"
#include"algorithm"
using namespace std;
const int MAXN=100005;
struct Node{
    int l,r;
    int color;
    int max,min;
}a[MAXN*3];
void build(int rt,int l,int r)
{
    a[rt].l=l;
    a[rt].r=r;
    if(l==r)
    {
        scanf("%d",&a[rt].color);
        a[rt].max=a[rt].color;
        a[rt].min=a[rt].color;
        return ;
    }
    int mid=(l+r)>>1;
    build(rt<<1,l,mid);
    build((rt<<1)|1,mid+1,r);
    if(a[rt<<1].color==a[(rt<<1)|1].color)    a[rt].color=a[rt<<1].color;
    else a[rt].color=-1;
    a[rt].max=max(a[rt<<1].max,a[(rt<<1)|1].max);
    a[rt].min=min(a[rt<<1].min,a[(rt<<1)|1].min);
}

void update(int rt,int l,int r,int v)
{
    if(a[rt].color==v)    return ;
    if(a[rt].l==l&&a[rt].r==r)
    {
        a[rt].color=v;
        a[rt].max=v;
        a[rt].min=v;
        return ;
    }
    
    if(a[rt].color!=-1)
    {
        a[rt<<1].color=a[(rt<<1)|1].color=a[rt].color;
        a[rt<<1].max=a[(rt<<1)|1].max=a[rt].max;
        a[rt<<1].min=a[(rt<<1)|1].min=a[rt].min;
    }
    
    int mid=(a[rt].l+a[rt].r)>>1;
    if(r<=mid)    update(rt<<1,l,r,v);
    else if(mid<l)    update((rt<<1)|1,l,r,v);
    else{
        update(rt<<1,l,mid,v);
        update((rt<<1)|1,mid+1,r,v);
    }
    if(a[rt<<1].color==a[(rt<<1)|1].color)    a[rt].color=a[rt<<1].color;
    else a[rt].color=-1;
    a[rt].max=max(a[rt<<1].max,a[(rt<<1)|1].max);
    a[rt].min=min(a[rt<<1].min,a[(rt<<1)|1].min);
}

int cnt;
void query(int rt,int l,int r,int c)
{
    if(!(a[rt].min<=c&&c<=a[rt].max))    return ;
    if(a[rt].l==l&&a[rt].r==r&&a[rt].color!=-1)
    {
        if(a[rt].color==c)    cnt+=(r-l+1);
        return ;
    }
    if(a[rt].l==a[rt].r)
    {
        if(a[rt].color==c)    cnt++;
        return ;
    }
    
    if(a[rt].color!=-1)
    {
        a[rt<<1].color=a[(rt<<1)|1].color=a[rt].color;
        a[rt<<1].max=a[(rt<<1)|1].max=a[rt].max;
        a[rt<<1].min=a[(rt<<1)|1].min=a[rt].min;
    }
    
    int mid=(a[rt].l+a[rt].r)>>1;
    if(r<=mid)    query(rt<<1,l,r,c);
    else if(mid<l)    query((rt<<1)|1,l,r,c);
    else{
        query(rt<<1,l,mid,c);
        query((rt<<1)|1,mid+1,r,c);
    }
}

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        build(1,1,n);
        while(m--)
        {
            int op,l,r,c;
            scanf("%d%d%d%d",&op,&l,&r,&c);
            l++,r++;
            if(op==1)
            {
                update(1,l,r,c);
            }
            else
            {
                cnt=0;
                query(1,l,r,c);
                printf("%d\n",cnt);
            }
        }
    }
    
    return 0;
}

 

HDU4391(线段树+剪枝)

原文:http://www.cnblogs.com/program-ccc/p/5142565.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!