There is a matrix A that has N rows and M columns. Each grid (i,j)(0 ≤ i < N, 0 ≤ j < M) is painted in white at first.
Then we perform q operations:
For each operation, we are given (xc, yc) and r. We will paint all grids (i, j) that meets to black.
You need to calculate the number of white grids left in matrix A.
输入描述:
The first line of the input is T(1≤ T ≤ 40), which stands for the number of test cases you need to solve.
The first line of each case contains three integers N, M and q (1 ≤ N, M ≤ 2 x 104; 1 ≤ q ≤ 200), as mentioned above.
The next q lines, each lines contains three integers xc, yc and r (0 ≤ xc < N; 0 ≤ yc < M; 0 ≤ r ≤ 105), as mentioned above.
输出描述:
For each test case, output one number.
示例1
输入
复制
2 39 49 2 12 31 6 15 41 26 1 1 1 0 0 1
输出
复制
729 0
题意:给一个矩形,用圆去覆盖。最后没有被覆盖的点有几个。
分析:拿二维树状数组和二维线段树一通乱搞,怎么也搞不出来!最后看了题解标记一下,然后暴力就好了,然后开始自闭。
1、考虑一个圆,先把圆最大能覆盖的列求出来,然后求每列在圆内的部分,并标记。
2、d=sqrt(r*r-(y-j)*(y-j)+1e-6);用这个求得当前圆的每一列在园内的部分。(其实就是固定列标记行)
用vector<pair<int,int> >v[MAX];存标记部分。v[i][0].first、v[i][0].second。表示第i列第一个被标记的行是从first~second。
3、然后扫一遍所有列,数一下该列被标记的有几行。因为圆和圆会重叠,所以,要分情况讨论一下。先跟据first从小到大排序。
①如果两个圆没有相交,那直接就是j.second-j.first+1。
②如果两个圆有交点,因为排序,所以前面的圆一定在上面,标记上面圆在该列靠下的点,那么此时被覆盖的点就是j.second-r。
③如果当前圆被上一个圆内含。那么就不用计算直接跳过。
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
int q,m,n;
vector<pair<int,int> >v[MAX];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&q);
for(int i=0;i<MAX;i++) v[i].clear();
for(int i=1;i<=q;i++)
{
int x,y,r;
scanf("%d%d%d",&x,&y,&r);
int uy=max(0,y-r);
int dy=min(m-1,y+r);
for(int j=uy;j<=dy;j++)
{
int d=sqrt(r*r-(y-j)*(y-j)+1e-6);
int ux=max(0,x-d);
int dx=min(x+d,n-1);
v[j].push_back(make_pair(ux,dx));
}
}
int ans=n*m;
for(int i=0;i<m;i++)
{
int len=v[i].size();
if(len>0)
{
sort(v[i].begin(),v[i].end());
int last=-1;
for(auto j:v[i])
{
if(last>=j.second)
continue;
if(j.first>last)
ans-=j.second-j.first+1;
else
ans-=j.second-last;
last=j.second;
}
}
}
printf("%d\n",ans);
}
return 0;
}