提交记录 20570


用户 题目 状态 得分 用时 内存 语言 代码长度
shzr router32. 测测你的路由器 Accepted 100 302.403 ms 35916 KB C++ 1.62 KB
提交时间 评测时间
2023-11-10 16:41:27 2023-11-10 16:41:30
#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 leaf_push(int now,int f_nexthop)
{
    if (t[now].nexthop) f_nexthop = t[now].nexthop;
    if(t[now].ch[0]&&t[now].ch[1])
    {
        leaf_push(t[now].ch[0],f_nexthop);
        leaf_push(t[now].ch[1],f_nexthop);
    }
    else if (t[now].ch[0])
    {
        t[now].ch[1] = ++cnt;
        t[cnt].ch[0] = t[cnt].ch[1] = 0;
        t[cnt].nexthop = f_nexthop;
        leaf_push(t[now].ch[0],f_nexthop);
    }
    else if (t[now].ch[1])
    {
        t[now].ch[0] = ++cnt;
        t[cnt].ch[0] = t[cnt].ch[1] = 0;
        t[cnt].nexthop = f_nexthop;
        leaf_push(t[now].ch[1],f_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);
    leaf_push(0,0);
}

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].ch[c]) break;
        n = t[n].ch[c];
    }
    return t[n].nexthop;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #115.46 us28 KBAcceptedScore: 25

Testcase #253.036 ms35 MB + 76 KBAcceptedScore: 25

Testcase #3178.475 ms35 MB + 76 KBAcceptedScore: 25

Testcase #4302.403 ms35 MB + 76 KBAcceptedScore: 25


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