提交记录 14338


用户 题目 状态 得分 用时 内存 语言 代码长度
zhangzj router32. 测测你的路由器 Wrong Answer 25 373.504 ms 91032 KB C++ 1.75 KB
提交时间 评测时间
2020-09-23 16:48:59 2020-09-23 16:49:05
#include "router.h"
#include <arpa/inet.h>

unsigned nexthop_trie[34000000] = {0};
unsigned *sub_trie[34000000];

unsigned Reverse(unsigned a)
{
    unsigned ret = 0;
    for (unsigned cnt = 31; a > 0; a >>= 1, cnt--)
        ret |= (a&1) << cnt;
    return ret;
}

void init(int n, int q, const RoutingTableEntry *a) {
    for (int i = 0; i < n; i++)
    {
        unsigned addr = Reverse(ntohl(a[i].addr));
        if (a[i].len > 24)
        {
            int cnt = 1;
            for (int l = 24; l > 0; l--)
            {
                cnt = cnt << 1 | (addr & 1);
                addr >>= 1;
            }
            if (sub_trie[cnt] == nullptr)
                sub_trie[cnt] = new unsigned[540];
            int sub_cnt = 1;
            for (int l = a[i].len - 24; l > 0; l--)
            {
                sub_cnt = sub_cnt << 1 | (addr & 1);
                addr >>= 1;
            }
            sub_trie[cnt][sub_cnt] = a[i].nexthop;
        }
        else
        {
            int cnt = 1;
            for (int l = a[i].len; l > 0; l--)
            {
                cnt = cnt << 1 | (addr & 1);
                addr >>= 1;
            }
            nexthop_trie[cnt] = a[i].nexthop;
        }
    }
}

unsigned query(unsigned addr) {
    addr = Reverse(ntohl(addr));
    int ret = 0;
    int cnt = 0;
    for (int l = 24; l > 0; l--)
    {
        cnt = cnt << 1 | (addr & 1);
        addr >>= 1;
        if (nexthop_trie[cnt] != 0)
            ret = nexthop_trie[cnt];
    }
    if (sub_trie[cnt] != nullptr)
    {
        int sub_cnt = 1;
        for (int l = 4; l > 0; l--)
        {
            sub_cnt = sub_cnt << 1 | (addr & 1);
            addr >>= 1;
            if (sub_trie[cnt][sub_cnt] != 0)
             ret = sub_trie[cnt][sub_cnt];
        }
    }
    return ret;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #117.8 us32 KBAcceptedScore: 25

Testcase #237.655 ms88 MB + 920 KBWrong AnswerScore: 0

Testcase #3210.586 ms88 MB + 920 KBWrong AnswerScore: 0

Testcase #4373.504 ms88 MB + 920 KBWrong AnswerScore: 0


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