2018上海大都会赛 F color it(扫描线)

题目描述 

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

思路:因为q最多只有200,所以考虑对每一行进行扫描,求出每一行被这些圆覆盖的个数。

代码:

// 2018上海大都会 F colcor it
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include <iostream>
using namespace std;

const int maxn=1e5+7;
#define mod 998244353
#define Lson l,m,rt<<1
#define Rson m+1,r,rt<<1|1
typedef long long ll;
using namespace std;
int n,m;
struct circle
{
    int x,y,r;
}p[500];
struct Point
{
    int l,r;
    bool operator < (const Point &a) const
    {
        if(l==a.l) return r < a.r;
        return l < a.l;
    }
}L[1005];
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int q;
        int ans=0;
        scanf("%d%d%d",&n,&m,&q);
        for(int i=0;i<q;i++)
        {
            scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].r);
        }
        for(int i=0;i<n;i++)
        {
            int cnt=0,w,h;
            for(int j=0;j<q;j++)
            {
                if(abs(i-p[j].x)>p[j].r) continue;
                w=abs(i-p[j].x);
                h=floor(sqrt(p[j].r*p[j].r-w*w));
                L[++cnt].l=max(0,p[j].y-h);
                L[cnt].r=min(m-1,p[j].y+h);
            }
            sort(L+1,L+1+cnt);
            if(cnt) ans+=L[1].r-L[1].l+1;
            for(int j=2;j<=cnt;j++)
            {
                if(L[j-1].r>=L[j].l)
                {
                    L[j].l=L[j-1].r+1;
                }
                if(L[j].r>=L[j].l)
                {
                    ans+=L[j].r-L[j].l+1;
                }
                L[j].r=max(L[j-1].r,L[j].r);
            }
        }
        printf("%d\n",n*m-ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值