题目
Link
就是查询矩形内有多少个点。
分析
二维树状数组维护就好了,。
Code
#include <bits/stdc++.h>
const int maxn = 1000 + 131;
struct Point {
int x, y;
};
int Num[maxn][maxn];
bool Vis[maxn][maxn];
int lowbit(int x) { return x&(-x); }
int Sum(int x, int y) {
int ret = 0;
int xx = x;
while(xx) {
int yy = y;
while(yy)
{
ret += Num[xx][yy];
yy -= lowbit(yy);
}
xx -= lowbit(xx);
}
return ret;
}
void Add(int x,int y, int val) {
int xx = x;
while(xx < maxn)
{
int yy = y;
while(yy < maxn)
{
Num[xx][yy] += val;
yy += lowbit(yy);
}
xx += lowbit(xx);
}
}
int main() {
int T;
scanf("%d",&T);
for(int kase = 1; kase <= T; kase++)
{
printf("Case %d:\n",kase);
memset(Num, 0, sizeof(Num));
memset(Vis, false, sizeof(Vis));
int Q, tmp;
Point a, b;
scanf("%d",&Q);
while(Q--)
{
scanf("%d",&tmp);
if(tmp == 0)
{
scanf("%d%d",&a.x,&a.y);
if(!Vis[a.x][a.y])
Add(a.x+1,a.y+1,1), Vis[a.x][a.y] = true;
}
else {
scanf("%d%d%d%d",&a.x,&a.y,&b.x,&b.y);
int Ans = Sum(b.x+1,b.y+1) - Sum(a.x,b.y+1) - Sum(b.x+1,a.y) + Sum(a.x,a.y);
printf("%d\n",Ans);
}
}
}
return 0;
}