HDU 6183 Color it(线段树)

59 篇文章 0 订阅

Description

给一个平面网格图染色,有四种操作:

0: 0 : 清空所有颜色

1 x y c: 1   x   y   c : (x,y) ( x , y ) c c 颜色

2 x y1 y2:查询以 (1,y1) ( 1 , y 1 ) 为左下角以 (x,y2) ( x , y 2 ) 为右上角的子矩阵中有多少不同的颜色

3: 3 : 结束

Input

每行一个操作,以 3 3 结束输入(1x,y106,0c50)

Output

对于每个查询操作,输出答案

Sample Input

0
1 1000000 1000000 50
1 1000000 999999 0
1 1000000 999999 0
1 1000000 1000000 49
2 1000000 1000000 1000000
2 1000000 1 1000000
0
1 1 1 1
2 1 1 2
1 1 2 2
2 1 1 2
1 2 2 2
2 1 1 2
1 2 1 3
2 2 1 2
2 10 1 2
2 10 2 2
0
1 1 1 1
2 1 1 1
1 1 2 1
2 1 1 2
1 2 2 1
2 1 1 2
1 2 1 1
2 2 1 2
2 10 1 2
2 10 2 2
3

Sample Output

2
3
1
2
2
3
3
1
1
1
1
1
1
1

Solution

对每种颜色建线段树,维护纵坐标区间该种颜色出现的横坐标最小值,查询时对每个颜色查询,只要给出的纵坐标区间中横坐标最小值不超过给出的横坐标那么说明该种颜色在这个子矩阵中出现过,注意按完全二叉树建立 51 51 棵线段树空间不够,但是所需节点并不多,所以把所有节点按出现顺序编号即可

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
#define M 1000000
#define maxn 1000005
int ls[maxn*3],rs[maxn*3],tot,root[55],pos[maxn*3],flag;
void update(int &t,int l,int r,int x,int y)
{
    if(t==0)
    {
        t=++tot;
        pos[t]=x;
    }
    if(pos[t]>x)pos[t]=x;
    if(l==r)return ;
    int mid=(l+r)/2;
    if(y<=mid)update(ls[t],l,mid,x,y);
    else update(rs[t],mid+1,r,x,y);
}
void query(int t,int l,int r,int x,int y,int yy)
{
    if(flag||t==0)return ;
    if(y<=l&&r<=yy)
    {
        if(pos[t]<=x)flag=1;
        return ;
    }
    int mid=(l+r)/2;
    if(y<=mid)query(ls[t],l,mid,x,y,yy);
    if(yy>mid)query(rs[t],mid+1,r,x,y,yy);
}
int main()
{
    int op;
    while(~scanf("%d",&op))
    {
        if(op==0)
        {
            for(int i=1;i<=tot;i++)ls[i]=rs[i]=0;
            tot=0;  
            memset(root,0,sizeof(root));
        }
        else if(op==1)
        {
            int x,y,c;
            scanf("%d%d%d",&x,&y,&c);
            update(root[c],1,M,x,y);
        }
        else if(op==2)
        {
            int x,y,yy;
            scanf("%d%d%d",&x,&y,&yy);
            int ans=0;
            for(int c=0;c<=50;c++)
            {
                flag=0;
                query(root[c],1,M,x,y,yy);
                ans+=flag;
            }
            printf("%d\n",ans);
        }
        else break;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值