提交记录 14134
提交时间 |
评测时间 |
2020-09-14 00:55:07 |
2020-09-14 00:55:14 |
#include "router.h"
typedef struct {
unsigned char len;
unsigned nexthop;
int next[2];
} TrieNode;
TrieNode trie[30000000];
int trie_index = 0;
unsigned n2hl(unsigned addr) {
return (addr >> 24) + (((addr >> 16) & 0xff) << 8) +
(((addr >> 8) & 0xff) << 16) + ((addr & 0xff) << 24);
}
void update(RoutingTableEntry entry) {
unsigned addr = n2hl(entry.addr);
if (addr == 0 && entry.len == 0) {
trie[0].len = 1;
trie[0].nexthop = entry.nexthop;
return;
}
int ptr = 0;
for (int i = 0; i < entry.len; i++) {
int b = (addr >> (31 - i)) & 1;
if (trie[ptr].next[b] == 0) {
trie[ptr].next[b] = ++trie_index;
}
ptr = trie[ptr].next[b];
}
trie[ptr].len = entry.len;
trie[ptr].nexthop = entry.nexthop;
}
void init(int n, int q, const RoutingTableEntry *a) {
for (int i = 0; i < n; i++) {
update(a[i]);
}
}
unsigned query(unsigned addr) {
addr = n2hl(addr);
unsigned nexthop = 0;
if (trie[0].len == 1) {
nexthop = trie[0].nexthop;
}
int ptr = 0;
for (int i = 0; i < 32; i++) {
int b = (addr >> (31 - i)) & 1;
if (trie[ptr].next[b] == 0) {
break;
}
ptr = trie[ptr].next[b];
if (trie[ptr].len != 0) {
nexthop = trie[ptr].nexthop;
}
}
return nexthop;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 12.03 us | 28 KB | Accepted | Score: 25 | 显示更多 |
Testcase #2 | 38.723 ms | 37 MB + 852 KB | Accepted | Score: 25 | 显示更多 |
Testcase #3 | 148.834 ms | 37 MB + 852 KB | Accepted | Score: 25 | 显示更多 |
Testcase #4 | 257.854 ms | 37 MB + 852 KB | Accepted | Score: 25 | 显示更多 |
Judge Duck Online | 评测鸭在线
Server Time: 2024-12-05 10:09:58 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠