版本比较
莫队学习笔记
正文
增加 1 行删除 86 行
54 行未改动5556575859606162636465666768697071727374755960616230 行未改动9394959697989910010124 行未改动12612712812914613013113213317 行未改动151152153154172173174155176177178179180181182183184185186187188189190191192193194156157158159125 行未改动全部展开285286287288328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373289290291292103 行未改动全部展开54 行未改动```
参考代码:```cpp#include<bits/stdc++.h>#define int long longusing namespace std;
int read() { int num = 0; bool f = 0; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = 1; c = getchar(); } while (isdigit(c)) num = (num << 3) + (num << 1) + (c ^ 48), c = getchar(); return f ? -num : num;}
void write(int x) { if (x < 0) putchar('-'), write(-x); else if (x < 10) putchar('0' + x); else write(x / 10), putchar('0' + x % 10);}
#define N 50005int n, m, k;int a[N], cnt[N], ans, res[N];int L = 1, R = 0, block;30 行未改动 for (int i = 1; i <= m; ++i) write(res[i]), putchar('\n'); return 0;}```*upd 2026.5.18* 请注意:一定要先加后减,若先缩减的话可能会出现负数长度。
---
### 带修莫队24 行未改动$\frac{qn}{B} + tB^2 = \frac{qn}{2B} + \frac{qn}{2B} + tB^2 \ge \sqrt[3]{\frac{qn}{2B} \cdot \frac{qn}{2B} \cdot tB^2} = \sqrt[3]{\frac{q^2n^2t}{4}}$当且仅当 $\frac{qn}{2B} = tB^2$,即 $B=\sqrt[3]{\frac{qn}{2t}}$ 时取等,此时 $B=O(n^{\frac{1}{3}})$$\because n,t,q$ 同阶$\therefore \sqrt[3]{\frac{q^2n^2t}{4}} = O(n^{\frac{5}{3}})$> **块长推导参考(求导法)**:[DeepSeek 详细推导过程](https://chat.deepseek.com/share/ed2h1mzkfcw5amm71a)
#### 奇偶排序优化带修莫队同样可以应用奇偶优化,且可以分层进行:- 右指针 $r$ 的排序方向由左块奇偶性决定17 行未改动```由上面的推导知道我们的复杂度会变成 $O\left(\dfrac{qn}{B} + tB^2\right)$,但是因为 $nB$ 这一项会比主导项低一阶所以说可以忽略,我们的块长不变 参考代码:```cpp#include <bits/stdc++.h>#define int long longusing namespace std;
int read() { int num = 0; bool f = 0; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = 1; c = getchar(); } while (isdigit(c)) num = (num << 3) + (num << 1) + (c ^ 48), c = getchar(); return f ? -num : num;}
void write(int x) { if (x < 0) putchar('-'), write(-x); else if (x < 10) putchar('0' + x); else write(x / 10), putchar('0' + x % 10);}
char rdc() { char c = getchar(); while (c == ' ' || c == '\n') c = getchar(); return c;}
#define N 150005int n, m;int cnt[1000005], a[N], hanser[N], ans;int l = 1, r = 0, now = 0;125 行未改动全部展开 这样就是比较清晰的了,每个区间都是:右端点移动,左端点移动得到答案,回滚左端点。 对于模板题我们写出代码: ```cpp#include <bits/stdc++.h>#define debug puts("-1")#define il inline#define kg putchar(' ')#define hh putchar('\n')#define int long long#ifdef int#define inf 0x3f3f3f3f3f3f3f3f#else#define inf 0x3f3f3f3f#endif#define db double#define fir first#define sec second#define pb push_back#define pii pair<int, int>#define max(a, b) (a > b ? a : b)#define min(a, b) (a < b ? a : b)#define ulp(i, a, b) for (int i = a; i <= b; ++i)#define dlp(i, a, b) for (int i = a; i >= b; --i)#define mod 1000000007#define Zero (void)0#define ci const int &#define lowbit(x) (x & (-x))using namespace std;int read() { int num = 0; bool f = 0; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = 1; c = getchar(); } while (isdigit(c)) num = (num << 3) + (num << 1) + (c ^ 48), c = getchar(); return f ? -num : num;}void write(int x) { if (x < 0) putchar('-'), write(-x); else if (x < 10) putchar('0' + x); else write(x / 10), putchar('0' + x % 10);}char rdc() { char c = getchar(); while (c == ' ' || c == '\n') c = getchar(); return c;}#define N 200005int a[N];int pre[N];int suf[N];103 行未改动全部展开