提交记录 14036


用户 题目 状态 得分 用时 内存 语言 代码长度
wys router32. 测测你的路由器 Accepted 100 54.993 ms 79480 KB C++ 1.39 KB
提交时间 评测时间
2020-08-24 14:37:13 2020-08-24 14:37:15
// 24-8 naive, ~17.3ns

#include <stdint.h>
#include <arpa/inet.h>
#include <algorithm>
#include "router.h"

const uint32_t TABLE_SIZE = 16384;

uint32_t table_cnt = 0;
uint32_t table_32[TABLE_SIZE][1 << 8] __attribute__((aligned(4096)));
uint32_t table_24[1 << 24] __attribute__((aligned(4096)));

inline void fill(uint32_t *a, int n, uint32_t val) {
	while (n >= 4) {
		a[0] = val;
		a[1] = val;
		a[2] = val;
		a[3] = val;
		n -= 4;
		a += 4;
	}
	while (n) {
		a[0] = val;
		n--;
		a++;
	}
}

inline void ins(uint32_t addr, int len, uint32_t nexthop) {
	if (len <= 24) {
		fill(table_24 + (addr >> 8), 1u << (24 - len), nexthop);
	} else {
		uint32_t &tmp = table_24[addr >> 8];
		addr &= 255u;
		
		if (tmp < -TABLE_SIZE) {
			uint32_t *tmp2 = table_32[--table_cnt + TABLE_SIZE];
			fill(tmp2, addr, tmp);
			fill(tmp2 + addr, 1u << (32 - len), nexthop);
			fill(tmp2 + addr + (1u << (32 - len)), 256 - addr - (1u << (32 - len)), tmp);
			tmp = table_cnt;
		} else {
			uint32_t *tmp2 = table_32[tmp + TABLE_SIZE];
			fill(tmp2 + addr, 1u << (32 - len), nexthop);
		}
	}
}

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

unsigned query(unsigned addr) {
	addr = htonl(addr);
	
	uint32_t tmp = table_24[addr >> 8];
	if (tmp >= -TABLE_SIZE) {
		return table_32[tmp + TABLE_SIZE][addr & 255u];
	} else {
		return tmp;
	}
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #113.38 us36 KBAcceptedScore: 25

Testcase #220.521 ms77 MB + 632 KBAcceptedScore: 25

Testcase #337.882 ms77 MB + 632 KBAcceptedScore: 25

Testcase #454.993 ms77 MB + 632 KBAcceptedScore: 25


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