提交记录 14285
提交时间 |
评测时间 |
2020-09-19 16:18:42 |
2020-09-19 16:18:52 |
#include "router.h"
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <vector>
#include <unordered_map>
#include <iostream>
std::unordered_map<uint32_t, RoutingTableEntry> map[33];
uint32_t big2small(uint32_t addr, int len) {
auto result = 0;
uint32_t subnet = ((len == 32 ? 0xffffffff : (1 << len) - 1)) << (32 - len);
return ntohl(addr) & subnet;
}
void update(bool insert, RoutingTableEntry entry) {
auto len = entry.len;
// auto subnet = (len == 32) ? 0xffffffff :(1 << len) - 1;
// auto refer = entry.addr & subnet;
auto refer = big2small(entry.addr, len);
if (insert) {
if (map[len].count(refer) == 0) {
map[len].insert(std::pair<uint32_t, RoutingTableEntry>(refer, entry));
// std::cout << "insert " << refer << " " << len << " " << std::endl;
} else {
if (map[len][refer].addr == entry.addr && map[len][refer].len == entry.len) {
map[len][refer] = entry;
}
}
} else {
if (map[len][refer].addr == entry.addr && map[len][refer].len == entry.len)
map[len].erase(refer);
}
}
void init(int n, int q, const RoutingTableEntry *a) {
for (int i = 0; i < n; ++i) {
update(true, a[i]);
}
}
bool prefix_query(uint32_t addr, uint32_t *nexthop) {
for (int i = 32; i >= 0; --i) {
auto refer = big2small(addr, i);
// if (i == 32) std::cout << "[32] " << refer << std::endl;
if (map[i].count(refer) == 1) {
// std::cout << "match " << i << " " << addr << std::endl;
*nexthop = map[i][refer].nexthop;
return true;
}
}
return false;
}
unsigned query(unsigned addr) {
unsigned a;
if (prefix_query(addr, &a)) return a;
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 37.91 us | 36 KB | Accepted | Score: 25 | 显示更多 |
Testcase #2 | 179.394 ms | 42 MB + 320 KB | Accepted | Score: 25 | 显示更多 |
Testcase #3 | 1.463 s | 42 MB + 320 KB | Accepted | Score: 25 | 显示更多 |
Testcase #4 | 2.746 s | 42 MB + 320 KB | Accepted | Score: 25 | 显示更多 |
Judge Duck Online | 评测鸭在线
Server Time: 2024-12-05 10:38:28 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠