提交记录 39995


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

// ============================================================================
// router32 flat /16 fill table, u32 arena (raw nexthop, no nh_tab indirection).
//   A[65536] u32   = /16 answer (0 = no match)
//   O[65536] u32   = arena element offset (0 = no deep entry in this slice)
//   arena          = 256 u32 slots per deep slice, prefilled with A[s],
//                    then len 17..24 entries fill 2^(24-len) slots (last write
//                    wins = longest prefix).
//   len>=25 (rare, 0.025%): arena slot = SENTINEL (0xFFFFFFFF) -> L3 table
//                    (256 u32 per /24), found via sorted l3key + binary search.
// Query: s=h>>16; ans=A[s]; o=O[s] (parallel loads); if(o) ans=arena[o+((h>>8)&255)].
// Uses the tasklib path (tasklib generates the random queries + checksum).
// ============================================================================

#define SENTINEL 0xFFFFFFFFu

static uint32_t *A;
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(65536, 4);
    O = (uint32_t*)malloc((size_t)65536 * 4);
    memset(O, 0, (size_t)65536 * 4);
    arena = (uint32_t*)malloc((size_t)65536 * 256 * 4);   // max 64MB, lazy
    L3 = (uint32_t*)malloc((size_t)n * 256 * 4);          // max, lazy
    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 {
            uint32_t s = v >> 16;
            uint32_t o = O[s];
            if (o == 0) {
                // first deep entry in this slice: allocate arena, prefill A[s]
                deep_ordinal++;
                o = deep_ordinal * 256;   // 1-based block
                O[s] = o;
                uint32_t base = o;
                uint32_t dv = A[s];
                for (uint32_t c = 0; c < 256; c++) arena[base + c] = dv;
            }
            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] = nh;
            } else {
                uint32_t slot = (v >> 8) & 255;
                uint32_t *e = &arena[o + slot];
                if (*e != SENTINEL) {
                    uint32_t l3 = l3cur; l3cur += 256;
                    // prefill L3 with current /24 answer (*e = code or A[s])
                    for (uint32_t c = 0; c < 256; c++) L3[l3 + c] = *e;
                    *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 >> 16;
    uint32_t ans = A[s];
    uint32_t o = O[s];
    if (o) {
        ans = arena[o + ((h >> 8) & 255)];
        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 #164.82 us548 KBAcceptedScore: 25

Testcase #213.435 ms38 MB + 328 KBAcceptedScore: 25

Testcase #330.664 ms38 MB + 328 KBAcceptedScore: 25

Testcase #447.612 ms38 MB + 328 KBAcceptedScore: 25


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