跳到正文

剪贴板

剪贴板 a4dw0osp

原文
剪贴板信息

剪贴板信息

收录
更新

指令集参考代码

坑点已列出,P2801,全注释(主要是懒得写中文)

此代码使用 AVX/AVX2 (256bit) 指令集,洛谷支持 AVX512,可以用 mm512,实测优化不大

坑点 11:103103 行,记得设初始值

坑点 22(调了很久):111111 行,因为大于等于 zz 相当于大于 z−1z - 1,不要写成 zz 了,否则 30pts30 \texttt{pts}

C++
// Import the library for instruction set functions.
#include <immintrin.h>
// Allow functions to use instruction set.
#define __AVX__ 1
#define __AVX2__ 1
#define __SSE__ 1
#define __SSE2__ 1
#define __SSE2_MATH__ 1
#define __SSE3__ 1
#define __SSE4_1__ 1
#define __SSE4_2__ 1
#define __SSE_MATH__ 1
#define __SSSE3__ 1
// Allow the compiler to use the target CPU's instruction set.
#pragma GCC target( \
	"sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#include <cstdio>
using namespace std;

// The code for Luogu Problem P2801, using Instruction Set Functions.
// --- 256bit version ---
// Written by dctc800d.
const int N = 1e6 + 32, BIT_LENGTH = 32;
int n, q, x, y, z, st, ed, l[N >> 3], r[N >> 3], bl[N], idx[N];
char s[5];
__m256i a[N >> 3];

// Functions are added attribute "always_inline" to speed up the code.
__attribute__((__gnu_inline__, __always_inline__)) inline void init_block_info() {
	// Generate Block Information.
	int ge = 0;	  // The counter of elements in the current block.
	int blk = 1;  // The current block number.
	const int BLOCK_SIZE = BIT_LENGTH / sizeof(int);  // The size per block.
	for (int i = 1; i <= n; i++) {
		ge++;  // Increment the counter.
		if (ge == BLOCK_SIZE) {
			ge = 0;			 // If reaches a new block, reset the counter.
			r[blk] = i - 1;	 // Record the end of the last block.
			blk++;			 // Increment the block number.
		}
		if (ge == 0) l[blk] = i;  // Record the start of the current block.
		bl[i] = blk;  // Record the block number of the current element.
		idx[i] =
			((i - 1) & (BLOCK_SIZE - 1));	// Record the in-block-index of the current element.
	}
}
__attribute__((__gnu_inline__, __always_inline__)) inline int* arrgetp(int _arr_index) {
	// Get the pointer of the current element in block array.
	return (&((int*)(&a[bl[_arr_index]]))[idx[_arr_index]]);
}
int main() {
	scanf("%d%d", &n, &q);
	init_block_info();
	for (int i = 1; i <= n; i++) scanf("%d", arrgetp(i));
	while (q--) {
		scanf("%s%d%d%d", s, &x, &y, &z);
		if (s[0] == 'M') {
			if (bl[x] == bl[y]) {
				// if x and y are in the same block (1), brute-force.
				for (int i = x; i <= y; i++) (*arrgetp(i)) += z;
				continue;
			}
			// Blockly Add + Brute-force incomplete.
			// Brute-force to process incomplete blocks.
			// Imcomplete block at left.
			for (int i = x; i <= r[bl[x]]; i++) (*arrgetp(i)) += z;
			// Imcomplete block at right.
			for (int i = l[bl[y]]; i <= y; i++) (*arrgetp(i)) += z;
			// Process complete blocks.
			for (int i = bl[x] + 1; i < bl[y]; i++)
				a[i] = _mm256_add_epi32(
					a[i], _mm256_set1_epi32(z));  // Add z to every block.
		} else if (s[0] == 'A') {
			int ans = 0;
			if (bl[x] == bl[y]) {
				// if x and y are in the same block (1), brute-force
				for (int i = x; i <= y; i++)
					if ((*arrgetp(i)) >= z) ans++;
				printf("%d\n", ans);
				continue;
			}
			// Blockly Counting + Brute-force incomplete.
			// Brute-force to process incomplete blocks.
			// Imcomplete block at left.
			for (int i = x; i <= r[bl[x]]; i++)
				if ((*arrgetp(i)) >= z) ans++;
			// Imcomplete block at right.
			for (int i = l[bl[y]]; i <= y; i++)
				if ((*arrgetp(i)) >= z) ans++;
			// Process complete blocks.
			// Function _mm256_cmpgt_epi32 returns 0xffff'ffff
			// if the first element is greater than the second element.
			// We use the mask 0x00000001 to bit-and the return value
			// and add it to result block to calculate the final answer.

			// Don't forgot to set a initial value to the result block!!!!
			__m256i result_block = _mm256_setzero_si256();
			for (int i = bl[x] + 1; i < bl[y]; i++)
				result_block = _mm256_add_epi32(
					result_block,
					_mm256_and_si256(
						_mm256_set1_epi32(0x0000'0001),
						_mm256_cmpgt_epi32(a[i], _mm256_set1_epi32(z - 1)))); // WARNING!!! Don't forgot -1 (if not, 50pts)
			// Get the sum of the result block and add it to the final answer.
			for (int i = 0; i < 8; i++) ans += ((int*)(&result_block))[i];
			printf("%d\n", ans);  // Finally, we did it!!!!!!!!!!
		}
	}
	return 0;
}

如果你真的想看看 512

当然有。但是要手搓 512512 的比较函数了。跟 256256 效率差不多,还不能本地调试,不建议使用。

C++
// Import the library for instruction set functions.
#include <immintrin.h>
// Allow functions to use instruction set.
#define __AVX__ 1
#define __AVX2__ 1
#define __SSE__ 1
#define __SSE2__ 1
#define __SSE2_MATH__ 1
#define __SSE3__ 1
#define __SSE4_1__ 1
#define __SSE4_2__ 1
#define __SSE_MATH__ 1
#define __SSSE3__ 1
// Allow the compiler to use the target CPU's instruction set.
#pragma GCC target( \
	"sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#include <cstdio>
typedef int __m512int __attribute((vector_size(64)));
using namespace std;

// The code for Luogu Problem P2801, using Instruction Set Functions.
// --- 512bit version ---
// Available on luogu (cannot use on local machine)
// Written by dctc800d.
const int N = 1e6 + 64, BIT_LENGTH = 64;
int n, q, x, y, z, st, ed, l[N >> 4], r[N >> 4], bl[N], idx[N];
char s[5];
__m512i a[N >> 4];

// Functions are added attribute "always_inline" to speed up the code.
__attribute__((__gnu_inline__, __always_inline__)) inline void init_block_info() {
	// Generate Block Information.
	int ge = 0;	  // The counter of elements in the current block.
	int blk = 1;  // The current block number.
	const int BLOCK_SIZE = BIT_LENGTH / sizeof(int);  // The size per block.
	for (int i = 1; i <= n; i++) {
		ge++;  // Increment the counter.
		if (ge == BLOCK_SIZE) {
			ge = 0;			 // If reaches a new block, reset the counter.
			r[blk] = i - 1;	 // Record the end of the last block.
			blk++;			 // Increment the block number.
		}
		if (ge == 0) l[blk] = i;  // Record the start of the current block.
		bl[i] = blk;  // Record the block number of the current element.
		idx[i] = ((i - 1) &
				  15);	// Record the in-block-index of the current element.
	}
}
__attribute__((__gnu_inline__, __always_inline__)) inline int* arrgetp(int _arr_index) {
	// Get the pointer of the current element in block array.
	return (&((int*)(&a[bl[_arr_index]]))[idx[_arr_index]]);
}
int main() {
	scanf("%d%d", &n, &q);
	init_block_info();
	for (int i = 1; i <= n; i++) scanf("%d", arrgetp(i));
	while (q--) {
		scanf("%s%d%d%d", s, &x, &y, &z);
		if (s[0] == 'M') {
			if (bl[x] == bl[y]) {
				// if x and y are in the same block (1), brute-force.
				for (int i = x; i <= y; i++) (*arrgetp(i)) += z;
				continue;
			}
			// Blockly Add + Brute-force incomplete.
			// Brute-force to process incomplete blocks.
			// Imcomplete block at left.
			for (int i = x; i <= r[bl[x]]; i++) (*arrgetp(i)) += z;
			// Imcomplete block at right.
			for (int i = l[bl[y]]; i <= y; i++) (*arrgetp(i)) += z;
			// Process complete blocks.
			for (int i = bl[x] + 1; i < bl[y]; i++)
				a[i] = _mm512_add_epi32(
					a[i], _mm512_set1_epi32(z));  // Add z to every block.
		} else if (s[0] == 'A') {
			int ans = 0;
			if (bl[x] == bl[y]) {
				// if x and y are in the same block (1), brute-force
				for (int i = x; i <= y; i++)
					if ((*arrgetp(i)) >= z) ans++;
				printf("%d\n", ans);
				continue;
			}
			// Blockly Counting + Brute-force incomplete.
			// Brute-force to process incomplete blocks.
			// Imcomplete block at left.
			for (int i = x; i <= r[bl[x]]; i++)
				if ((*arrgetp(i)) >= z) ans++;
			// Imcomplete block at right.
			for (int i = l[bl[y]]; i <= y; i++)
				if ((*arrgetp(i)) >= z) ans++;
			// Process complete blocks.
			// Comparison of two __m512i (converted to int) returns 0xffff'ffff
			// if the first element is greater than the second element.
			// We use the mask 0x00000001 to bit-and the return value
			// and add it to result block to calculate the final answer.

			// Don't forgot to set a initial value to the result block!!!!
			__m512i result_block = _mm512_setzero_si512();
			for (int i = bl[x] + 1; i < bl[y]; i++)
				result_block = _mm512_add_epi32(
					result_block,
					_mm512_and_si512(
						_mm512_set1_epi32(0x0000'0001),
						(__m512i)((__m512int)a[i] >
								  (__m512int)_mm512_set1_epi32(z - 1))));
			// Get the sum of the result block and add it to the final answer.
			for (int i = 0; i < 16; i++) ans += ((int*)(&result_block))[i];
			printf("%d\n", ans);  // Finally, we did it!!!!!!!!!!
		}
	}
	return 0;
}

去注释版

256-bit version256 \texttt{-bit version}

C++
#include <immintrin.h>
#define __AVX__ 1
#define __AVX2__ 1
#define __SSE__ 1
#define __SSE2__ 1
#define __SSE2_MATH__ 1
#define __SSE3__ 1
#define __SSE4_1__ 1
#define __SSE4_2__ 1
#define __SSE_MATH__ 1
#define __SSSE3__ 1
#pragma GCC target( \
	"sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#include <cstdio>
using namespace std;
const int N = 1e6 + 32, BIT_LENGTH = 32;
int n, q, x, y, z, st, ed, l[N >> 3], r[N >> 3], bl[N], idx[N];
char s[5];
__m256i a[N >> 3];
__attribute__((__gnu_inline__, __always_inline__)) inline void init_block_info() {
	int ge = 0;
	int blk = 1;
	const int BLOCK_SIZE = BIT_LENGTH / sizeof(int);
	for (int i = 1; i <= n; i++) {
		ge++;
		if (ge == BLOCK_SIZE) {
			ge = 0;
			r[blk] = i - 1;
			blk++;
		}
		if (ge == 0) l[blk] = i;
		bl[i] = blk;
		idx[i] = ((i - 1) & (BLOCK_SIZE - 1));
	}
}
__attribute__((__gnu_inline__, __always_inline__)) inline int* arrgetp(int _arr_index) {
	return (&((int*)(&a[bl[_arr_index]]))[idx[_arr_index]]);
}
int main() {
	scanf("%d%d", &n, &q);
	init_block_info();
	for (int i = 1; i <= n; i++) scanf("%d", arrgetp(i));
	while (q--) {
		scanf("%s%d%d%d", s, &x, &y, &z);
		if (s[0] == 'M') {
			if (bl[x] == bl[y]) {
				for (int i = x; i <= y; i++) (*arrgetp(i)) += z;
				continue;
			}
			for (int i = x; i <= r[bl[x]]; i++) (*arrgetp(i)) += z;
			for (int i = l[bl[y]]; i <= y; i++) (*arrgetp(i)) += z;
			for (int i = bl[x] + 1; i < bl[y]; i++)
				a[i] = _mm256_add_epi32(a[i], _mm256_set1_epi32(z));
		} else if (s[0] == 'A') {
			int ans = 0;
			if (bl[x] == bl[y]) {
				for (int i = x; i <= y; i++)
					if ((*arrgetp(i)) >= z) ans++;
				printf("%d\n", ans);
				continue;
			}
			for (int i = x; i <= r[bl[x]]; i++)
				if ((*arrgetp(i)) >= z) ans++;
			for (int i = l[bl[y]]; i <= y; i++)
				if ((*arrgetp(i)) >= z) ans++;
			__m256i result_block = _mm256_setzero_si256();
			for (int i = bl[x] + 1; i < bl[y]; i++)
				result_block = _mm256_add_epi32(
					result_block,
					_mm256_and_si256(
						_mm256_set1_epi32(0x0000'0001),
						_mm256_cmpgt_epi32(a[i], _mm256_set1_epi32(z - 1))));
			for (int i = 0; i < 8; i++) ans += ((int*)(&result_block))[i];
			printf("%d\n", ans);
		}
	}
	return 0;
}

512-bit version512 \texttt{-bit version}

C++
#include <immintrin.h>
#define __AVX__ 1
#define __AVX2__ 1
#define __SSE__ 1
#define __SSE2__ 1
#define __SSE2_MATH__ 1
#define __SSE3__ 1
#define __SSE4_1__ 1
#define __SSE4_2__ 1
#define __SSE_MATH__ 1
#define __SSSE3__ 1
#pragma GCC target( \
	"sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#include <cstdio>
typedef int __m512int __attribute((vector_size(64)));
using namespace std;
const int N = 1e6 + 64, BIT_LENGTH = 64;
int n, q, x, y, z, st, ed, l[N >> 4], r[N >> 4], bl[N], idx[N];
char s[5];
__m512i a[N >> 4];
__attribute__((__gnu_inline__, __always_inline__)) inline void init_block_info() {
	int ge = 0;
	int blk = 1;
	const int BLOCK_SIZE = BIT_LENGTH / sizeof(int);
	for (int i = 1; i <= n; i++) {
		ge++;
		if (ge == BLOCK_SIZE) {
			ge = 0;
			r[blk] = i - 1;
			blk++;
		}
		if (ge == 0) l[blk] = i;
		bl[i] = blk;
		idx[i] = ((i - 1) & 15);
	}
}
__attribute__((__gnu_inline__, __always_inline__)) inline int* arrgetp(int _arr_index) {
	return (&((int*)(&a[bl[_arr_index]]))[idx[_arr_index]]);
}
int main() {
	scanf("%d%d", &n, &q);
	init_block_info();
	for (int i = 1; i <= n; i++) scanf("%d", arrgetp(i));
	while (q--) {
		scanf("%s%d%d%d", s, &x, &y, &z);
		if (s[0] == 'M') {
			if (bl[x] == bl[y]) {
				for (int i = x; i <= y; i++) (*arrgetp(i)) += z;
				continue;
			}
			for (int i = x; i <= r[bl[x]]; i++) (*arrgetp(i)) += z;
			for (int i = l[bl[y]]; i <= y; i++) (*arrgetp(i)) += z;
			for (int i = bl[x] + 1; i < bl[y]; i++)
				a[i] = _mm512_add_epi32(a[i], _mm512_set1_epi32(z));
		} else if (s[0] == 'A') {
			int ans = 0;
			if (bl[x] == bl[y]) {
				for (int i = x; i <= y; i++)
					if ((*arrgetp(i)) >= z) ans++;
				printf("%d\n", ans);
				continue;
			}
			for (int i = x; i <= r[bl[x]]; i++)
				if ((*arrgetp(i)) >= z) ans++;
			for (int i = l[bl[y]]; i <= y; i++)
				if ((*arrgetp(i)) >= z) ans++;
			__m512i result_block = _mm512_setzero_si512();
			for (int i = bl[x] + 1; i < bl[y]; i++)
				result_block = _mm512_add_epi32(
					result_block,
					_mm512_and_si512(
						_mm512_set1_epi32(0x0000'0001),
						(__m512i)((__m512int)a[i] >
								  (__m512int)_mm512_set1_epi32(z - 1))));
			for (int i = 0; i < 16; i++) ans += ((int*)(&result_block))[i];
			printf("%d\n", ans);
		}
	}
	return 0;
}

附加代码库

  1. 过不去的线段树 2
C++
#include <immintrin.h>
#define __AVX__ 1
#define __AVX2__ 1
#define __SSE__ 1
#define __SSE2__ 1
#define __SSE2_MATH__ 1
#define __SSE3__ 1
#define __SSE4_1__ 1
#define __SSE4_2__ 1
#define __SSE_MATH__ 1
#define __SSSE3__ 1
#pragma GCC target( \
	"sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
#include <cstdio>
typedef long long ll;
using namespace std;
const ll N = 1e6 + 256, BIT_LENGTH = 64;
ll n, q, MOD, opt, x, y, z, st, ed, l[N >> 4], r[N >> 4], bl[N], idx[N];
__m512i a[N >> 4];
__attribute__((__gnu_inline__, __always_inline__)) inline void init_block_info() {
	ll ge = 0ll;
	ll blk = 1ll;
	const ll BLOCK_SIZE = BIT_LENGTH / sizeof(ll);
	for (ll i = 1; i <= n; i++) {
		ge++;
		if (ge == BLOCK_SIZE) {
			ge = 0ll;
			r[blk] = i - 1ll;
			blk++;
		}
		if (ge == 0ll) l[blk] = i;
		bl[i] = blk;
		idx[i] = ((i - 1ll) & 7ll);
	}
}
__attribute__((__gnu_inline__, __always_inline__)) inline ll* arrgetp(ll _arr_index) {
	return &(a[bl[_arr_index]][idx[_arr_index]]);
}
signed main() {
	scanf("%lld%lld%lld", &n, &q, &MOD);
	init_block_info();
	for (ll i = 1ll; i <= n; i++) scanf("%lld", arrgetp(i));
	while (q--) {
		scanf("%lld%lld%lld", &opt, &x, &y);
		if (opt == 2ll) {
			scanf("%lld", &z);
			if (bl[x] == bl[y]) {
				for (ll i = x; i <= y; i++)
					(*arrgetp(i)) = (z + (*arrgetp(i))) % MOD;
				continue;
			}
			for (ll i = x; i <= r[bl[x]]; i++)
				(*arrgetp(i)) = (z + (*arrgetp(i))) % MOD;
			for (ll i = l[bl[y]]; i <= y; i++)
				(*arrgetp(i)) = (z + (*arrgetp(i))) % MOD;
			for (ll i = bl[x] + 1ll; i < bl[y]; i++)
				a[i] = _mm512_add_epi64(a[i], _mm512_set1_epi64(z)) %
					   _mm512_set1_epi64(MOD);
		} else if (opt == 1ll) {
			scanf("%lld", &z);
			if (bl[x] == bl[y]) {
				for (ll i = x; i <= y; i++)
					(*arrgetp(i)) = (z * (*arrgetp(i))) % MOD;
				continue;
			}
			for (ll i = x; i <= r[bl[x]]; i++)
				(*arrgetp(i)) = (z * (*arrgetp(i))) % MOD;
			for (ll i = l[bl[y]]; i <= y; i++)
				(*arrgetp(i)) = (z * (*arrgetp(i))) % MOD;
			for (ll i = bl[x] + 1ll; i < bl[y]; i++)
				a[i] = _mm512_mullo_epi64(a[i], _mm512_set1_epi64(z)) %
					   _mm512_set1_epi64(MOD);
		} else if (opt == 3ll) {
			ll ans = 0ll;
			if (bl[x] == bl[y]) {
				for (ll i = x; i <= y; i++) ans += (*arrgetp(i));
				printf("%lld\n", ans % MOD);
				continue;
			}
			for (ll i = x; i <= r[bl[x]]; i++) ans += (*arrgetp(i));
			for (ll i = l[bl[y]]; i <= y; i++) ans += (*arrgetp(i));
			__m512i result_block = _mm512_setzero_si512();
			for (ll i = bl[x] + 1ll; i < bl[y]; i++)
				result_block = _mm512_add_epi64(result_block, a[i]);
			for (ll i = 0ll; i < 8ll; i++) ans += result_block[i];
			printf("%lld\n", ans % MOD);
		}
	}
	return 0;
}