提交记录 13218


用户 题目 状态 得分 用时 内存 语言 代码长度
wys router32. 测测你的路由器 Accepted 100 266.086 ms 53248 KB C++ 1.05 KB
提交时间 评测时间
2020-08-01 00:46:15 2020-08-01 00:46:19
#include "router.h"

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

const int MAX_N = 900000;
const int MAX_N_NODES = MAX_N * 32;

struct TrieNode {
	TrieNode *s[2];
	uint32_t nexthop;
};

static TrieNode nodes[MAX_N_NODES], *last_node = nodes, *root;

static inline TrieNode * new_node() {
	return last_node++;
}

static void trie_insert(TrieNode *root, uint32_t addr, int len, uint32_t nexthop) {
	int bit_id = 32 - len;
	for (int i = 31; i >= bit_id; i--) {
		TrieNode *&t = root->s[(addr >> i) & 1];
		if (!t) t = new_node();
		root = t;
	}
	
	root->nexthop = nexthop;
}

static uint32_t trie_query(TrieNode *root, uint32_t addr) {
	uint32_t ans = root->nexthop;
	for (int i = 31; i >= 0; i--) {
		TrieNode *t = root->s[(addr >> i) & 1];
		if (!t) return ans;
		if (t->nexthop) ans = t->nexthop;
		root = t;
	}
	return ans;
}

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

unsigned query(unsigned addr) {
	return trie_query(root, htonl(addr));
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #112.71 us28 KBAcceptedScore: 25

Testcase #237.218 ms52 MBAcceptedScore: 25

Testcase #3152.047 ms52 MBAcceptedScore: 25

Testcase #4266.086 ms52 MBAcceptedScore: 25


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