提交记录 14223
提交时间 |
评测时间 |
2020-09-18 17:46:04 |
2020-09-18 17:46:10 |
#include "router.h"
struct RoutingTableNode {
RoutingTableNode *nx[2];
unsigned int nexthop;
RoutingTableNode() {
nx[0] = nx[1] = nullptr;
nexthop = 0;
}
~RoutingTableNode() {
if (nx[0]) {
delete nx[0];
}
if (nx[1]) {
delete nx[1];
}
}
} *root = nullptr;
void init(int n, int q, const RoutingTableEntry *a) {
root = new RoutingTableNode();
for (int i = 0; i < n; i++) {
RoutingTableNode *now = root;
unsigned int addr = a[i].addr;
addr = ((addr & 0xffff) << 16) | (addr >> 16); // 2143
addr = ((addr & 0x00ff00ff) << 8) | ((addr & 0xff00ff00) >> 8); //1234
for (int j = 0; j < a[i].len; j++) {
int nw = (addr & 0x80000000) != 0 ;
addr <<= 1;
if (!now->nx[nw]) {
now->nx[nw] = new RoutingTableNode();
}
now = now->nx[nw];
}
now->nexthop = a[i].nexthop;
}
}
unsigned query(unsigned addr) {
RoutingTableNode *now = root;
addr = ((addr & 0xffff) << 16) | (addr >> 16); // 2143
addr = ((addr & 0x00ff00ff) << 8) | ((addr & 0xff00ff00) >> 8); //1234
RoutingTableNode *found = nullptr;
found = now;
for (int i = 0; i < 32; i++) {
int nw = (addr & 0x80000000) != 0;
addr <<= 1;
if (!now->nx[nw]) {
break;
}
now = now->nx[nw];
if (now->nexthop) {
found = now;
}
}
return found->nexthop;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 11.19 us | 24 KB | Accepted | Score: 25 | 显示更多 |
Testcase #2 | 46.848 ms | 66 MB + 172 KB | Accepted | Score: 25 | 显示更多 |
Testcase #3 | 159.174 ms | 66 MB + 172 KB | Accepted | Score: 25 | 显示更多 |
Testcase #4 | 270.821 ms | 66 MB + 172 KB | Accepted | Score: 25 | 显示更多 |
Judge Duck Online | 评测鸭在线
Server Time: 2024-12-05 10:33:13 | Loaded in 0 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠