提交记录 10718


用户 题目 状态 得分 用时 内存 语言 代码长度
lyricz router32. 测测你的路由器 Accepted 100 265.888 ms 33316 KB C++ 1.27 KB
提交时间 评测时间
2019-09-27 16:06:25 2020-08-01 02:20:21
# define max_entries 827088ul

# include "router.h"

# include <arpa/inet.h>
# include <iostream>

unsigned n_nodes;
unsigned trie_childs[max_entries * 32][2];
unsigned trie_nexthop[max_entries * 32];
bool trie_is_valid[max_entries * 32];

void init(int n, int q, const RoutingTableEntry *a) {
    RoutingTableEntry *routing_table = (RoutingTableEntry*) a;
    for (int i = 0; i < n; ++ i) {
        unsigned bit, current = 0;
        unsigned addr = htonl(routing_table[i].addr), length = routing_table[i].len;
        for (unsigned j = 0; j < length; ++ j) {
            bit = (addr >> (31ul - j)) & 1ul;
            if (!trie_childs[current][bit])
                trie_childs[current][bit] = ++ n_nodes;
            current = trie_childs[current][bit];
        }
        trie_is_valid[current] = true;
        trie_nexthop[current] = routing_table[i].nexthop;
    }
}

unsigned query(unsigned addr) {
    addr = htonl(addr);
    unsigned current = 0, bit, nexthop = trie_nexthop[0];
    for (unsigned i = 0; i < 32; ++ i) {
        bit = (addr >> (31ul - i)) & 1ul;
        if (trie_childs[current][bit]) {
            current = trie_childs[current][bit];
            if (trie_is_valid[current])
                nexthop = trie_nexthop[current];
        } else break;
    }
    return nexthop;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #135.48 us48 KBAcceptedScore: 25

Testcase #236.808 ms32 MB + 548 KBAcceptedScore: 25

Testcase #3151.84 ms32 MB + 548 KBAcceptedScore: 25

Testcase #4265.888 ms32 MB + 548 KBAcceptedScore: 25


Judge Duck Online | 评测鸭在线
Server Time: 2024-05-08 04:59:02 | Loaded in 3 ms | Server Status
个人娱乐项目,仅供学习交流使用