提交记录 14177


用户 题目 状态 得分 用时 内存 语言 代码长度
chrogeek router32. 测测你的路由器 Accepted 100 274.545 ms 33304 KB C++ 1.18 KB
提交时间 评测时间
2020-09-17 09:28:30 2020-09-17 09:28:37
#include "router.h"

#include <cassert>

const int maxn = 827090 << 5;

int nodes = 1;
int next[maxn][2];
bool isEntry[maxn];
unsigned nexthop[maxn];

unsigned toLittleEndian(unsigned bigEndian) {
	return ((bigEndian & 0xffu) << 24) | ((bigEndian & 0xff00u) << 8) | ((bigEndian & 0xff0000u) >> 8) | ((bigEndian & 0xff000000u) >> 24);
}

void insert(const RoutingTableEntry &entry) {
	const unsigned addr = toLittleEndian(entry.addr), len = entry.len;
	int o = 0;
	for (unsigned i = 0; i < len; ++i) {
		const int digit = !!(addr & (1u << (31 - i)));
		assert(digit == 0 || digit == 1);
		if (!next[o][digit]) {
			next[o][digit] = nodes;
			isEntry[nodes] = false;
			++nodes;
		}
		o = next[o][digit];
	}
	isEntry[o] = true;
	nexthop[o] = entry.nexthop;
}

void init(int n, int q, const RoutingTableEntry *a) {
	for (int i = 0; i < n; ++i) {
		insert(a[i]);
	}
}

unsigned query(unsigned addr) {
	addr = toLittleEndian(addr);
	int o = 0;
	unsigned ans = isEntry[o] ? nexthop[o] : 0;
	for (int i = 0; i < 32; ++i) {
		const int digit = !!(addr & (1u << (31 - i)));
		if (!next[o][digit]) {
			return ans;
		} else {
			o = next[o][digit];
			if (isEntry[o]) {
				ans = nexthop[o];
			}
		}
	}
	return ans;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #112.99 us36 KBAcceptedScore: 25

Testcase #238.102 ms32 MB + 536 KBAcceptedScore: 25

Testcase #3156.84 ms32 MB + 536 KBAcceptedScore: 25

Testcase #4274.545 ms32 MB + 536 KBAcceptedScore: 25


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