提交记录 32431


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_dsh_260814 router32. 测测你的路由器 Accepted 100 280.357 ms 31488 KB C 1.39 KB
提交时间 评测时间
2026-08-14 10:54:22 2026-08-14 10:54:26
#include "router.h"
#include <stdlib.h>

typedef struct { unsigned c0, c1, nh; } Node; // nh = nexthop+1, 0 = none
static Node *tr;
static unsigned cnt;

static inline unsigned newnode(void) {
    tr[cnt].c0 = tr[cnt].c1 = tr[cnt].nh = 0;
    return cnt++;
}

void init(int n, int q, const RoutingTableEntry *a) {
    size_t maxn = (size_t)n * 33 + 2; // worst case; only created nodes are touched
    tr = (Node*)malloc(maxn * sizeof(Node));
    cnt = 0;
    newnode(); // root = node 0
    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 = 0;
        for (unsigned b = 0; b < len; b++) {
            unsigned bit = (ip >> (31 - b)) & 1u;
            unsigned c = bit ? tr[node].c1 : tr[node].c0;
            if (c == 0) {
                c = newnode();
                if (bit) tr[node].c1 = c; else tr[node].c0 = c;
            }
            node = c;
        }
        tr[node].nh = h + 1;
    }
}

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

CompilationN/AN/ACompile OKScore: N/A

Testcase #114.88 us24 KBAcceptedScore: 25

Testcase #260.442 ms30 MB + 768 KBAcceptedScore: 25

Testcase #3171.691 ms30 MB + 768 KBAcceptedScore: 25

Testcase #4280.357 ms30 MB + 768 KBAcceptedScore: 25


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