提交记录 10737


用户 题目 状态 得分 用时 内存 语言 代码长度
Zhengyi_Wang router32. 测测你的路由器 Wrong Answer 25 144.075 ms 17280 KB C++ 1.44 KB
提交时间 评测时间
2019-09-28 19:02:49 2020-08-01 02:25:10
//#define Debug 
#ifndef Debug
#include "router.h"
#endif
#ifdef Debug
typedef struct {
    unsigned addr;
    unsigned char len;
    char pad[3];  // Padding for memory alignment
    unsigned nexthop;
} __attribute__((packed)) RoutingTableEntry;
#endif

#include <iostream>
#include <cstdlib>
unsigned ones_prefix[33];
unsigned mask_addr[33];
int maxlen=-1;
int n;
int q;
RoutingTableEntry *a;

struct trie_node
{
	unsigned nexthop;
	trie_node* son[2];
};
trie_node* root=new trie_node;
void insert_node(unsigned addr,int len,unsigned nexthop){
	trie_node* tmp_node=root;
	for (int i=0;i<len;i++){
		int dir=(addr>>(31-i))&1;
		if (tmp_node->son[dir]==NULL){
			tmp_node->son[dir]=new trie_node;
			tmp_node=tmp_node->son[dir];
			tmp_node->son[0]=tmp_node->son[1]=NULL;
			tmp_node->nexthop=0;
		}
		else{
			tmp_node=tmp_node->son[dir];
		}
	}
	tmp_node->nexthop=nexthop;
}



void init(int _n, int _q, const RoutingTableEntry *_a) {
	n=_n;
	q=_q;
	a=(RoutingTableEntry*)_a;
	for (int i=1;i<=32;i++){
		ones_prefix[i]=ones_prefix[i-1]|(1<<(32-i));
	}

	root->son[0]=root->son[1]=NULL;
	root->nexthop=0;
	for (int i=0;i<n;i++){
		insert_node(a[i].addr,a[i].len,a[i].nexthop);
	}
}


unsigned query(unsigned addr) {
	trie_node* tmp_node=root;
	unsigned ans=0;
	for (int i=0;i<32;i++){
		int dir=(addr>>(31-i))&1;
		if (tmp_node->son[dir]==NULL){
			break;
		}
		tmp_node=tmp_node->son[dir];
		if (tmp_node->nexthop!=0){
			ans=tmp_node->nexthop;
		}
	}


	return ans;
}


CompilationN/AN/ACompile OKScore: N/A

Testcase #134.12 us36 KBAcceptedScore: 25

Testcase #242.353 ms16 MB + 896 KBWrong AnswerScore: 0

Testcase #393.42 ms16 MB + 896 KBWrong AnswerScore: 0

Testcase #4144.075 ms16 MB + 896 KBWrong AnswerScore: 0


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