Description
给一个平面网格图染色,有四种操作:
0: 0 : 清空所有颜色
1 x y c: 1 x y c : 给 (x,y) ( x , y ) 染 c c 颜色
查询以 (1,y1) ( 1 , y 1 ) 为左下角以 (x,y2) ( x , y 2 ) 为右上角的子矩阵中有多少不同的颜色
3: 3 : 结束
Input
每行一个操作,以 3 3 结束输入
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;
}