提交记录 39998


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_dsh_260814 router32. 测测你的路由器 Accepted 100 42.459 ms 29668 KB C++ 3.88 KB
提交时间 评测时间
2026-08-17 05:23:31 2026-08-17 05:23:34
#include "router.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

// flat /20 fill: A[2^20] u32 (4MB, raw nexthop, 87.4% shallow) + hasDeep bitset
// + O[2^20] u32 (deep offset) + per-/20-slice 16-slot u32 arena (len 21..24).
// len>=25: arena slot = SENTINEL -> per-/24 256-u32 L3 (binary search, rare).
#define SENTINEL 0xFFFFFFFFu

static uint32_t *A;
static uint8_t  *hasDeep;
static uint32_t *O;
static uint32_t *arena;
static uint32_t *L3;
static uint32_t *l3key;
static uint32_t *l3off;
static uint32_t l3cnt;

void init(int n, int q, const RoutingTableEntry *a){
    (void)q;
    A = (uint32_t*)calloc(1u << 20, 4);
    hasDeep = (uint8_t*)calloc((1u << 20) >> 3, 1);
    O = (uint32_t*)malloc((size_t)(1u << 20) * 4);
    arena = (uint32_t*)malloc((size_t)(1u << 20) * 16 * 4);
    L3 = (uint32_t*)malloc((size_t)n * 256 * 4);
    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 <= 20) {
            uint32_t shift = 20 - len;
            uint32_t count = 1u << shift;
            uint32_t mask = count - 1;
            uint32_t base = (v >> 12) & ~mask;
            uint32_t end = base | mask;
            for (uint32_t x = base; x <= end; x++) A[x] = nh;
        } else {
            uint32_t s = v >> 12;
            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 * 16;
                O[s] = o;
                uint64_t dvd = ((uint64_t)A[s] << 32) | A[s];
                uint64_t *pp = (uint64_t*)(arena + o);
                for (uint32_t c = 0; c < 8; c++) pp[c] = dvd;
            } 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) & 15;
                uint32_t base = slot & ~mask;
                uint32_t end = base | mask;
                for (uint32_t x = base; x <= end; x++) arena[o + x] = nh;
            } else {
                uint32_t slot = (v >> 8) & 15;
                uint32_t *e = &arena[o + slot];
                if (*e != SENTINEL) {
                    uint32_t l3 = l3cur; l3cur += 256;
                    uint32_t dv = *e;
                    uint64_t dvd = ((uint64_t)dv << 32) | dv;
                    uint64_t *pp = (uint64_t*)(L3 + l3);
                    for (uint32_t c = 0; c < 128; c++) pp[c] = dvd;
                    *e = SENTINEL;
                    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)] = nh;
            }
        }
    }
}

static inline uint32_t lookup(uint32_t h){
    uint32_t s = h >> 12;
    uint32_t ans = A[s];
    if (hasDeep[s >> 3] & (1u << (s & 7))) {
        ans = arena[O[s] + ((h >> 8) & 15)];
        if (ans == SENTINEL) {
            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;
            }
            ans = L3[l3off[lo] + (h & 255)];
        }
    }
    return ans;
}

unsigned query(unsigned addr){ return lookup(__builtin_bswap32(addr)); }

CompilationN/AN/ACompile OKScore: N/A

Testcase #1355.09 us4 MB + 168 KBAcceptedScore: 25

Testcase #211.532 ms28 MB + 996 KBAcceptedScore: 25

Testcase #327.097 ms28 MB + 996 KBAcceptedScore: 25

Testcase #442.459 ms28 MB + 996 KBAcceptedScore: 25


Judge Duck Online | 评测鸭在线
Server Time: 2026-09-03 21:21:24 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠