提交记录 10642


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah router32. 测测你的路由器 Accepted 100 41.44 ms 41628 KB C++11 6.32 KB
提交时间 评测时间
2019-09-24 11:14:37 2020-08-01 02:18:44
#pragma GCC optimize("Ofast")
#pragma GCC target("popcnt")

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

/* HashMap */

const int HASHMAP_SIZE = 1 << 16;
const uint32_t HASHMAP_MOD = 65537;

uint32_t hashmap_key[HASHMAP_SIZE] __attribute__((aligned(4096)));
uint32_t hashmap_next[HASHMAP_SIZE] __attribute__((aligned(4096)));
uint32_t hashmap_first[HASHMAP_MOD] __attribute__((aligned(4096)));
uint32_t mask_pre[256] __attribute__((aligned(4096))); // 2u << (index & 31)) - 1
int hashmap_size __attribute__((aligned(4096)));

inline uint32_t hashmap_get(uint32_t x) {
	uint32_t hash = x % HASHMAP_MOD;
	uint32_t &first_entry_id = hashmap_first[hash];
	uint32_t entry_id = first_entry_id;
	while (1) {
		const uint32_t &key = hashmap_key[entry_id];
		if (key == x) {
			return entry_id;
		} else if (entry_id) {
			entry_id = hashmap_next[entry_id];
		} else {
			break;
		}
	}
	++hashmap_size;
	hashmap_key[hashmap_size] = x;
	hashmap_next[hashmap_size] = first_entry_id;
	first_entry_id = hashmap_size;
	
	return hashmap_size;
}

/* 3-level tree */

const uint32_t TABLE_32_SIZE = 16384;
const uint32_t TABLE_24_SIZE = 32768;

uint32_t table_32_cnt __attribute__((aligned(4096)));
uint32_t table_24_cnt __attribute__((aligned(4096)));
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++;
	}
}

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

/* Bit sets */

const int MAX_N_LEVEL3_POINTERS = 1000000;
struct LvPack
{
    uint32_t s, sum;
};
LvPack level3_bit[TABLE_32_SIZE][(1 << 8) / 32] __attribute__((aligned(4096)));
#define level3_bits(x, y) level3_bit[x][y].s
#define level3_bit_sums(x, y) level3_bit[x][y].sum
// uint32_t level3_offsets[TABLE_32_SIZE];  // ??????
uint16_t level3_pointers[MAX_N_LEVEL3_POINTERS] __attribute__((aligned(4096)));
uint32_t n_level3_pointers __attribute__((aligned(4096)));

inline void walk_level3(uint32_t level3_index) {
	uint32_t *tmp = table_32[level3_index];
	// level3_offsets[level3_index] = n_level3_pointers;
	uint16_t *pointers = level3_pointers + n_level3_pointers;
	
	uint32_t cur = tmp[0];
	int cnt = 0;
	level3_bit_sums(level3_index, 0) = n_level3_pointers;
	
	// index 0
	pointers[0] = hashmap_get(cur);
	
	for (int i = 1; i < 1 << 8; i++) {
		if (i % 32 == 0) {
			level3_bit_sums(level3_index, i / 32) = cnt + n_level3_pointers;
		}
		if (tmp[i] != cur) {
			level3_bits(level3_index, i >> 5) |= 1u << (i & 31);
			cur = tmp[i];
			pointers[++cnt] = hashmap_get(cur);
		}
	}
	
	n_level3_pointers += cnt + 1;
}

const int MAX_N_LEVEL2_POINTERS = 2000000;
LvPack level2_bit[TABLE_24_SIZE][(1 << 8) / 32] __attribute__((aligned(4096)));
#define level2_bits(x, y) level2_bit[x][y].s
#define level2_bit_sums(x, y) level2_bit[x][y].sum
// uint32_t level2_offsets[TABLE_24_SIZE];  // may overflow uint16 ???
uint16_t level2_pointers[MAX_N_LEVEL2_POINTERS] __attribute__((aligned(4096)));
uint32_t n_level2_pointers __attribute__((aligned(4096)));

inline void walk_level2(uint32_t level2_index) {
	uint32_t *tmp = table_24[level2_index];
	uint16_t *pointers = level2_pointers + n_level2_pointers;
	
	uint32_t cur = tmp[0];
	int cnt = 0;
	level2_bit_sums(level2_index, 0) = n_level2_pointers;
	
	// index 0
	pointers[0] = cur >= -TABLE_32_SIZE ? cur : hashmap_get(cur);
	
	for (int i = 1; i < 1 << 8; i++) {
		if (i % 32 == 0) {
			level2_bit_sums(level2_index, i / 32) = cnt + n_level2_pointers;
		}
		if (tmp[i] != cur) {
			level2_bits(level2_index, i >> 5) |= 1u << (i & 31);
			cur = tmp[i];
			pointers[++cnt] = cur >= -TABLE_32_SIZE ? cur : hashmap_get(cur);
		}
	}
	
	n_level2_pointers += cnt + 1;
}

void init(int n, int q, const RoutingTableEntry *tbl) {
    for (int i = 0; i < 256; i++) {
        mask_pre[i] = (2u << (i & 31)) - 1;
    }

	for (int i = 0; i < n; i++) {
		ins(htonl(tbl[i].addr), tbl[i].len, tbl[i].nexthop);
	}
	
	for (int i = table_32_cnt; i < 0; i++) {
		walk_level3(i + TABLE_32_SIZE);
	}
	
	for (int i = table_24_cnt; i < 0; i++) {
		walk_level2(i + TABLE_24_SIZE);
	}
}

unsigned query(unsigned addr_raw) {
	unsigned addr = htonl(addr_raw);
	
	uint32_t tmp = table_16[addr >> 16];
	if (tmp >= -TABLE_24_SIZE) {
		uint32_t level2_index = tmp + TABLE_24_SIZE;
		uint32_t addr_l2 = (unsigned char) (addr_raw >> 16);
		uint32_t off = level2_bit_sums(level2_index, addr_l2 >> 5)
			+ __builtin_popcount(level2_bits(level2_index, addr_l2 >> 5) & mask_pre[addr_l2]);
		
		tmp = (int32_t) (int16_t) level2_pointers[off];
		if (tmp >= -TABLE_32_SIZE) {
			uint32_t level3_index = tmp + TABLE_32_SIZE;
			uint32_t addr_l3 = (unsigned char) (addr_raw >> 24);
			uint32_t off = level3_bit_sums(level3_index, addr_l3 >> 5)
				+ __builtin_popcount(level3_bits(level3_index, addr_l3 >> 5) & mask_pre[addr_l3]);
			
			tmp = (uint32_t) level3_pointers[off];
			return hashmap_key[tmp];
		} else {
			return hashmap_key[tmp];
		}
	} else {
		return tmp;
	}
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #117.03 us76 KBAcceptedScore: 25

Testcase #220.468 ms40 MB + 668 KBAcceptedScore: 25

Testcase #331.407 ms40 MB + 668 KBAcceptedScore: 25

Testcase #441.44 ms40 MB + 668 KBAcceptedScore: 25


Judge Duck Online | 评测鸭在线
Server Time: 2024-05-07 17:22:27 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用