提交记录 10480


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah router32. 测测你的路由器 Compile Error 0 0 ns 0 KB C 1.21 KB
提交时间 评测时间
2019-09-18 15:33:54 2020-08-01 02:15:47
#pragma GCC optimize("Ofast,no-stack-protector")
#include "router.h"
#include <string.h>
#include <arpa/inet.h>

typedef struct
{
    unsigned *son;
    unsigned nexthop;
} VEntry;
VEntry table[1 << 24];

int top = 0;
unsigned shop[1 << 20][256];

unsigned inv24(unsigned x)
{
    char a[4]; memcpy(a, &x, sizeof(a));
    swap(a[0], a[2]); memcpy(&x, a, sizeof(a));
    return x;
}

void ins(unsigned addr, int len, unsigned nexthop)
{
    if(len <= 24)
    {
        addr >>= 8;
        for(int T = 1 << (24 - len); T--; ++addr) table[inv24(addr)].nexthop = nexthop;
    }
    else
    {
        unsigned key = inv24(addr >> 8);
        unsigned **son = &table[key].son;
        if(!*son)
        {
            *son = shop[++top];
            for(int i = 0; i < 256; i++) *son[i] = table[key].nexthop;
        }
        addr &= 255u;
        for(int T = 1 << (32 - len); T--; ++addr) *son[addr] = nexthop;
    }
}

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

struct Query
{
    unsigned lo: 24;
    unsigned hi: 8;
};

unsigned query(Query q)
{
    VEntry &ent = table[q.lo];
    if(ent.son) return ent.son[q.hi];
    else return ent.nexthop;
}

CompilationN/AN/ACompile ErrorScore: N/A


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