时间限制: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
Solution
这题可以用线段树来做,区间更新和查询。n*m的网格,如果把每一行都看成一条长度为m的线段,刚开始是全白色的,每次画圆圈都涉及这条线段上的一个区间那就把它涂黑,总共n行。
建树只需1次,O(nlogn);
因共有n行,q=200个圆圈,故有n*200次修改和n次查询。q*O(nlogn),q<=200,n<=20000,不会T。
#include<bits/stdc++.h>
#define ll long long
#define L(u) u<<1
#define R(u) u<<1|1
using namespace std;
const int MX = 20001;
int T, n, m, q, L, R, ans;
struct treeNode {
int l, r, sum, lazy;
};
struct segmentTree {
treeNode tree[MX << 2];
void pushDown(int u) { //Key Point, Keynote
if (tree[u].lazy != -1) {
tree[u].sum = tree[u].lazy * (tree[u].r - tree[u].l + 1);
if (tree[u].l != tree[u].r) {
tree[L(u)].lazy = tree[u].lazy;
tree[R(u)].lazy = tree[u].lazy;
}
tree[u].lazy = -1;
}
}
void update(int u) {
pushDown(L(u));
pushDown(R(u));
tree[u].sum = tree[L(u)].sum + tree[R(u)].sum;
}
void build(int u, int l, int r) {
tree[u].l = l;
tree[u].r = r;
tree[u].lazy = -1;
if (l != r) {
int mid = l + r >> 1;
build(L(u), l, mid);
build(R(u), mid + 1, r);
update(u);
} else {
tree[u].sum = 1;
}
}
void change(int u, int l, int r, int val) {
if (l == tree[u].l && r == tree[u].r) {
tree[u].lazy = val;
pushDown(u);
return;
}
pushDown(u);
int mid = tree[u].l + tree[u].r >> 1;
if (r <= mid) change(L(u), l, r, val);
else if (l > mid) change(R(u), l, r, val);
else {
change(L(u), l, mid, val);
change(R(u), mid + 1, r, val);
}
update(u);
}
int sum(int u, int l, int r) {
pushDown(u);
if (tree[u].l == l && tree[u].r == r)
return tree[u].sum;
int mid = tree[u].l + tree[u].r >> 1;
if (r <= mid) return sum(L(u), l, r);
else if (l > mid) return sum(R(u), l, r);
else return sum(L(u), l, mid) + sum(R(u), mid + 1, r);
}
};
segmentTree ST;
struct Circle {
int x, y, r;
} c[205];
struct Segment {
int l, r;
} lin[205];
int main() {
//freopen("../in","r",stdin);
scanf("%d", &T);
while (T--) {
scanf("%d%d%d", &n, &m, &q);
for (int i = 0; i < q; i++) {
scanf("%d%d%d", &c[i].x, &c[i].y, &c[i].r);
lin[i].l = lin[i].r = c[i].y;
}
ST.build(1, 1, m);
ans = 0;
for (ll i = 0; i < n; ++i) {
ST.change(1, 1, m, 1);
for (int j = 0; j < q; ++j) {
while ((lin[j].l >= 0 || lin[j].r <= m) &&
(i - c[j].x) * (i - c[j].x) + 1ll * (lin[j].r - c[j].y) * (lin[j].r - c[j].y) <= 1ll* c[j].r * c[j].r)
++lin[j].r, --lin[j].l;
while (lin[j].l < lin[j].r &&
(i - c[j].x) * (i - c[j].x) + 1ll * ((lin[j].r - 1) - c[j].y) * ((lin[j].r - 1) - c[j].y) >
1ll * c[j].r * c[j].r)
--lin[j].r, ++lin[j].l;
}
for (int j = 0; j < q; ++j) {
L = lin[j].l + 1;
R = lin[j].r - 1;
L = max(L, 0);
R = min(R, m - 1);
++L;
++R;
if (L <= R) {
ST.change(1, L, R, 0);
}
}
ans += ST.sum(1, 1, m);
}
printf("%d\n", ans);
}
return 0;
}