1 条题解
-
0
每个位置最多被采摘一次,可以考虑使用标记数组
st[i][j]=1
表示位置(i,j)
被采摘了,所有操作结束后,对标记数组求和即为答案。整体复杂度为 ,会超时。正解,考虑二维差分,将区间修改转为单点修改,整体复杂度为 。
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1005, INF = 0x3f3f3f3f, MOD = 1E9 + 7; int n, k, st[N][N], ans; void solve1() { cin >> n >> k; int x1, y1, x2, y2; while (k--) { cin >> x1 >> y1 >> x2 >> y2; for (int i = x1; i <= x2; i++) for (int j = y1; j <= y2; j++) st[i][j] = 1; } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) ans += st[i][j]; cout << ans << endl; } int solve2() { cin >> n >> k; int x1, y1, x2, y2; while (k--) { cin >> x1 >> y1 >> x2 >> y2; st[x1][y1]++; st[x1][y2 + 1]--; st[x2 + 1][y1]--; st[x2 + 1][y2 + 1]++; } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { st[i][j] += st[i - 1][j] + st[i][j - 1] - st[i - 1][j - 1]; if (st[i][j] > 0) ans++; } cout << ans << endl; return 0; } int main() { solve1(); }
- 1
信息
- ID
- 417
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 8
- 标签
- (无)
- 递交数
- 787
- 已通过
- 100
- 上传者