我们注意到洛谷支持 avx512f 指令集,而这个指令集支持同时对 个 位有符号整数(即 long long)进行加法、取 和赋值操作。具体地,使用 __m512i 类型,它相当于一个 位的变量,并且可以拆分为 个 位的变量分别操作。
Intel 指令集手册官网。也可以下载离线版本。
void _mm512_storeu_epi64(void *addr, __m512i a)把a中的 位二进制数解释为 个 位二进制数,存入addr[0], addr[1], ..., addr[7]中。__m512i _mm512_loadu_epi64(void *const addr)把addr解释为 位整数数组,将addr[0], addr[1], ..., addr[7]存入返回值中。__m512i _mm512_max_epi64(__m512i a, __m512i b)把a和b解释为 个 位整数,分别取 后存入返回值中。__m512i _mm512_add_epi64(__m512i a, __m512i b)把a和b解释为 个 位整数,分别求和 后存入返回值中。
所以我们使用指令集即可通过本题。指令集效果是常数 。
还要加一点卡常,比如快读,还有 max 数组的复用。不然可能出现这样的尴尬情况。
rec&code
应该能稳过。反正我交了三发都过了。
rec。
// brute force
#pragma GCC target("avx512f")
#include <cstdio>
#include <algorithm>
#include <immintrin.h>
using namespace std;
long long a[100005], mx[100005];
long long mxs[8], mss[8];
char ipb[10000005];
unsigned li = 0;
inline int qread()
{
char ch;
int res = 0;
int flag = 1;
while((ch = ipb[li++]) < '0' || ch > '9') if(ch == '-') flag = -1;
do
{
res = res * 10 + ch - '0';
} while((ch = ipb[li++]) >= '0' && ch <= '9');
return res * flag;
}
inline unsigned uqread()
{
char ch;
unsigned res = 0;
while((ch = ipb[li++]) < '0' || ch > '9');
do
{
res = res * 10 + ch - '0';
} while((ch = ipb[li++]) >= '0' && ch <= '9');
return res;
}
int main()
{
fread(ipb, 1, sizeof ipb, stdin);
int n, q;
// scanf("%d", &n);
n = qread();
// printf("n = %d\n", n); return 0;
for(int i=1;i<=n;i++)
{
// scanf("%lld", a + i);
a[i] = qread();
mx[i] = a[i];
}
// scanf("%d", &q);
q = qread();
// printf("q = %d\n", q); return 0;
mss[0] = mss[1] = mss[2] = mss[3] = mss[4] = mss[5] = mss[6] = mss[7] = -0x3f3f3f3f3f3f3f3f;
while(q--)
{
char op = ipb[li++];
unsigned x = uqread(), y = uqread();
int z;
__m512i ms;
long long mx9 = -0x3f3f3f3f3f3f3f3f;
// printf("%c %u %u\n", op, x, y); fflush(stdout);
switch(op)
{
case 'Q':
ms = _mm512_loadu_epi64(mss);
for(;x+7<=y;x+=8)
{
ms = _mm512_max_epi64(ms, _mm512_loadu_epi64(a + x));
}
for(; x<=y; x++) mx9 = max(mx9, a[x]);
_mm512_storeu_epi64(mxs, ms);
printf("%lld\n", max({mxs[0], mxs[1], mxs[2], mxs[3], mxs[4], mxs[5], mxs[6], mxs[7], mx9}));
break;
case 'A':
ms = _mm512_loadu_epi64(mss);
for(;x+7<=y;x+=8)
{
ms = _mm512_max_epi64(ms, _mm512_loadu_epi64(mx + x));
}
for(; x<=y; x++) mx9 = max(mx9, mx[x]);
_mm512_storeu_epi64(mxs, ms);
printf("%lld\n", max({mxs[0], mxs[1], mxs[2], mxs[3], mxs[4], mxs[5], mxs[6], mxs[7], mx9}));
break;
case 'P':
mxs[0] = mxs[1] = mxs[2] = mxs[3] = mxs[4] = mxs[5] = mxs[6] = mxs[7] = z = qread();
ms = _mm512_loadu_epi64(mxs);
for(;x+7<=y;x+=8)
{
auto res = _mm512_add_epi64(_mm512_loadu_epi64(a + x), ms);
_mm512_storeu_epi64(a + x, res);
_mm512_storeu_epi64(mx + x, _mm512_max_epi64(res, _mm512_loadu_epi64(mx + x)));
}
for(; x<=y; x++) { a[x] += z; mx[x] = max(mx[x], a[x]); }
break;
case 'C':
mxs[0] = mxs[1] = mxs[2] = mxs[3] = mxs[4] = mxs[5] = mxs[6] = mxs[7] = z = qread();
ms = _mm512_loadu_epi64(mxs);
for(;x+7<=y;x+=8)
{
_mm512_storeu_epi64(a + x, ms);
_mm512_storeu_epi64(mx + x, _mm512_max_epi64(ms, _mm512_loadu_epi64(mx + x)));
}
for(; x<=y; x++) { a[x] = z; mx[x] = max(mx[x], a[x]); }
break;
}
}
return 0;
}