提交记录 10744
提交时间 |
评测时间 |
2019-09-28 19:35:10 |
2020-08-01 02:25:17 |
#include "router.h"
#include <netinet/in.h>
#include <cstdlib>
unsigned ones_prefix[33];
unsigned mask_addr[33];
int maxlen=-1;
int n;
int q;
RoutingTableEntry *a;
struct trie_node
{
unsigned nexthop;
unsigned isvalid;
trie_node* son[2];
};
trie_node* root=new trie_node;
void insert_node(unsigned addr,unsigned len,unsigned nexthop){
trie_node* tmp_node=root;
for (unsigned 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=tmp_node->isvalid=0;
}
else{
tmp_node=tmp_node->son[dir];
}
}
tmp_node->nexthop=nexthop;
tmp_node->isvalid=1;
}
void init(int _n, int _q, const RoutingTableEntry *_a) {
n=_n;
q=_q;
a=(RoutingTableEntry*)_a;
root->son[0]=root->son[1]=NULL;
root->nexthop=root->isvalid=0;
for (int i=0;i<n;i++){
insert_node(htonl(a[i].addr),a[i].len,a[i].nexthop);
}
}
unsigned query(unsigned addr) {
addr=htonl(addr);
trie_node* tmp_node=root;
unsigned ans=root->nexthop;
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->isvalid==1){
ans=tmp_node->nexthop;
}
}
return ans;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 12.62 us | 24 KB | Accepted | Score: 25 | 显示更多 |
Testcase #2 | 48.001 ms | 66 MB + 172 KB | Accepted | Score: 25 | 显示更多 |
Testcase #3 | 182.136 ms | 66 MB + 172 KB | Accepted | Score: 25 | 显示更多 |
Testcase #4 | 315.556 ms | 66 MB + 172 KB | Accepted | Score: 25 | 显示更多 |
Judge Duck Online | 评测鸭在线
Server Time: 2024-12-05 10:35:48 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠