提交记录 20569


用户 题目 状态 得分 用时 内存 语言 代码长度
shzr router32. 测测你的路由器 Accepted 100 246.06 ms 31484 KB C++ 1.05 KB
提交时间 评测时间
2023-11-10 16:25:21 2023-11-10 16:25:36
#include "router.h"

struct node
{
    int ch[2];
    unsigned nexthop;
}t[5000000];
int cnt = 0;

unsigned rev(unsigned x)
{
    unsigned ans = 0;
    for (int i=0;i<4;++i)
    {
        ans <<= 8;
        ans |= (x & 0xff);
        x >>= 8;
    }
    return ans;
}

void ins(unsigned addr, unsigned char len, unsigned nexthop)
{
    int now = 0;
    for (int i=0;i<len;++i)
    {
        int c = (addr >> (31 - i)) & 1;
        if (!t[now].ch[c])
        {
            t[now].ch[c] = ++cnt;
            t[cnt].ch[0] = t[cnt].ch[1] = 0;
            t[cnt].nexthop = 0;
        }
        now = t[now].ch[c];
    }
    t[now].nexthop = nexthop;
}

void init(int n, int q, const RoutingTableEntry *a) 
{
    for (int i=0;i<n;++i)
        ins(rev(a[i].addr), a[i].len, a[i].nexthop);
}

unsigned query(unsigned addr) 
{
    addr = rev(addr);
    unsigned ans = 0;
    int n = 0;
    for (int i=0;i<32;++i)
    {
        int c = (addr >> (31 - i)) & 1;
        if (t[n].nexthop) ans = t[n].nexthop;
        if (!t[n].ch[c]) break;
        n = t[n].ch[c];
    }
    return ans;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #114.6 us28 KBAcceptedScore: 25

Testcase #243.239 ms30 MB + 764 KBAcceptedScore: 25

Testcase #3145.107 ms30 MB + 764 KBAcceptedScore: 25

Testcase #4246.06 ms30 MB + 764 KBAcceptedScore: 25


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