提交记录 14134


用户 题目 状态 得分 用时 内存 语言 代码长度
onionyst router32. 测测你的路由器 Accepted 100 257.854 ms 38740 KB C 1.25 KB
提交时间 评测时间
2020-09-14 00:55:07 2020-09-14 00:55:14
#include "router.h"

typedef struct {
  unsigned char len;
  unsigned nexthop;
  int next[2];
} TrieNode;

TrieNode trie[30000000];
int trie_index = 0;

unsigned n2hl(unsigned addr) {
  return (addr >> 24) + (((addr >> 16) & 0xff) << 8) +
         (((addr >> 8) & 0xff) << 16) + ((addr & 0xff) << 24);
}

void update(RoutingTableEntry entry) {
  unsigned addr = n2hl(entry.addr);

  if (addr == 0 && entry.len == 0) {
    trie[0].len = 1;
    trie[0].nexthop = entry.nexthop;
    return;
  }

  int ptr = 0;
  for (int i = 0; i < entry.len; i++) {
    int b = (addr >> (31 - i)) & 1;
    if (trie[ptr].next[b] == 0) {
      trie[ptr].next[b] = ++trie_index;
    }
    ptr = trie[ptr].next[b];
  }
  trie[ptr].len = entry.len;
  trie[ptr].nexthop = entry.nexthop;
}

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

unsigned query(unsigned addr) {
  addr = n2hl(addr);

  unsigned nexthop = 0;

  if (trie[0].len == 1) {
    nexthop = trie[0].nexthop;
  }

  int ptr = 0;
  for (int i = 0; i < 32; i++) {
    int b = (addr >> (31 - i)) & 1;
    if (trie[ptr].next[b] == 0) {
      break;
    }
    ptr = trie[ptr].next[b];
    if (trie[ptr].len != 0) {
      nexthop = trie[ptr].nexthop;
    }
  }

  return nexthop;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #112.03 us28 KBAcceptedScore: 25

Testcase #238.723 ms37 MB + 852 KBAcceptedScore: 25

Testcase #3148.834 ms37 MB + 852 KBAcceptedScore: 25

Testcase #4257.854 ms37 MB + 852 KBAcceptedScore: 25


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