提交记录 14039


用户 题目 状态 得分 用时 内存 语言 代码长度
wys router32. 测测你的路由器 Accepted 100 67.949 ms 83900 KB C++ 2.32 KB
提交时间 评测时间
2020-08-24 15:07:34 2020-08-24 15:07:37
// 24-8 bitmap (combined)

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

struct BitmapEntry {
	uint64_t bits;
	int sum;
} __attribute__((packed));

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)));
BitmapEntry table_24_bitmap[1 << 24 >> 6] __attribute__((aligned(4096)));  // 6 MiB
uint32_t table_24_content[1 << 24] __attribute__((aligned(4096)));  // (2N + 1)

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);
		}
	}
}

inline void build_bitmap(uint32_t *table, int size, BitmapEntry *bitmap, uint32_t *content) {
	uint32_t last_val = table[0] + 1;
	int cur_sum = 0;
	
	for (int i = 0; i < size; i++) {
		if (!(i & 63)) {
			bitmap[i >> 6].sum = cur_sum;
		}
		if (table[i] != last_val) {
			bitmap[i >> 6].bits |= 1ull << (i & 63);
			++cur_sum;
			content[cur_sum] = last_val = table[i];
		}
	}
}

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);
	}
	
	build_bitmap(table_24, 1 << 24, table_24_bitmap, table_24_content);
}

unsigned query(unsigned addr) {
	addr = htonl(addr);
	
	uint32_t addr_24 = addr >> 8;
	uint32_t addr_24_idx = addr_24 >> 6;
	uint32_t addr_24_bits = addr_24 & 63;
	int s1 = table_24_bitmap[addr_24_idx].sum;
	int s2 = __builtin_popcountll(table_24_bitmap[addr_24_idx].bits & ((2ull << addr_24_bits) - 1ull));
	uint32_t tmp = table_24_content[s1 + s2];
	
	if (tmp >= -TABLE_SIZE) {
		return table_32[tmp + TABLE_SIZE][addr & 255u];
	} else {
		return tmp;
	}
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #123.543 ms3 MB + 40 KBAcceptedScore: 25

Testcase #242.993 ms81 MB + 956 KBAcceptedScore: 25

Testcase #356.124 ms81 MB + 956 KBAcceptedScore: 25

Testcase #467.949 ms81 MB + 956 KBAcceptedScore: 25


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