1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| #include<bits/stdc++.h> #include<immintrin.h> #define AVX 512
typedef unsigned int u32; typedef unsigned long long u64; using std::cin; using std::cout; using std::endl; using std::min; using std::max;
inline u32 next_integer(u32 x) { x ^= x << 13; x ^= x >> 17; x ^= x << 5; return x; }
inline void output_arr(u32 *a, int blocks) { u32 ret = blocks << 2; u32 x = 23333333; for(u32 i = 0; i < blocks; ++i) { ret = ret ^ (a[i] + x); x ^= x << 13; x ^= x >> 17; x ^= x << 5; } printf("%u\n", ret); }
namespace Sorting { u32 cnt11[2049], cnt22[2049], cnt32[1025]; void init_data(u32 *a, int n, u32 seed) { for(int i=0; i<n; i++) { seed=next_integer(seed), a[i]=seed; ++cnt11[(a[i]&2047)+1], ++cnt22[((a[i]>>11)&2047)+1], ++cnt32[(a[i]>>22)+1]; } } void main() { int n; u32 seed; scanf("%d%u", &n, &seed);
u32 *a = new u32[n+1]; u32 *b = new u32[n+1];
init_data(a, n, seed);
for(u32 *i=cnt11+2; i<cnt11+2048; ++i) *i += *(i-1); for(u32 *i=cnt22+2; i<cnt22+2048; ++i) *i += *(i-1); for(u32 *i=cnt32+2; i<cnt32+1024; ++i) *i += *(i-1); for(u32 i=0; i<n; ++i) b[++cnt11[a[i]&2047]]=a[i]; for(u32 i=1; i<=n; ++i) a[++cnt22[(b[i]>>11)&2047]]=b[i]; for(u32 i=1; i<=n; ++i) b[++cnt32[a[i]>>22]]=a[i];
output_arr(b+1, n); } }
namespace Game { void main() { int n, q; scanf("%d%d", &n, &q);
char *s1 = new char[n + 1]; char *s2 = new char[n + 1]; scanf("%s%s", s1, s2);
u32 *anss = new u32[q];
for(int i=0, x, y, l; i < q; i++) { scanf("%d%d%d", &x, &y, &l); int j=0; anss[i]=0; #if AVX == 512 for(; j+63<l; j+=64) { __m512i sub = _mm512_sub_epi8(_mm512_loadu_si512(s2+y+j), _mm512_loadu_si512(s1+x+j)); anss[i] += _mm_popcnt_u64(_mm512_cmpeq_epu8_mask(sub, _mm512_set1_epi8(1)) | _mm512_cmpeq_epu8_mask(sub, _mm512_set1_epi8(-2))); } #else #if AVX == 256 for(; j+31<l; j+=32) { __m256i sub = _mm256_sub_epi8(_mm256_loadu_si256((__m256i*)(s2+y+j)), _mm256_loadu_si256((__m256i*)(s1+x+j))); anss[i] += _mm_popcnt_u32(_mm256_movemask_epi8(_mm256_cmpeq_epi8(sub, _mm256_set1_epi8(1))) | _mm256_movemask_epi8(_mm256_cmpeq_epi8(sub, _mm256_set1_epi8(-2)))); } #endif #endif for(; j<l; j++) { anss[i] += s1[x+j]=='0' && s2[y+j]=='1' || s1[x+j]=='1' && s2[y+j]=='2' || s1[x+j]=='2' && s2[y+j]=='0'; } }
output_arr(anss, q); } }
namespace Parentheses { void main() { int n; scanf("%d", &n);
char *s = new char[n + 1]; scanf("%s", s);
u32 *dp = new u32[3*n]; dp+=n; dp[0]=1; for(int i=0; i<n; i++) { if(s[i]=='(') { dp--; } else if(s[i]==')') { dp++; } else { dp--; int j=0, lim=min(i+1, n-i-1); #if AVX == 512 for(; j+15<=lim; j+=16) _mm512_storeu_si512(dp+j, _mm512_add_epi32(_mm512_loadu_si512(dp+j), _mm512_loadu_si512(dp+j+2))); #else #if AVX == 256 for(; j+7<=lim; j+=8) _mm256_storeu_si256((__m256i*)(dp+j), _mm256_add_epi32(_mm256_loadu_si256((__m256i*)(dp+j)), _mm256_loadu_si256((__m256i*)(dp+j+2)))); #endif #endif for(; j<=lim; j++) dp[j]+=dp[j+2]; } dp[-1]=0; }
printf("%u\n", dp[0]); } }
int main() { int task_id; scanf("%d", &task_id);
switch(task_id) { case 1: Sorting::main(); break; case 2: Game::main(); break; case 3: Parentheses::main(); break; }
return 0; }
|