提交记录 14349


用户 题目 状态 得分 用时 内存 语言 代码长度
zhangzj router32. 测测你的路由器 Accepted 100 449.198 ms 298960 KB C++ 2.00 KB
提交时间 评测时间
2020-09-24 17:54:41 2020-09-24 17:54:48
#include "router.h"
#include <cstring>
#include <arpa/inet.h>

long long nexthop_trie[34000000];
long long *sub_trie[34000000];

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

void init(int n, int q, const RoutingTableEntry *a) {
    memset(nexthop_trie, -1, sizeof(nexthop_trie));
    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] == NULL)
            {
                sub_trie[cnt] = new long long[540];
                memset(sub_trie[cnt], -1, sizeof(long long) * 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));
    unsigned ret = 0;
    int cnt = 1;
    if (nexthop_trie[1] != -1)
        ret = nexthop_trie[1];
    for (int l = 24; l > 0; l--)
    {
        cnt = cnt << 1 | (addr & 1);
        addr >>= 1;
        if (nexthop_trie[cnt] != -1)
            ret = nexthop_trie[cnt];
    }
    if (sub_trie[cnt] != NULL)
    {
        int sub_cnt = 1;
        for (int l = 8; l > 0; l--)
        {
            sub_cnt = sub_cnt << 1 | (addr & 1);
            addr >>= 1;
            if (sub_trie[cnt][sub_cnt] != -1)
                ret = sub_trie[cnt][sub_cnt];
        }
    }
    return ret;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #121.686 ms259 MB + 436 KBAcceptedScore: 25

Testcase #251.629 ms291 MB + 976 KBAcceptedScore: 25

Testcase #3254.364 ms291 MB + 976 KBAcceptedScore: 25

Testcase #4449.198 ms291 MB + 976 KBAcceptedScore: 25


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