提交记录 13221


用户 题目 状态 得分 用时 内存 语言 代码长度
wys router32. 测测你的路由器 Accepted 100 244.723 ms 16988 KB C++ 1.41 KB
提交时间 评测时间
2020-08-01 00:47:56 2020-08-01 00:48:03
#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;
	}
}

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

unsigned query(unsigned addr) {
	addr = htonl(addr);
	
	if (addr == -1u) {
		return nexthops[n_intervals - 1];
	} else {
		int id = std::lower_bound(left, left + n_intervals, addr + 1) - left - 1;
		return nexthops[id];
	}
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #114.24 us32 KBAcceptedScore: 25

Testcase #211.539 ms16 MB + 604 KBAcceptedScore: 25

Testcase #3128.124 ms16 MB + 604 KBAcceptedScore: 25

Testcase #4244.723 ms16 MB + 604 KBAcceptedScore: 25


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