2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it (扫描线)
链接:https://ac.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 (i−xc)2+(j−yc)2≤r\sqrt{(i-x_c)^2 + (j-y_c)^2} \leq r(i−xc)2+(j−yc)2≤r 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
题意:
给你一个n*m的矩阵,初始每一个方格全是 0,然后给你q个操作,
每一个操作给你一个x,y和一个r,然后再矩阵中的\((i,j)\) 方格,如果满足
$ (\sqrt{(i-x_c)^2 + (j-y_c)^2} \leq r $ 就涂为黑色。
问你最后还有多少个白色的方格。
思路:
对于每一个询问,遍历它能影响到的行,利用上面的公式可以直接求出该询问在该行影响的区间,
我们把用vector 储存每一行 有哪些区间要被涂成黑色,
把所有询问都转成按行的区间后,我们对于每一个行,对区间进行sort,然后合并处理区间,同时计算出该行有多少个方格被涂成黑色即可。
总体的时间复杂度是\(O(T*q*n*log_2(m))\)
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline void getInt(int *p);
const int maxn = 30010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n, m, q;
struct node {
int l, r;
node() {}
node(int xx, int yy)
{
l = xx;
r = yy;
}
bool operator < (const node &bb) const
{
if (l != bb.l) {
return l < bb.l;
} else {
return r > bb.r;
}
}
};
std::vector<node> v[maxn];
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
int t;
scanf("%d", &t);
while (t--) {
du3(n, m, q);
int x, y, r;
while (q--) {
du3(x, y, r);
for (int i = max(0, x - r); i <= min(n - 1, x + r); ++i) {
int d = sqrt(r * r - (i - x) * (i - x) + eps);
int l = max(0, y - d );
int rr = min(m - 1, y + d );
v[i].push_back(node(l, rr));
// cout << i << " " << l << " " << rr << endl;
}
}
int ans = n * m;
rep(i, 0, n) {
if (sz(v[i])) {
sort(ALL(v[i]));
node now = node(0, -1);
for (auto x : v[i]) {
if (x.l > now.r) {
ans -= (now.r - now.l + 1);
now = x;
} else {
now.r = max(now.r, x.r);
}
}
ans -= (now.r - now.l + 1);
v[i].clear();
}
}
printf("%d\n", ans );
}
return 0;
}
inline void getInt(int *p)
{
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
} else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}