正文
增加 5 行删除 5 行
20 行未改动21222324252627282953 行未改动83848586878889909193 行未改动1851861871881891901911921931941951961971981992006 行未改动2072082092102112122132142152 行未改动20 行未改动- 对于一个满足 $x\bmod d=c$ 的数列,把所有的 $x$ 变成 $\dfrac{x-c}{d}$ (因为整形的性质,可以直接除)。这一步称之为归一化,实现了将一个区间的的公差化为 $1$,问题就转化成了加入 $k$ 个数,使区间排序后公差为 $1$。- 对于一个区间 $[L,R]$,算出最少加几个数。53 行未改动```cpp/*Author:loceaner线段树,单调栈*/#include <map>#include <cmath>#include <cstdio>93 行未改动cout << ansl << ' ' << ansr << '\n';return 0;}build(1, 1, n);//枚举右端点for (int i = 1, L = 1; i <= n; i++) {int tmp = L;if (a[i] % d == a[i - 1] % d) L = max(L, last[a[i]] + 1);else L = i;last[a[i]] = i;while (tmp < L) del(1, tmp++);//递增栈while (topl && sl[topl] >= L && a[sl[topl]] >= a[i]) {insert(1, max(L, sl[topl - 1] + 1), sl[topl], a[sl[topl]] / d);topl--;}6 行未改动}insert(1, max(L, sr[topr] + 1), i, a[i] / d);sr[++topr] = i;flag = 0, ans = 0;query(1, L, i, k + i); //思路中的式子if (ansr - ansl < i - ans) ansl = ans, ansr = i;}cout << ansl << " " << ansr << '\n';return 0;2 行未改动20 行未改动21222324252627282953 行未改动83848586878889909193 行未改动1851861871881891901911921931941951961971981992006 行未改动2072082092102112122132142152 行未改动20 行未改动- 对于一个满足 $x\bmod d=c$ 的数列,把所有的 $x$ 变成 $\dfrac{x-c}{d}$ (因为整形的性质,可以直接除)。这一步称之为归一化,实现了将一个区间的的公差化为 $1$,问题就转化成了加入 $k$ 个数,使区间排序后公差为 $1$。- 对于一个区间 $[L,R]$,算出最少加几个数。53 行未改动```cpp/*Author:loceaner线段树,单调栈*/#include <map>#include <cmath>#include <cstdio>93 行未改动cout << ansl << ' ' << ansr << '\n';return 0;}build(1, 1, n);//枚举右端点for (int i = 1, L = 1; i <= n; i++) {int tmp = L;if (a[i] % d == a[i - 1] % d) L = max(L, last[a[i]] + 1);else L = i;last[a[i]] = i;while (tmp < L) del(1, tmp++);//递增栈while (topl && sl[topl] >= L && a[sl[topl]] >= a[i]) {insert(1, max(L, sl[topl - 1] + 1), sl[topl], a[sl[topl]] / d);topl--;}6 行未改动}insert(1, max(L, sr[topr] + 1), i, a[i] / d);sr[++topr] = i;flag = 0, ans = 0;query(1, L, i, k + i); //思路中的式子if (ansr - ansl < i - ans) ansl = ans, ansr = i;}cout << ansl << " " << ansr << '\n';return 0;2 行未改动