提交记录 13220


用户 题目 状态 得分 用时 内存 语言 代码长度
wys router32. 测测你的路由器 Accepted 100 1.603 s 87676 KB C++ 1.17 KB
提交时间 评测时间
2020-08-01 00:46:50 2020-08-01 00:47:48
#include "router.h"

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

#include <unordered_map>

static std::unordered_map<uint32_t, uint32_t> M[33];

// Assume sorted
static void insert(uint32_t addr, int len, uint32_t nexthop) {
	uint32_t last_nexthop = M[0][0];
	for (int i = 1; i < len; i++) {
		uint32_t &t = M[i][addr >> (32 - i) << (32 - i)];
		if (t) {
			last_nexthop = t;  // get deepest nexthop
		} else {
			t = last_nexthop;  // push-down
		}
	}
	
	M[len][addr] = nexthop;
}

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

unsigned query(unsigned addr) {
	addr = htonl(addr);
	int bits = 0;
	if (M[16].count(addr >> 16 << 16)) bits = 16;
	if (M[bits + 8].count(addr >> (32 - bits - 8) << (32 - bits - 8))) bits += 8;
	if (M[bits + 4].count(addr >> (32 - bits - 4) << (32 - bits - 4))) bits += 4;
	if (M[bits + 2].count(addr >> (32 - bits - 2) << (32 - bits - 2))) bits += 2;
	if (M[bits + 1].count(addr >> (32 - bits - 1) << (32 - bits - 1))) bits += 1;
	if (bits == 31 && M[32].count(addr)) return M[32][addr];
	if (bits == 0) return M[0][0];
	return M[bits][addr >> (32 - bits) << (32 - bits)];
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #117.05 us28 KBAcceptedScore: 25

Testcase #2668.91 ms85 MB + 636 KBAcceptedScore: 25

Testcase #31.135 s85 MB + 636 KBAcceptedScore: 25

Testcase #41.603 s85 MB + 636 KBAcceptedScore: 25


Judge Duck Online | 评测鸭在线
Server Time: 2026-03-24 17:36:00 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠