轉載自: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;
}