转载自:http://blog.csdn.net/oranges_c/article/details/77800825
Do you like painting? Little D doesn’t like painting, especially messy color paintings. Now Little B is painting. To prevent him from drawing messy painting, Little D asks you to write a program to maintain following operations. The specific format of these operations is as follows.
00 : clear all the points.
11 xx yy cc : add a point which color is cc at point (x,y)(x,y).
22 xx y1y1 y2y2 : count how many different colors in the square (1,y1)(1,y1) and (x,y2)(x,y2). That is to say, if there is a point (a,b)(a,b) colored cc, that 1≤a≤x1≤a≤x and y1≤b≤y2y1≤b≤y2, then the color cc should be counted.
33 : exit.
Input
The input contains many lines.
Each line contains a operation. It may be ‘0’, ‘1 x y c’ ( 1≤x,y≤106,0≤c≤501≤x,y≤106,0≤c≤50 ), ‘2 x y1 y2’ (1≤x,y1,y2≤1061≤x,y1,y2≤106 ) or ‘3’.
x,y,c,y1,y2x,y,c,y1,y2 are all integers.
Assume the last operation is 3 and it appears only once.
There are at most 150000150000 continuous operations of operation 1 and operation 2.
There are at most 1010 operation 0.
Output
For each operation 2, output an integer means the answer .
思路:
1,由于颜色只有51种所以我们可以分别建立51棵线段树,每一棵线段树维护一种颜色值;
2,由于查询的时候横坐标的较小值一直都是一,变化的只是较大的那个横坐标,纵坐标两个都在变。所以,我们可以将纵坐标作为线段树的区间,然后在每个区间内维护这个区间中的出现的最小横坐标值,如果连最小的横坐标都比查询的较大的那个横坐标大,那么就说明这个矩形区域中没有我们要找的点;
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
int tot=0,T[59],v[maxn],ls[maxn],rs[maxn],flag=0;
int op,x,y,c,y2;
void update(int &t,int y,int x,int l,int r)
{
if(!t)
{
t=++tot;
v[t]=x;
}
v[t]=min(v[t],x);
if(l==r) return;
int m=l+r>>1;
if(y<=m)
update(ls[t],y,x,l,m);
else update(rs[t],y,x,m+1,r);
}
void query(int t,int L,int R,int x,int l,int r)
{
if(flag||!t)
return;
if(L<=l&&r<=R)
{
if(v[t]<=x)
flag=1;
return;
}
int m=l+r>>1;
if(L<=m) query(ls[t],L,R,x,l,m);
if(m<R) query(rs[t],L,R,x,m+1,r);
}
int main()
{
while(~scanf("%d",&op))
{
if(op==0)
{
memset(ls,0,sizeof ls);
memset(rs,0,sizeof rs);
memset(T,0,sizeof T);
tot=0;
}
else if(op==1)
{
scanf("%d%d%d",&x,&y,&c);
update(T[c],y,x,1,1e6);
}
else if(op==2)
{
scanf("%d%d%d",&x,&y,&y2);
int ans=0;
for(int i=0;i<=50;i++)
{
flag=0;
query(T[i],y,y2,x,1,1e6);
ans+=flag;
}
printf("%d\n",ans);
}
else return 0;
}
return 0;
}