难度:4
知识点:差分
这个题是对序列的差分序列的差分序列进行操作即可,也就是差分套差分,然后为了避免左端点出界,我们设定一个偏移量,整个区间加上两个偏移量,跳水点加上一个偏移量,这样就能避免左右两侧超出数组范围的情况出现了,尤其是左边要避免的,右边还能分类讨论,左边一旦下标为负数了就不行了,然后就是差分序列的一系列操作了,最后输出即可,
写法上我按左右两个分两行来写了没有合并,四个区间这样看的比较清楚,最左边区间的开头是x-3v+1,这个注意不要写错
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;
const int maxn = 1e6 + 5;
const int offset = 30005;
int a[maxn + offset * 2];
int main() {
int n, m;
cin >> n >> m;
m += offset * 2;
while (n--) {
int v, x;
scanf("%d %d", &v, &x);
x += offset;
a[x + 1]++, a[x + 2 * v + 1]--, a[x + 2 * v + 1]--, a[x + 3 * v + 1]++;
a[x - 3 * v + 1]++, a[x - 2 * v + 1]--, a[x - 2 * v + 1]--, a[x + 1]++;
}
for (int i = 1; i <= m; i++) a[i] += a[i - 1];
for (int i = 1; i <= m; i++) a[i] += a[i - 1];
for (int i = 1 + offset; i <= m - offset; i++) cout << a[i] << (i < m ? " " : "");
return 0;
}