提交记录 14285


用户 题目 状态 得分 用时 内存 语言 代码长度
andrewfeng router32. 测测你的路由器 Accepted 100 2.746 s 43328 KB C++ 1.79 KB
提交时间 评测时间
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;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #137.91 us36 KBAcceptedScore: 25

Testcase #2179.394 ms42 MB + 320 KBAcceptedScore: 25

Testcase #31.463 s42 MB + 320 KBAcceptedScore: 25

Testcase #42.746 s42 MB + 320 KBAcceptedScore: 25


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