提交记录 40001
| 提交时间 |
评测时间 |
| 2026-08-17 05:31:42 |
2026-08-17 05:31:44 |
#include "router.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
// /16 cut + per-slice 256-slot u8 arena (6.3MB, L3-resident) + nh_tab.
// A[65536] u32 = /16 answer (raw nexthop, L2-resident, shallow 62.3%)
// hasDeep bitset + O[65536] u32 (arena offset, read only on deep path)
// arena u8: 0..253 = code, 254 = has L3 (len>=25), 255 = fallback to A[s]
// Deep read arena[O[s]+slot] is u8 (L3) -> nh_tab (L1). 37.7% deep.
static uint32_t *A;
static uint8_t *hasDeep;
static uint32_t *O;
static uint8_t *arena;
static uint8_t *L3;
static uint32_t *l3key;
static uint32_t *l3off;
static uint32_t l3cnt;
static uint32_t nh_tab[256];
static uint32_t nh_keys[256];
static uint8_t nh_codes[256];
static uint32_t ncode_count;
static inline uint8_t nh_code(uint32_t raw){
uint32_t h = (raw * 2654435761u) >> 24;
for(;;){
if (nh_codes[h] == 0){
nh_keys[h] = raw;
ncode_count++;
nh_tab[ncode_count] = raw;
nh_codes[h] = (uint8_t)ncode_count;
return (uint8_t)ncode_count;
}
if (nh_keys[h] == raw) return nh_codes[h];
h = (h + 1) & 255;
}
}
void init(int n, int q, const RoutingTableEntry *a){
(void)q;
memset(nh_codes, 0, sizeof(nh_codes));
ncode_count = 0;
nh_tab[0] = 0;
A = (uint32_t*)calloc(65536, 4);
hasDeep = (uint8_t*)calloc(8192, 1);
O = (uint32_t*)malloc((size_t)65536 * 4);
arena = (uint8_t*)malloc((size_t)65536 * 256);
L3 = (uint8_t*)malloc((size_t)n * 256);
l3key = (uint32_t*)malloc((size_t)n * 4);
l3off = (uint32_t*)malloc((size_t)n * 4);
l3cnt = 0;
uint32_t deep_ordinal = 0;
uint32_t l3cur = 0;
for (int i = 0; i < n; i++) {
uint32_t v = __builtin_bswap32(a[i].addr);
uint32_t len = a[i].len;
uint32_t nh = a[i].nexthop;
if (len <= 16) {
uint32_t shift = 16 - len;
uint32_t count = 1u << shift;
uint32_t mask = count - 1;
uint32_t base = (v >> 16) & ~mask;
uint32_t end = base | mask;
for (uint32_t x = base; x <= end; x++) A[x] = nh;
} else {
uint8_t code = nh_code(nh);
uint32_t s = v >> 16;
uint32_t idx = s >> 3;
uint32_t bit = 1u << (s & 7);
uint32_t o;
if (!(hasDeep[idx] & bit)) {
hasDeep[idx] |= bit;
deep_ordinal++;
o = deep_ordinal * 256;
O[s] = o;
memset(arena + o, 255, 256);
} else {
o = O[s];
}
if (len <= 24) {
uint32_t shift = 24 - len;
uint32_t count = 1u << shift;
uint32_t mask = count - 1;
uint32_t slot = (v >> 8) & 255;
uint32_t base = slot & ~mask;
uint32_t end = base | mask;
for (uint32_t x = base; x <= end; x++) arena[o + x] = code;
} else {
uint32_t slot = (v >> 8) & 255;
uint8_t *e = &arena[o + slot];
if (*e != 254) {
uint32_t l3 = l3cur; l3cur += 256;
memset(L3 + l3, *e, 256);
*e = 254;
l3key[l3cnt] = v >> 8;
l3off[l3cnt] = l3;
l3cnt++;
}
uint32_t l3 = l3off[l3cnt - 1];
uint32_t shift = 32 - len;
uint32_t count = 1u << shift;
uint32_t mask = count - 1;
uint32_t base = v & ~mask;
uint32_t end = base | mask;
for (uint32_t x = base; x <= end; x++) L3[l3 + (x & 255)] = code;
}
}
}
}
static inline uint32_t lookup(uint32_t h){
uint32_t s = h >> 16;
uint32_t ans = A[s];
if (hasDeep[s >> 3] & (1u << (s & 7))) {
uint32_t c = arena[O[s] + ((h >> 8) & 255)];
if (c != 255) {
if (c == 254) {
uint32_t k = h >> 8;
uint32_t lo = 0, hi = l3cnt;
while (lo < hi) {
uint32_t mid = (lo + hi) >> 1;
if (l3key[mid] < k) lo = mid + 1; else hi = mid;
}
uint32_t cc = L3[l3off[lo] + (h & 255)];
ans = (cc == 255) ? A[s] : nh_tab[cc];
} else {
ans = nh_tab[c];
}
}
}
return ans;
}
unsigned query(unsigned addr){ return lookup(__builtin_bswap32(addr)); }
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 38.84 us | 300 KB | Accepted | Score: 25 | 显示更多 |
| Testcase #2 | 10.451 ms | 17 MB + 96 KB | Accepted | Score: 25 | 显示更多 |
| Testcase #3 | 25.493 ms | 17 MB + 96 KB | Accepted | Score: 25 | 显示更多 |
| Testcase #4 | 39.347 ms | 17 MB + 96 KB | Accepted | Score: 25 | 显示更多 |
Judge Duck Online | 评测鸭在线
Server Time: 2026-09-03 20:47:25 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠