版本比较
水题技巧之:如果你做莫队题时不会标准根号复杂度……
正文
增加 20 行删除 20 行
40 行未改动4142434445454647484942 行未改动9293949596969798991001011021031031041051061071081081091101111121131131141151151161171181193 行未改动12312412512612712712812913013113213213313413513613713713813914014118 行未改动1601611621631641641651661671687 行未改动1761771781791801801811821831831841851861879 行未改动1971981992002012012022032042052052062072082092102102112122132132142152162179 行未改动2272282292302312312322332342352362362372382382392402412428 行未改动25125225325425525525625740 行未改动#define en_ putchar_unlocked('\n')#define e_ putchar_unlocked(' ')// #define int long longusing namespace std;inline int in() { inline int in() { int n = 0; char p = getchar_unlocked(); while (p < '-') p = getchar_unlocked(); bool f = p == '-' ? p = getchar_unlocked() : 0; do n = n * 10 + (p ^ 48), p = getchar_unlocked();42 行未改动
int ans[N];
signed main() { #ifndef ONLINE_JUDGE #ifndef ONLINE_JUDGE freopen("in.ru", "r", stdin); freopen("out.ru", "w", stdout); #endif in(n); for(int i = 1; i <= n; i ++) hs[i] = in(a[i]); for(int i = 1; i <= n; i ++) in(q[i].l), in(q[i].r), q[i].id = i, pos[i] = (i - 1) / B +1;
for(int i = 1; R[i - 1] != n; i ++) { L[i] = (i - 1) * K + 1, R[i] = min(i * K, n); for(int j = L[i]; j <= R[i]; j ++) bel[j] = i; } sort(q + 1, q + 1 + n, [](const query &a, const query &b) sort(q + 1, q + 1 + n, [](const query &a, const query &b) { return pos[a.r] == pos[b.r] ? pos[a.r] & 1 ? a.l < b.l : a.l > b.l : pos[a.r] < pos[b.r];});
sort(hs + 1, hs + 1 + n); int len = unique(hs + 1, hs + 1+ n) - hs - 1; for(int i = 1; i <= n; i ++) for(int i = 1; i <= n; i ++) a[i] = lower_bound(hs + 1, hs + len + 1, a[i]) - hs;
for(int i = 1, l = 1, r = 0, MX, ID; i <= n; i ++) { while(l > q[i].l) -- l, rt[bel[a[l]]].update(a[l]); while(r < q[i].r) ++ r, rt[bel[a[r]]].update(a[r]); while(l < q[i].l) rt[bel[a[l]]].reduce(a[l]), ++ l;3 行未改动 if(rt[i].mx[1] > MX) { MX = rt[i].mx[1]; ID = rt[i].id[1]; } } } ans[q[i].id] = hs[ID]; }
for(int i = 1; i <= n; i ++) out(ans[i]), en_;} }// 星間~ 干渉~ 融解~ 輪迴~ 邂逅~ 再生~ ララバイ~```
### Part II:个位数的块长,$O(n m^{\frac {2}{3}})$
这个倒是听说过呢。
如上所言,一般分块和莫队搭档都是十分好的,但是有时候,你发现:
18 行未改动#include <bits/stdc++.h>#define en_ putchar_unlocked('\n')#define e_ putchar_unlocked(' ')using namespace std;template<typename T> inline T in() { template<typename T> inline T in() { T n = 0; char p = getchar_unlocked(); while (p < '-') p = getchar_unlocked(); bool f = p == '-' ? p = getchar_unlocked() : 0; do n = n * 10 + (p ^ 48), p = getchar_unlocked();7 行未改动 putchar_unlocked(n % 10 + '0');}const int N = 3e5 + 10, B = 577, B2 = 64, B3 = 8;using pii = pair<int, int>;
int a[N], n, L1[N], R1[N], bel1[N], L2[N], R2[N], bel2[N];int hs[N];
struct query { int l, r, id;} q[N];int pos[N];9 行未改动 if(cnt[i] > mx2[id]) { mx2[id] = cnt[i]; id2[id] = i; } } } int idl = (idp - 1) * B3 + 1; int idr = min(bel2[n], idl + B3 - 1); mx1[idp] = 0; for(int i = idl; i <= idr; i ++) { for(int i = idl; i <= idr; i ++) { if(mx2[i] > mx1[idp]){ mx1[idp] = mx2[i]; id1[idp] = id2[i]; } } }}signed main() { #ifndef ONLINE_JUDGE #ifndef ONLINE_JUDGE freopen("in.ru", "r", stdin); freopen("out.ru", "w", stdout); #endif in(n);9 行未改动 L2[i] = (i - 1) * B3 + 1, R2[i] = min(i * B3, n); for(int j = L2[i]; j <= R2[i]; j ++) bel2[j] = i; }
sort(q + 1, q + 1 + n, [](query a, query b) sort(q + 1, q + 1 + n, [](query a, query b) { return pos[a.l] == pos[b.l] ? pos[a.l] & 1 ? a.r < b.r : a.r > b.r : pos[a.l] < pos[b.l];});
sort(hs + 1, hs + 1 + n); int len = unique(hs + 1, hs + 1+ n) - hs - 1; for(int i = 1; i <= n; i ++) for(int i = 1; i <= n; i ++) a[i] = lower_bound(hs + 1, hs + len + 1, a[i]) - hs;
for(int i = 1, l = 1, r = 0, mx, id; i <= n; i ++) { while(l < q[i].l) upd(a[l], -1), l ++; while(l > q[i].l) l --, upd(a[l], 1); while(r < q[i].r) r ++, upd(a[r], 1);8 行未改动 ans[q[i].id] = hs[id]; }
for(int i = 1; i <= n; i ++) out(ans[i]), en_;} }// 星間~ 干渉~ 融解~ 輪迴~ 邂逅~ 再生~ ララバイ~```