提交记录 10867


用户 题目 状态 得分 用时 内存 语言 代码长度
ta123 router32. 测测你的路由器 Accepted 100 3.293 s 61412 KB C++11 1.08 KB
提交时间 评测时间
2019-10-06 20:57:53 2020-08-01 02:35:18
#include "router.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <map>
#include <arpa/inet.h>
using namespace std;

map<pair<unsigned, char>, unsigned> table;

void init(int n, int q, const RoutingTableEntry *a) {
	for (int i=0; i<n; i++) {
        RoutingTableEntry x = a[i];
        x.addr = htonl(x.addr);
        x.len = 32 - x.len;
        if (x.len != 32) {
            unsigned mask = (-1)^((1<<x.len)-1);
            x.addr &= mask; 
        } else {
            x.addr = 0;
        }
        pair<unsigned, char> pr = make_pair((unsigned)x.addr, x.len);
        if (table.find(pr) == table.end())
            table[pr] = x.nexthop;
    }
}

unsigned query(unsigned addr) {
    addr = htonl(addr);
    for (char i = 0; i <= 32; i++)
    {
        if (i != 32) {
            unsigned mask = (-1)^((1<<i)-1);
            addr &= mask;
        } else {
            addr = 0;
        }
        pair<unsigned, char> toSearch = make_pair(addr, i);
        if (table.find(toSearch) != table.end()) {
            return table[toSearch];
        }
    }
	return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #112.82 us24 KBAcceptedScore: 25

Testcase #2144.677 ms59 MB + 996 KBAcceptedScore: 25

Testcase #31.717 s59 MB + 996 KBAcceptedScore: 25

Testcase #43.293 s59 MB + 996 KBAcceptedScore: 25


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