提交记录 31848


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_dsh_260814 router32. 测测你的路由器 Wrong Answer 25 377.013 ms 329568 KB C 1.31 KB
提交时间 评测时间
2026-08-14 10:15:28 2026-08-14 10:15:31
#include "router.h"
#include <stdlib.h>
#include <string.h>

static unsigned *ch0, *ch1, *nh;
static unsigned cnt;

void init(int n, int q, const RoutingTableEntry *a) {
    size_t maxn = (size_t)n * 33 + 2;
    ch0 = (unsigned*)malloc(maxn * sizeof(unsigned));
    ch1 = (unsigned*)malloc(maxn * sizeof(unsigned));
    nh  = (unsigned*)malloc(maxn * sizeof(unsigned));
    memset(ch0, 0, maxn * sizeof(unsigned));
    memset(ch1, 0, maxn * sizeof(unsigned));
    memset(nh,  0, maxn * sizeof(unsigned));
    cnt = 1; // root = node 1
    for (int i = 0; i < n; i++) {
        unsigned ip = __builtin_bswap32(a[i].addr);
        unsigned len = a[i].len;
        unsigned h = a[i].nexthop;
        unsigned node = 1;
        for (unsigned b = 0; b < len; b++) {
            unsigned bit = (ip >> (31 - b)) & 1u;
            unsigned *c = bit ? &ch1[node] : &ch0[node];
            if (*c == 0) *c = ++cnt;
            node = *c;
        }
        nh[node] = h;
    }
}

unsigned query(unsigned addr) {
    unsigned ip = __builtin_bswap32(addr);
    unsigned node = 1, best = 0;
    for (int b = 0; b < 32; b++) {
        unsigned bit = (ip >> (31 - b)) & 1u;
        unsigned c = bit ? ch1[node] : ch0[node];
        if (c == 0) return best;
        node = c;
        unsigned h = nh[node];
        if (h) best = h;
    }
    return best;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #114.43 us24 KBAcceptedScore: 25

Testcase #287.088 ms321 MB + 864 KBWrong AnswerScore: 0

Testcase #3232.536 ms321 MB + 864 KBWrong AnswerScore: 0

Testcase #4377.013 ms321 MB + 864 KBWrong AnswerScore: 0


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