70分代码:
#include <bits/stdc++.h>//70分,有待优化 using namespace std; const int maxn = 200005; int n, m, k; int t[maxn]; int c[maxn]; int main() { cin >> n >> m >> k; for (int i = 1; i <= n; i++) { cin >> t[i] >> c[i]; } for (int i = 1; i <= m; i++) { int ans = 0; int q; cin >> q; for (int j = 1; j <= n; j++) { if (q + k <= t[j] && q + k + c[j] > t[j]) { ans++; } } cout << ans << endl; } }
100分代码:使用差分+前缀和优化
(看了csdn一位大佬的分析,很直观)
100分代码:
#include <bits/stdc++.h> //100分 优化方法:差分 using namespace std; const int maxn = 200005; int n, m, k; int a[maxn]; int main() { cin >> n >> m >> k; for (int i = 1; i <= n; i++) { int t, c; cin >> t >> c; int l = max(0, t + 1 - k - c), r = max(0, t - k); a[l]++; a[r + 1]--; // 处理差分数组 } for (int i = 1; i <= maxn; i++) // 变前缀和(预处理),这里用maxn,不用n { a[i] += a[i - 1]; } for (int i = 1; i <= m; i++) { int q; cin >> q; cout << a[q] << endl; } }