提交记录 10563


用户 题目 状态 得分 用时 内存 语言 代码长度
wys router32. 测测你的路由器 Accepted 100 44.462 ms 38940 KB C++11 2.35 KB
提交时间 评测时间
2019-09-19 11:39:30 2020-08-01 02:17:04
#include <stdint.h>
#include <arpa/inet.h>
#include <algorithm>
#include "router.h"

const uint32_t TABLE_32_SIZE = 16384;
const uint32_t TABLE_24_SIZE = 65536;

uint32_t table_32_cnt = 0;
uint32_t table_24_cnt = 0;
uint32_t table_32[TABLE_32_SIZE][1 << 8] __attribute__((aligned(4096)));
uint32_t table_24[TABLE_24_SIZE][1 << 8] __attribute__((aligned(4096)));
uint32_t table_16[1 << 16] __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++;
	}
}

void ins(uint32_t addr, int len, uint32_t nexthop) {
	if (len <= 16) {
		fill(table_16 + (addr >> 16), 1u << (16 - len), nexthop);
	} else if (len <= 24) {
		uint32_t &t16 = table_16[addr >> 16];
		addr = (addr & 65535u) >> 8;
		
		uint32_t *tmp;
		if (t16 < -TABLE_24_SIZE) {
			tmp = table_24[--table_24_cnt + TABLE_24_SIZE];
			fill(tmp, addr, t16);
			fill(tmp + addr, 1u << (24 - len), nexthop);
			fill(tmp + addr + (1u << (24 - len)), 256 - addr - (1u << (24 - len)), t16);
			t16 = table_24_cnt;
		} else {
			tmp = table_24[t16 + TABLE_24_SIZE];
			fill(tmp + addr, 1u << (24 - len), nexthop);
		}
	} else {
		uint32_t &t16 = table_16[addr >> 16];
		addr &= 65535u;
		
		uint32_t *tmp;
		if (t16 < -TABLE_24_SIZE) {
			tmp = table_24[--table_24_cnt + TABLE_24_SIZE];
			fill(tmp, 256, t16);
			t16 = table_24_cnt;
		} else {
			tmp = table_24[t16 + TABLE_24_SIZE];
		}
		
		uint32_t &t24 = tmp[addr >> 8];
		addr &= 255u;
		
		if (t24 < -TABLE_32_SIZE) {
			tmp = table_32[--table_32_cnt + TABLE_32_SIZE];
			fill(tmp, addr, t24);
			fill(tmp + addr, 1u << (32 - len), nexthop);
			fill(tmp + addr + (1u << (32 - len)), 256 - addr - (1u << (32 - len)), t24);
			t24 = table_32_cnt;
		} else {
			tmp = table_32[t24 + TABLE_32_SIZE];
			fill(tmp + 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_16[addr >> 16];
	if (tmp >= -TABLE_24_SIZE) {
		addr &= 65535u;
		tmp = table_24[tmp + TABLE_24_SIZE][addr >> 8];
		if (tmp >= -TABLE_32_SIZE) {
			return table_32[tmp + TABLE_32_SIZE][addr & 255u];
		} else {
			return tmp;
		}
	} else {
		return tmp;
	}
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #112.76 us40 KBAcceptedScore: 25

Testcase #212.836 ms38 MB + 28 KBAcceptedScore: 25

Testcase #328.725 ms38 MB + 28 KBAcceptedScore: 25

Testcase #444.462 ms38 MB + 28 KBAcceptedScore: 25


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