Color it

链接:https://www.nowcoder.com/acm/contest/163/F
来源:牛客网

时间限制:C/C++ 3秒,其他语言6秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
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

把圆抽象成一条一条的线段,对每个Y。排序后合并线段,累加求和即可。

#include <bits/stdc++.h>
const int MAXN=21000;
using namespace std;
struct node
{
    int l,r,y;
} nodes[MAXN*200];
bool cmp(node a,node b)
{
    if(a.l!=b.r) return a.l<b.l;
    return a.r<b.r;
}
struct edge
{
    vector<node> s;
} a[MAXN];
void init()
{
    for(int i=0; i<=20000; i++)
    {
        a[i].s.clear();
    }
    memset(nodes,0,sizeof(nodes));
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        int n,m,q,i,j;
        int x,y,r;
        scanf("%d%d%d",&n,&m,&q);
        int cnt=0;
        for(i=1; i<=q; i++)
        {
            scanf("%d%d%d",&x,&y,&r);
            for(j=0; j+y<m&&j<=r; j++)
            {
                int temp_x=floor(sqrt(r*r-j*j));
                nodes[cnt].l=max(x-temp_x,0);
                nodes[cnt].r=min(x+temp_x,n-1);
                nodes[cnt++].y=j+y;
            }
            for(j=1; y-j>=0&&j<=r; j++)
            {
                int temp_x=floor(sqrt(r*r-j*j));
                //if(temp_x<1) break;
                nodes[cnt].l=max(x-temp_x,0);
                nodes[cnt].r=min(x+temp_x,n-1);
                nodes[cnt++].y=y-j;
            }
        }
        for(i=0; i<cnt; i++)
        {
            a[nodes[i].y].s.push_back({nodes[i].l,nodes[i].r,nodes[i].y});
        }
        for(i=0; i<20000; i++)
        {
            sort(a[i].s.begin(),a[i].s.end(),cmp);
        }
        int temp_left=nodes[0].l;
        int temp_right=nodes[0].r;
        int ans=0;
        int tmp=0;
        for(i=0; i<20000; i++)
        {
            if(a[i].s.empty()) continue;
            tmp=a[i].s[0].r-a[i].s[0].l+1;
            ans+=tmp;
            temp_left=a[i].s[0].l;
            temp_right=a[i].s[0].r;
            for(j=1; j<a[i].s.size(); j++)
            {
                if(a[i].s[j].l>temp_right)
                {
                    ans+=a[i].s[j].r-a[i].s[j].l+1;
                    temp_left=a[i].s[j].l;
                    temp_right=a[i].s[j].r;
                }
                else if(a[i].s[j].l==temp_right)
                {
                    ans+=a[i].s[j].r-a[i].s[j].l;
                    temp_left=a[i].s[j].l;
                    temp_right=a[i].s[j].r;
                }
                else
                {
                    ans+=max(a[i].s[j].r-temp_right,0);
                    temp_right=max(temp_right,a[i].s[j].r);
                }
            }
        }
        ans=n*m-ans;
        cout<<ans<<endl;
    }
    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/zyf3855923/p/9432045.html