提交记录 32431
| 提交时间 |
评测时间 |
| 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;
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 14.88 us | 24 KB | Accepted | Score: 25 | 显示更多 |
| Testcase #2 | 60.442 ms | 30 MB + 768 KB | Accepted | Score: 25 | 显示更多 |
| Testcase #3 | 171.691 ms | 30 MB + 768 KB | Accepted | Score: 25 | 显示更多 |
| Testcase #4 | 280.357 ms | 30 MB + 768 KB | Accepted | Score: 25 | 显示更多 |
Judge Duck Online | 评测鸭在线
Server Time: 2026-09-11 21:16:39 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠