提交记录 13222


用户 题目 状态 得分 用时 内存 语言 代码长度
wys router32. 测测你的路由器 Accepted 100 322.196 ms 513084 KB C++ 1.70 KB
提交时间 评测时间
2020-08-01 00:48:12 2020-08-01 00:48:21
#include "router.h"

#include <stdint.h>
#include <arpa/inet.h>

#include <algorithm>

static inline bool in_entry_range(const RoutingTableEntry &e, uint32_t addr) {
	return (uint64_t) (addr ^ e.addr) >> (32 - e.len) == 0;
}

const int MAXN = 900000;

static uint32_t left[MAXN * 2];
static uint32_t nexthops[MAXN * 2];
static int n_intervals;

static inline void append(uint32_t l, uint32_t nexthop) {
	if (left[n_intervals - 1] == l) {
		nexthops[n_intervals - 1] = nexthop;
	} else if (left[n_intervals - 1] < l) {
		left[n_intervals] = l;
		nexthops[n_intervals] = nexthop;
		++n_intervals;
	}
}

static uint64_t bitmap[1 << (32 - 6)];
static int sum[1 << (32 - 6)];

void init(int n, int q, const RoutingTableEntry *a) {
	RoutingTableEntry stack[34];
	int top = 0;
	
	stack[0].nexthop = -1u;
	
	n_intervals = 1;
	left[0] = 0;
	nexthops[0] = 0;
	
	for (int i = 0; i < n; i++) {
		RoutingTableEntry tmp = a[i];
		uint32_t addr = tmp.addr = htonl(tmp.addr);
		while (top && !in_entry_range(stack[top], addr)) {
			append(stack[top].addr + (1ull << (32 - stack[top].len)), stack[top - 1].nexthop);
			--top;
		}
		stack[++top] = tmp;
		append(tmp.addr, tmp.nexthop);
	}
	
	while (top) {
		append(stack[top].addr + (1ull << (32 - stack[top].len)), stack[top - 1].nexthop);
		--top;
	}
	
	for (int i = 0; i < n_intervals; i++) {
		uint32_t l = left[i];
		bitmap[l >> 6] |= 1ull << (l & 63);
	}
	
	int s = 0;
	for (int i = 0; i < 1 << (32 - 6); i++) {
		sum[i] = s;
		s += __builtin_popcountll(bitmap[i]);
	}
}

unsigned query(unsigned addr) {
	addr = htonl(addr);
	
	uint32_t idx = addr >> 6;
	uint32_t bits = addr & 63;
	int s1 = sum[idx];
	int s2 = __builtin_popcountll(bitmap[idx] & ((2ull << bits) - 1ull));
	return nexthops[s1 + s2 - 1];
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #1213.595 ms256 MB + 40 KBAcceptedScore: 25

Testcase #2229.692 ms501 MB + 60 KBAcceptedScore: 25

Testcase #3276.293 ms501 MB + 60 KBAcceptedScore: 25

Testcase #4322.196 ms501 MB + 60 KBAcceptedScore: 25


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