Color it
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 302 Accepted Submission(s): 62
0 : clear all the points.
1 x y c : add a point which color is c at point (x,y).
2 x y1 y2 : count how many different colors in the square (1,y1) and (x,y2). That is to say, if there is a point (a,b) colored c, that 1≤a≤x and y1≤b≤y2, then the color c should be counted.
3 : exit.
Each line contains a operation. It may be '0', '1 x y c' ( 1≤x,y≤106,0≤c≤50 ), '2 x y1 y2' (1≤x,y1,y2≤106 ) or '3'.
x,y,c,y1,y2 are all integers.
Assume the last operation is 3 and it appears only once.
There are at most 150000 continuous operations of operation 1 and operation 2.
There are at most 10 operation 0.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000 + 10;
int inf = 0x3f3f3f3f;
struct node
{
int l, r;
int lson, rson;
int sum;
int Min;
node()
{
sum = 0;
Min = inf;
lson = rson = 0;
}
}Node[maxn<<2];
int tot;
int root[55];
int judge;
void update(int &rt, int l, int r, int value, int Mi)
{
if(!rt) rt = ++tot;
Node[rt].l = l;
Node[rt].r = r;
Node[rt].sum++;
Node[rt].Min = min(Node[rt].Min, Mi);
if(l == r) return;
int mid = (l + r)>>1;
if(value <= mid) update(Node[rt].lson, l, mid, value, Mi);
else update(Node[rt].rson, mid + 1, r, value, Mi);
}
void query(int rt, int l, int r, int up)
{
if(!rt || judge) return;
if(Node[rt].l == l && Node[rt].r == r)
{
if(Node[rt].Min <= up && Node[rt].sum > 0)
{
judge = 1;
return;
}
return;
}
int mid = (Node[rt].l + Node[rt].r)>>1;
if(r <= mid) query(Node[rt].lson, l, r, up);
else if(l >= mid + 1) query(Node[rt].rson, l, r, up);
else
{
query(Node[rt].lson, l, mid, up);
query(Node[rt].rson, mid + 1, r, up);
}
}
void init()
{
tot = 0;
memset(root, 0, sizeof(root));
for(int i = 0; i < (maxn<<2); i++)
{
Node[i].lson = Node[i].rson = Node[i].sum = 0;
Node[i].Min = inf;
}
}
int main()
{
int op;
init();
while(~scanf("%d", &op) && op != 3)
{
if(op == 0) init();
else if(op == 1)
{
int x, y, c;
scanf("%d%d%d", &x, &y, &c);
update(root[c], 1 , 1000000, y, x);
}
else if(op == 2)
{
int x, y1, y2;
scanf("%d%d%d", &x, &y1, &y2);
int ans = 0;
for(int i = 0; i <= 50; i++)
{
judge = 0;
query(root[i], y1, y2, x);
if(judge)
{
ans++;
}
}
printf("%d\n", ans);
}
}
return 0;
}