如何 n^2 过半百万:题解 P4117 【[Ynoi2018]五彩斑斓的世界】
w33z8kqrqk8zzzx33 · · 题解
介绍一个目前可以稳稳的过的一个暴力做法,
前置:
- AVX512 指令集
- 洛谷评测机构造
- 逐块处理优化缓存
- 模拟多线优化寄存器
人人都应该知道裸的指令集写法?
signed main() {
int n, m; n = itrr; m = itrr;
rep(i, n) ar(i) = itrr;
const __m512i alignas(512) onemask = _mm512_set1_epi32(1);
while(m--) {
int o, l, r, x; o = itrr; l = itrr; r = itrr; x = itrr;
l--; r--;
int lc = l>>LOGKDS, rc = r>>LOGKDS;
if(o == 1) {
if(lc == rc) iter(i, l, r+1) ar(i) -= (ar(i) > x ? x : 0);
else {
while(l&(KDS-1)) { ar(l) -= (ar(l) > x ? x : 0); l++; }
while((0xffff^r)&(KDS-1)) { ar(r) -= (ar(r) > x ? x : 0); r--; }
lc = l>>LOGKDS, rc = r>>LOGKDS;
const __m512i alignas(512) s = _mm512_set1_epi32(x);
iter(i, lc, rc+1)
_ar[i].d = _mm512_mask_sub_epi32(_ar[i].d, _mm512_cmpgt_epi32_mask(_ar[i].d, s), _ar[i].d, s);
}
} else {
kuai alignas(512) ans; ans.d = _mm512_set1_epi32(0);
if(lc == rc) iter(i, l, r+1) ans.dat[0] += (ar(i) == x);
else {
while(l&(KDS-1)) { ans.dat[0] += (ar(l) == x); l++; }
while((0xffff^r)&(KDS-1)) { ans.dat[0] += (ar(r) == x); r--; }
lc = l>>LOGKDS, rc = r>>LOGKDS;
const __m512i alignas(512) s = _mm512_set1_epi32(x);
iter(i, lc, rc+1)
ans.d = _mm512_mask_add_epi32(ans.d,_mm512_cmpeq_epi32_mask(_ar[i].d,s),ans.d,onemask);
}
int tot = 0;
rep(i, KDS) tot += ans.dat[i];
std::printf("%d\n", tot);
}
}
}
然后提交上去得到 5 ~ 30 分的好成绩,考虑如何优化。
一整个数组只能塞入 L3 缓存,然后 L3 缓存是所有洛谷评测线公用的,所以极其慢。
根据 lxl 的提示,在暴力里面也可以用到逐块处理。
显然操作独立,按照
然后我们发现每一次遍历这个 L2 缓存没有充分的用系统的
捆绑两个操作得到 50~55 分的好成绩。
把
然后就愉快的过啦,也并没有跑到最劣解,似乎 2,3,4,5,6,7,11,12,13,14 这些测试点吊着打分块。
大声吼一下 simonlindholm 天下第一!
最终代码:
#pragma GCC optimize("Ofast")
#pragma GCC target ("avx512f,avx512bw,avx512dq,avx512vl")
// writer: w33z8kqrqk8zzzx33
#include <cstdio>
#include <utility>
#include <cassert>
#include <cstring>
#include <algorithm>
#include <sys/mman.h>
#include <immintrin.h>
struct _io {
char* s;
_io() : s((char*)mmap(0, 1 << 28, PROT_READ, MAP_PRIVATE, fileno(stdin), 0)) {}
operator int() {
int x = 0;
while (*s < 48) s++;
while (*s > 32) x = x * 10 + ((*s++) & 15);
return x;
}
} itrr;
using std::swap;
// begin fast read template by CYJian (source: https://www.luogu.com.cn/paste/i11c3ppx)
namespace io {
const int __SIZE = (1 << 21) + 1;
char ibuf[__SIZE], *iS, *iT, obuf[__SIZE], *oS = obuf, *oT = oS + __SIZE - 1, __c, qu[55]; int __f, qr, _eof;
#define Gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
inline void flush () { fwrite (obuf, 1, oS - obuf, stdout), oS = obuf; }
inline void gc (char &x) { x = Gc(); }
inline void pc (char x) { *oS ++ = x; if (oS == oT) flush (); }
inline void pstr (const char *s) { int __len = strlen(s); for (__f = 0; __f < __len; ++__f) pc (s[__f]); }
inline void gstr (char *s) { for(__c = Gc(); __c < 32 || __c > 126 || __c == ' ';) __c = Gc();
for(; __c > 31 && __c < 127 && __c != ' '; ++s, __c = Gc()) *s = __c; *s = 0; }
template <class I> inline bool gi (I &x) { _eof = 0;
for (__f = 1, __c = Gc(); (__c < '0' || __c > '9') && !_eof; __c = Gc()) { if (__c == '-') __f = -1; _eof |= __c == EOF; }
for (x = 0; __c <= '9' && __c >= '0' && !_eof; __c = Gc()) x = x * 10 + (__c & 15), _eof |= __c == EOF; x *= __f; return !_eof; }
template <class I> inline void print (I x) { if (!x) pc ('0'); if (x < 0) pc ('-'), x = -x;
while (x) qu[++ qr] = x % 10 + '0', x /= 10; while (qr) pc (qu[qr --]); }
struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
} using io::pc; using io::gc; using io::pstr; using io::gstr; using io::gi; using io::print;
// end fast read template by CYJian
#define iter(i, a, b) for(int i=(a); i<(b); i++)
#define rep(i, a) iter(i, 0, a)
#define rep1(i, a) iter(i, 1, (a)+1)
#define log(a) cerr<<"\033[32m[DEBUG] "<<#a<<'='<<(a)<<" @ line "<<__LINE__<<"\033[0m"<<endl
#define all(a) a.begin(), a.end()
#define fi first
#define se second
#define pb push_back
#define mp make_pair
using ll=long long;
//#define int ll
const int MOD = 1000000007;
#define KDS 16
#define LOGKDS 4
#define cock 1984
union kuai {
__m512i d;
int dat[16];
};
kuai Bar[31255];
kuai _ar[cock];
#define Barr(i) (Bar[(i)>>LOGKDS].dat[(i)&(KDS-1)])
#define ar(i) (_ar[(i)>>LOGKDS].dat[(i)&(KDS-1)])
#define op1(s, b) b.d = _mm512_mask_sub_epi32(b.d, _mm512_cmpgt_epi32_mask(b.d, s), b.d, s);
#define op2(s, r, b) r.d = _mm512_mask_add_epi32(r.d, _mm512_cmpeq_epi32_mask(b.d, s), r.d, onemask);
#define redc(s) (s.dat[0]+s.dat[1]+s.dat[2]+s.dat[3]+s.dat[4]+s.dat[5]+s.dat[6]+s.dat[7]+s.dat[8]+s.dat[9]+s.dat[10]+s.dat[11]+s.dat[12]+s.dat[13]+s.dat[14]+s.dat[15])
struct tt { int i, s; };
void r1(int l, int r, tt& x1) {
const __m512i onemask = _mm512_set1_epi32(1);
const __m512i s1 = _mm512_set1_epi32(x1.s);
if(x1.i == 1) {
iter(i, l, r) op1(s1, _ar[i])
x1.i = -1;
} else {
kuai r1; r1.d = _mm512_set1_epi32(0);
iter(i, l, r) op2(s1, r1, _ar[i])
x1.i = redc(r1);
}
}
void r2(int l, int r, tt& x1, tt& x2) {
const __m512i onemask = _mm512_set1_epi32(1);
const __m512i s1 = _mm512_set1_epi32(x1.s);
const __m512i s2 = _mm512_set1_epi32(x2.s);
if(x1.i == 1) {
if(x2.i == 1) { iter(i, l, r) { op1(s1, _ar[i]) op1(s2, _ar[i]) } x2.i = -1; }
else { kuai r2; r2.d = _mm512_set1_epi32(0); iter(i, l, r) { op1(s1, _ar[i]) op2(s2, r2, _ar[i]) } x2.i = redc(r2); }
x1.i = -1;
} else {
kuai r1; r1.d = _mm512_set1_epi32(0);
if(x2.i == 1) { iter(i, l, r) { op2(s1, r1, _ar[i]) op1(s2, _ar[i]) } x2.i = -1; }
else { kuai r2; r2.d = _mm512_set1_epi32(0); iter(i, l, r) { op2(s1, r1, _ar[i]) op2(s2, r2, _ar[i]) } x2.i = redc(r2); }
x1.i = redc(r1);
}
}
void r3(int l, int r, tt& x1, tt& x2, tt& x3) {
const __m512i onemask = _mm512_set1_epi32(1);
const __m512i s1 = _mm512_set1_epi32(x1.s);
const __m512i s2 = _mm512_set1_epi32(x2.s);
const __m512i s3 = _mm512_set1_epi32(x3.s);
if(x1.i == 1) {
if(x2.i == 1) {
if(x3.i == 1) {
iter(i, l, r) { op1(s1, _ar[i]) op1(s2, _ar[i]) op1(s3, _ar[i]) }
x3.i = -1;
}
else {
kuai r3; r3.d = _mm512_set1_epi32(0);
iter(i, l, r) { op1(s1, _ar[i]) op1(s2, _ar[i]) op2(s3, r3, _ar[i]) }
x3.i = redc(r3);
}
x2.i = -1;
}
else {
kuai r2; r2.d = _mm512_set1_epi32(0);
if(x3.i == 1) {
iter(i, l, r) { op1(s1, _ar[i]) op2(s2, r2, _ar[i]) op1(s3, _ar[i]) }
x3.i = -1;
}
else {
kuai r3; r3.d = _mm512_set1_epi32(0);
iter(i, l, r) { op1(s1, _ar[i]) op2(s2, r2, _ar[i]) op2(s3, r3, _ar[i]) }
x3.i = redc(r3);
}
x2.i = redc(r2);
}
x1.i = -1;
} else {
kuai r1; r1.d = _mm512_set1_epi32(0);
if(x2.i == 1) {
if(x3.i == 1) {
iter(i, l, r) { op2(s1, r1, _ar[i]) op1(s2, _ar[i]) op1(s3, _ar[i]) }
x3.i = -1;
}
else {
kuai r3; r3.d = _mm512_set1_epi32(0);
iter(i, l, r) { op2(s1, r1, _ar[i]) op1(s2, _ar[i]) op2(s3, r3, _ar[i]) }
x3.i = redc(r3);
}
x2.i = -1;
}
else {
kuai r2; r2.d = _mm512_set1_epi32(0);
if(x3.i == 1) {
iter(i, l, r) { op2(s1, r1, _ar[i]) op2(s2, r2, _ar[i]) op1(s3, _ar[i]) }
x3.i = -1;
}
else {
kuai r3; r3.d = _mm512_set1_epi32(0);
iter(i, l, r) { op2(s1, r1, _ar[i]) op2(s2, r2, _ar[i]) op2(s3, r3, _ar[i]) }
x3.i = redc(r3);
}
x2.i = redc(r2);
}
x1.i = redc(r1);
}
}
struct triplet { int a, b, c; };
struct op { int o, l, r, x; };
int brute(int o, int l, int r, int x) {
const __m512i onemask = _mm512_set1_epi32(1);
const __m512i s = _mm512_set1_epi32(x);
int lc = l>>LOGKDS, rc = r>>LOGKDS;
if(o == 1) {
if(lc == rc) iter(i, l, r+1) ar(i) -= (ar(i) > x ? x : 0);
else {
while(l&(KDS-1)) { ar(l) -= (ar(l) > x ? x : 0); l++; }
while((0xffff^r)&(KDS-1)) { ar(r) -= (ar(r) > x ? x : 0); r--; }
lc = l>>LOGKDS, rc = r>>LOGKDS;
iter(i, lc, rc+1)
_ar[i].d = _mm512_mask_sub_epi32(_ar[i].d, _mm512_cmpgt_epi32_mask(_ar[i].d, s), _ar[i].d, s);
}
return 0;
} else {
kuai ans; ans.d = _mm512_set1_epi32(0);
if(lc == rc) iter(i, l, r+1) ans.dat[0] += (ar(i) == x);
else {
while(l&(KDS-1)) { ans.dat[0] += (ar(l) == x); l++; }
while((0xffff^r)&(KDS-1)) { ans.dat[0] += (ar(r) == x); r--; }
lc = l>>LOGKDS, rc = r>>LOGKDS;
iter(i, lc, rc+1)
ans.d = _mm512_mask_add_epi32(ans.d,_mm512_cmpeq_epi32_mask(_ar[i].d,s),ans.d,onemask);
}
return redc(ans);
}
}
std::pair<int, int> lockstep(op x1, op x2) {
if(x1.l == x2.l && x1.r == x2.r && x1.l % 16 == 0 && x1.r % 16 == 15) {
tt a = {x1.o, x1.x}, b = {x2.o, x2.x};
r2(x1.l/16, (x1.r+1)/16, a, b);
return {std::max(0, a.i), std::max(0, b.i)};
}
int x1a = -(int)(x1.o==1), x2a = -(int)(x2.o==1);
auto SpecialCheckForChunk = [&](int cid) {
int bg = cid * 16;
rep(i, 16) {
if(x1.l <= bg+i && bg+i <= x1.r) {
if(x1.o == 1) ar(bg+i) -= (ar(bg+i) > x1.x ? x1.x : 0);
else x1a += (ar(bg+i) == x1.x);
}
if(x2.l <= bg+i && bg+i <= x2.r) {
if(x2.o == 1) ar(bg+i) -= (ar(bg+i) > x2.x ? x2.x : 0);
else x2a += (ar(bg+i) == x2.x);
}
}
};
int nch[4] = {x1.l / 16, x1.r/16, x2.l / 16, x2.r/16};
if(nch[0] > nch[1]) swap(nch[0], nch[1]);
if(nch[1] > nch[2]) swap(nch[1], nch[2]);
if(nch[2] > nch[3]) swap(nch[2], nch[3]);
if(nch[0] > nch[1]) swap(nch[0], nch[1]);
if(nch[1] > nch[2]) swap(nch[1], nch[2]);
if(nch[0] > nch[1]) swap(nch[0], nch[1]);
SpecialCheckForChunk(nch[0]);
rep(i, 3) {
if(nch[i] == nch[i+1]) continue;
if(nch[i]+1 != nch[i+1]) {
int reprs = (nch[i]+1)*16;
tt op1 = {x1.o, x1.x};
tt op2 = {x2.o, x2.x};
if(x1.l <= reprs && reprs <= x1.r) {
if(x2.l <= reprs && reprs <= x2.r) {
r2(nch[i]+1, nch[i+1], op1, op2);
if(x1a != -1) x1a += op1.i;
if(x2a != -1) x2a += op2.i;
} else {
r1(nch[i]+1, nch[i+1], op1);
if(x1a != -1) x1a += op1.i;
}
} else {
if(x2.l <= reprs && reprs <= x2.r) {
r1(nch[i]+1, nch[i+1], op2);
if(x2a != -1) x2a += op2.i;
}
}
}
SpecialCheckForChunk(nch[i+1]);
}
return {std::max(0, x1a), std::max(0, x2a)};
}
triplet lockstep(op x1, op x2, op x3) {
if(x1.l == x2.l && x1.r == x2.r && x1.l == x3.l && x1.r == x3.r && x1.l % 16 == 0 && x1.r % 16 == 15 ) {
tt a = {x1.o, x1.x}, b = {x2.o, x2.x}, c = {x3.o, x3.x};
r3(x1.l/16, (x1.r+1)/16, a, b, c);
return {std::max(0, a.i), std::max(0, b.i), std::max(0, c.i)};
}
int x1a = -(int)(x1.o==1), x2a = -(int)(x2.o==1), x3a = -(int)(x3.o==1);
auto SpecialCheckForChunk = [&](int cid) {
int bg = cid * 16;
rep(i, 16) {
if(x1.l <= bg+i && bg+i <= x1.r) {
if(x1.o == 1) ar(bg+i) -= (ar(bg+i) > x1.x ? x1.x : 0);
else x1a += (ar(bg+i) == x1.x);
}
if(x2.l <= bg+i && bg+i <= x2.r) {
if(x2.o == 1) ar(bg+i) -= (ar(bg+i) > x2.x ? x2.x : 0);
else x2a += (ar(bg+i) == x2.x);
}
if(x3.l <= bg+i && bg+i <= x3.r) {
if(x3.o == 1) ar(bg+i) -= (ar(bg+i) > x3.x ? x3.x : 0);
else x3a += (ar(bg+i) == x3.x);
}
}
};
int nch[6] = {x1.l / 16, x1.r/16, x2.l / 16, x2.r/16, x3.l / 16, x3.r/16};
std::sort(nch, nch+6); // TODO: expand
SpecialCheckForChunk(nch[0]);
rep(i, 5) {
if(nch[i] == nch[i+1]) continue;
if(nch[i]+1 != nch[i+1]) {
int reprs = (nch[i]+1)*16;
tt op1 = {x1.o, x1.x};
tt op2 = {x2.o, x2.x};
tt op3 = {x3.o, x3.x};
if(x1.l <= reprs && reprs <= x1.r) {
if(x2.l <= reprs && reprs <= x2.r) {
if(x3.l <= reprs && reprs <= x3.r) {
r3(nch[i]+1, nch[i+1], op1, op2, op3);
if(x1a != -1) x1a += op1.i;
if(x2a != -1) x2a += op2.i;
if(x3a != -1) x3a += op3.i;
} else {
r2(nch[i]+1, nch[i+1], op1, op2);
if(x1a != -1) x1a += op1.i;
if(x2a != -1) x2a += op2.i;
}
} else {
if(x3.l <= reprs && reprs <= x3.r) {
r2(nch[i]+1, nch[i+1], op1, op3);
if(x1a != -1) x1a += op1.i;
if(x3a != -1) x3a += op3.i;
} else {
r1(nch[i]+1, nch[i+1], op1);
if(x1a != -1) x1a += op1.i;
}
}
} else {
if(x2.l <= reprs && reprs <= x2.r) {
if(x3.l <= reprs && reprs <= x3.r) {
r2(nch[i]+1, nch[i+1], op2, op3);
if(x2a != -1) x2a += op2.i;
if(x3a != -1) x3a += op3.i;
} else {
r1(nch[i]+1, nch[i+1], op2);
if(x2a != -1) x2a += op2.i;
}
} else {
if(x3.l <= reprs && reprs <= x3.r) {
r1(nch[i]+1, nch[i+1], op3);
if(x3a != -1) x3a += op3.i;
}
}
}
}
SpecialCheckForChunk(nch[i+1]);
}
return {std::max(0, x1a), std::max(0, x2a), std::max(0, x3a)};
}
op q[500005];
int ans[500005];
void mainloopBruteForce(int chunkid, int n, int m) {
int blockbegin = chunkid * 1901;
int blockend = (chunkid + 1) * 1901 - 1;
int L = blockbegin * 16;
int R = (blockend + 1) * 16 - 1;
iter(i, blockbegin, blockend + 1) _ar[i-blockbegin] = Bar[i];
rep(i, m) {
if(q[i].r < L || R < q[i].l) continue;
ans[i] += brute(q[i].o, std::max(0, q[i].l-L), std::min(R, q[i].r)-L, q[i].x);
}
}
void mainloopLockstep(int chunkid, int n, int m) {
int blockbegin = chunkid * cock;
int blockend = (chunkid + 1) * cock - 1;
int L = blockbegin * 16;
int R = (blockend + 1) * 16 - 1;
iter(i, blockbegin, blockend + 1) _ar[i-blockbegin] = Bar[i];
int po = -1, pl, pr, px, id;
rep(i, m) {
if(q[i].r < L || R < q[i].l) continue;
if(po == -1) {
po = q[i].o, pl = std::max(0, q[i].l-L), pr = std::min(R, q[i].r)-L, px = q[i].x, id = i;
} else {
auto [r1, r2] = lockstep({po, pl, pr, px}, {q[i].o, std::max(0, q[i].l-L), std::min(R, q[i].r)-L, q[i].x});
ans[id] += r1; ans[i] += r2;
po = -1;
}
}
if(po != -1) ans[id] += brute(po, pl, pr, px);
}
void mainloopLockstep3(int chunkid, int n, int m) {
int blockbegin = chunkid * cock;
int blockend = (chunkid + 1) * cock - 1;
int L = blockbegin * 16;
int R = (blockend + 1) * 16 - 1;
iter(i, blockbegin, blockend + 1) _ar[i-blockbegin] = Bar[i];
int po = -1, pl, pr, px, id;
int Po = -1, Pl, Pr, Px, Id;
rep(i, m) {
if(q[i].r < L || R < q[i].l) continue;
if(po == -1) {
po = q[i].o, pl = std::max(0, q[i].l-L), pr = std::min(R, q[i].r)-L, px = q[i].x, id = i;
} else {
if(Po == -1) {
Po = q[i].o, Pl = std::max(0, q[i].l-L), Pr = std::min(R, q[i].r)-L, Px = q[i].x, Id = i;
} else {
auto [r1, r2, r3] =
lockstep({po, pl, pr, px}, {Po, Pl, Pr, Px}, {q[i].o, std::max(0, q[i].l-L), std::min(R, q[i].r)-L, q[i].x});
ans[id] += r1; ans[Id] += r2; ans[i] += r3;
po = -1; Po = -1;
}
}
}
if(Po != -1) {
auto [r1, r2] = lockstep({po, pl, pr, px}, {Po, Pl, Pr, Px});
ans[id] += r1; ans[Id] += r2;
} else if(po != -1) ans[id] += brute(po, pl, pr, px);
}
signed main() {
assert((long long)(_ar)%64==0);
int n, m; n = itrr; m = itrr;
rep(i, n) Barr(i) = itrr;
rep(i, m) {
q[i].o = itrr;
q[i].l = itrr-1;
q[i].r = itrr-1;
q[i].x = itrr;
}
int chunkid = 0;
while(chunkid * cock * 16 < n) {
mainloopLockstep3(chunkid, n, m);
chunkid++;
}
rep(i, m) if(q[i].o == 2) print(ans[i]), pc('\n');
}