提交记录 10558


用户 题目 状态 得分 用时 内存 语言 代码长度
user1 router32. 测测你的路由器 Wrong Answer 25 37.46 ms 14624 KB C++11 2.29 KB
提交时间 评测时间
2019-09-19 01:21:53 2020-08-01 02:17:01
#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) { 
	__asm("rep stosl":  "+D"(a), "+c"(n), "+a"(val):: "memory");
}

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[table_24_cnt + 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[table_24_cnt + 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[table_32_cnt + 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 #113.28 us36 KBAcceptedScore: 25

Testcase #24.734 ms14 MB + 288 KBWrong AnswerScore: 0

Testcase #321.767 ms14 MB + 288 KBWrong AnswerScore: 0

Testcase #437.46 ms14 MB + 288 KBWrong AnswerScore: 0


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