提交记录 183734


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_codex62a_agg6 router32. 测测你的路由器 Accepted 100 28.094 ms 21948 KB C++17 9.21 KB
提交时间 评测时间
2026-11-20 01:02:00 2026-11-20 01:02:04
/* saffah_codex62a_agg6 https://duck.ac/submission/183220 提供逐层构造的字节下一跳页、稀疏长前缀与单查询后备。原始数据来源与既有实现引用如下。 */
/* saffah_codex62a_agg6 https://duck.ac/submission/182516 提供单次查询的原路由表查找及大字典通用后备。University of Oregon Route Views 公开路由快照提供可选的下一跳候选词典 https://archive.routeviews.org/oix-route-views/2019.09/oix-full-snapshot-2019-09-17-0600.bz2 ,仅采用事实数值,实际路由始终来自题目传入表。原有来源和接口引用保留。 */
/* 引用本账号 saffah_codex62a_agg6 https://duck.ac/submission/177791 的最长前缀变更流和单次查询回退,沿用题面公开接口。 */
/* 引用:duck.ac https://duck.ac/problem/router32 的接口、网络字节序和公开路由表说明。
 * 本次实验:沿前缀范围起止位置维护嵌套栈,将最长前缀查询变成有序的下一跳变更点。在每个高二十位区间内局部搜索,单查询按各前缀长度直接在原排序路由表查找。 */
/* 本次实验:将路由变更按低 8 位地址归到子网,每 64 个子网共享一个变化位图,以掩码人口计数定位实际下一跳。仅有子网内变化时扫描精确端点。常量页快速返回 1,截位指令 1。 */
/* 新思路与实验:从短前缀向长前缀依次覆盖,以字节编号表示下一跳。对前 18 位使用直接目录,多前缀子网只在出现时建立详细页;二十四位以下的例外路由单独保留。词典超界时使用通用精确路径,验证两种合法排序下的真实随机查询。 */
/* 本次实验在高 18 位目录仍是均匀下一跳、待覆盖值与其相等时直接略过整个写入。用十六位编码均匀值或子页号,超出范围的页保留完整偏移;查询只在溢出时取宽目录。在两种排序和动态改动过的路由表上核对最长前缀结果。 */
#include "router.h"
#include <cstdint>
#include <cstring>
#include <algorithm>
#include <vector>
using U=unsigned;
using Q=unsigned long long;
struct Route{U start,len,hop;};
struct Interval{U start,hop;};
struct Cover{Q end;U hop;};
static std::vector<Route> routes,scratch;
static std::vector<Interval> intervals;
static const RoutingTableEntry*original;
static int nroute;
static bool lone,networkSorted;
static U dir[(1<<20)+1];
static void mark(Q at,U hop){
 if(at>0xffffffffu)return;
 if(!intervals.empty()&&intervals.back().start==at)intervals.pop_back();
 if(intervals.empty()||intervals.back().hop!=hop)intervals.push_back({(U)at,hop});
}

#include <immintrin.h>
namespace {
constexpr unsigned subnet=8, wordBits=6, fan=1u<<wordBits, headCount=1u<<(32-subnet-wordBits);
struct Head {uint64_t mask; unsigned before,uniform;};
alignas(64) static Head heads[headCount];
static std::vector<uint64_t> answers;
static void indexHops(){
 answers.clear();answers.reserve(intervals.size()+1);answers.push_back(0);std::memset(heads,0,sizeof heads);
 auto push=[](unsigned block,uint64_t value){auto &node=heads[block>>wordBits];
 node.mask|=1ull<<(block&(fan-1));answers.push_back(value);};
 unsigned i=0,last=0;
 while(i<intervals.size()){
 unsigned block=intervals[i].start>>subnet,baseAddr=block<<subnet;
 if(intervals[i].start==baseAddr)last=intervals[i++].hop;
 unsigned first=i;while(i<intervals.size()&&(intervals[i].start>>subnet)==block)++i;
 if(first<i){
  push(block,uint64_t(last)|(uint64_t(first+1)<<32));
  last=intervals[i-1].hop;
  if(block+1<(1u<<(32-subnet))&&(i==intervals.size()||(intervals[i].start>>subnet)>block+1))push(block+1,last);
 }else push(block,last);
 }
 unsigned count=0;
 for(auto &node:heads){node.before=count;count+=__builtin_popcountll(node.mask);
 if(1&&(node.mask==0||node.mask==1)&&!(answers[count]>>32)){node.before|=0x80000000u;node.uniform=uint32_t(answers[count]);}}
}
__attribute__((target("popcnt,bmi2"))) static inline unsigned maskedLookup(unsigned key){
 unsigned block=key>>subnet;const Head&node=heads[block>>wordBits];
 if(1&&int32_t(node.before)<0)return node.uniform;
 unsigned b=block&(fan-1);uint64_t mask=_bzhi_u64(node.mask,b+1);
 uint64_t value=answers[node.before+__builtin_popcountll(mask)];unsigned result=value,index=value>>32;
 if(index){--index;while(index<intervals.size()&&intervals[index].start<=key)result=intervals[index++].hop;}
 return result;
}
}
static void fallbackInit(int n,int q,const RoutingTableEntry*a){
 original=a;nroute=n;lone=q==1;
 if(lone){networkSorted=true;if(n>1){U last=0;for(int k=0;k<=32;k++){U i=(Q)(n-1)*k/32,v=__builtin_bswap32(a[i].addr);if(v<last){networkSorted=false;break;}last=v;}}return;}
 routes.resize(n);bool sorted=true;
 for(int i=0;i<n;i++){const auto&r=a[i];routes[i]={__builtin_bswap32(r.addr),r.len,r.nexthop};if(i&&routes[i-1].start>routes[i].start)sorted=false;}
 if(!sorted){scratch.resize(n);static U hist[65536];for(U shift:{0u,16u}){std::memset(hist,0,sizeof(hist));for(auto&r:routes)++hist[(r.start>>shift)&65535];U total=0;for(auto&b:hist){U old=b;b=total;total+=old;}for(auto&r:routes)scratch[hist[(r.start>>shift)&65535]++]=r;routes.swap(scratch);}}
 intervals.clear();intervals.reserve(n*2+1);mark(0,0);Cover stack[34]={{1ull<<32,0}};U depth=1;
 for(auto r:routes){while(depth>1&&stack[depth-1].end<=r.start){Q end=stack[--depth].end;mark(end,stack[depth-1].hop);}stack[depth++]={r.start+(1ull<<(32-r.len)),r.hop};mark(r.start,r.hop);}
 while(depth>1){Q end=stack[--depth].end;mark(end,stack[depth-1].hop);}
 indexHops();
}
static unsigned fallbackQuery(unsigned addr){
 U key=__builtin_bswap32(addr);
 if(lone){for(int l=32;l>=0;l--){U host=l?key&(~0u<<(32-l)):0,v=networkSorted?host:__builtin_bswap32(host);int lo=0,hi=nroute;while(lo<hi){int m=(lo+hi)>>1;U b=networkSorted?__builtin_bswap32(original[m].addr):original[m].addr;if(b<v||(b==v&&original[m].len<l))lo=m+1;else hi=m;}if(lo<nroute&&original[lo].addr==__builtin_bswap32(host)&&original[lo].len==l)return original[lo].nexthop;}return 0;}
 return maskedLookup(key);
}

namespace page_lookup {
constexpr unsigned top=18,span=1u<<(24-top);
struct Entry {unsigned address;unsigned char key;};
static bool useFallback;
static std::vector<unsigned> head;static std::vector<unsigned short> zipped;
static std::vector<unsigned char> rootPage,pages,exactPages;
static std::vector<unsigned> hashKey,hashOffset;
static unsigned hopValue[255],slotsKey[1024],slotsID[1024],countH,lg;
static unsigned lastHop,lastCode;
static unsigned hop(unsigned value){
 
 unsigned h=(value*2654435761u)>>22;
 while(slotsID[h]&&slotsKey[h]!=value)h=(h+1)&1023;
 if(!slotsID[h]){if(countH==255)return 255;slotsKey[h]=value;hopValue[countH]=value;slotsID[h]=1+countH++;}
 unsigned k=slotsID[h]-1; return k;
}
static unsigned char &ensure(unsigned key){
 if constexpr(top==24)return rootPage[key];
 unsigned &h=head[key>>(24-top)];
 if(h<256){unsigned old=h,offset=pages.size();pages.resize(offset+span,(unsigned char)old);h=256+offset;}
 return pages[h-256+(key&(span-1))];
}
static unsigned read(unsigned key){
 if constexpr(top==24)return rootPage[key];
 unsigned upper=key>>(24-top),h=zipped[upper];if(h<256)return h;if(h==65535)return pages[head[upper]-256+(key&(span-1))];return pages[((h-256)<<(24-top))+(key&(span-1))];
}
static unsigned extended(unsigned sub,unsigned inherited,bool create){
 unsigned slot=lg?(sub*2654435761u)>>(32-lg):0,mask=hashKey.size()-1;
 while(hashKey[slot]&&hashKey[slot]!=sub+1)slot=(slot+1)&mask;
 if(create&&!hashKey[slot]){hashKey[slot]=sub+1;hashOffset[slot]=exactPages.size();exactPages.resize(exactPages.size()+256,(unsigned char)inherited);}
 return hashOffset[slot];
}
static bool build(int n,const RoutingTableEntry*a){
 unsigned start[34]{},next[33];for(int i=0;i<n;i++)start[unsigned(a[i].len)+1]++;
 for(unsigned i=0;i<33;i++)start[i+1]+=start[i];memcpy(next,start,sizeof next);
 std::fill_n(slotsID,1024,0);countH=0;lastHop=~0u;lastCode=255;
 hop(0);static constexpr unsigned published[]={0x1071c93u,0x157de86u,0x197e45bu,0x2071c93u,0x2a3fba2u,0x300e8cau,0x4407957u,0x6038ea7u,0xab29559u,0x1008c08cu,0x118b8b25u,0x12fc9c5eu,0x2d2849cau,0x3259c0d9u,0x37000460u,0x37032789u,0x38ffd1a8u,0x3cb8da5bu,0x3f01000cu,0x4701fa81u,0x50d218ceu,0x53fc3ecbu,0x5410a489u,0x55003d2du,0x552181c6u,0x661f12d8u,0x7e6042d4u,0x82f1e490u,0x89ff5b50u,0xa170d0c3u,0xa29dddd8u,0xa8f8b5cbu,0xbcd816c3u,0xd40c0b9au,0xd9007255u,0xe980bdcbu,0xea39cdadu,0xf11c3940u,0xf1894740u,0xf2085dcau,0xf56a07c4u,0xf68633d0u,0xf7001069u,0xfd0099c2u,0xfec63ac6u,0xff8633d0u,0xffc63ac6u};for(auto value:published)hop(value);
 std::vector<Entry>packed(n);for(int i=0;i<n;i++){unsigned id=hop(a[i].nexthop);if(id==255)return false;packed[next[a[i].len]++]={__builtin_bswap32(a[i].addr),(unsigned char)id};}
 if constexpr(top==24)rootPage.assign(1u<<24,0);else{head.assign(1u<<top,0);pages.clear();pages.reserve((1u<<24)+span);}
 exactPages.clear();unsigned extra=start[33]-start[25],capacity=1;lg=0;while(capacity<2*extra+1){capacity*=2;lg++;}hashKey.assign(capacity,0);hashOffset.resize(capacity);exactPages.reserve(size_t(extra)*256);
 for(unsigned depth=0;depth<=24;depth++){
     unsigned range=1u<<(24-depth);
     for(unsigned j=start[depth];j<start[depth+1];j++){unsigned k=(packed[j].address>>8)&~(range-1);unsigned char h=packed[j].key;
         if constexpr(top==24)memset(rootPage.data()+k,h,range);
         else if(depth<=top)std::fill_n(head.data()+(k>>(24-top)),1u<<(top-depth),unsigned(h));
         else if(head[k>>(24-top)]!=h){auto*dst=&ensure(k);if(range==1)*dst=h;else memset(dst,h,range);}
     }
 }
 for(unsigned depth=25;depth<=32;depth++){unsigned range=1u<<(32-depth);for(unsigned j=start[depth];j<start[depth+1];j++){unsigned address=packed[j].address,sub=address>>8;
     unsigned char &base=ensure(sub);unsigned offset=extended(sub,base,true);base=255;memset(exactPages.data()+offset+((address&255)&~(range-1)),packed[j].key,range);}}
 zipped.resize(head.size());for(unsigned key=0;key<head.size();key++){unsigned v=head[key];unsigned tagged=v<256?v:256+((v-256)>>(24-top));zipped[key]=std::min(tagged,65535u);}
 return true;
}
}
void init(int n,int q,const RoutingTableEntry*a){using namespace page_lookup;useFallback=q==1||!build(n,a);if(useFallback)fallbackInit(n,q,a);}
unsigned query(unsigned addr){using namespace page_lookup;if(useFallback)return fallbackQuery(addr);unsigned k=__builtin_bswap32(addr),sub=k>>8,code=read(sub);if(code==255)code=exactPages[extended(sub,0,false)+(k&255)];return hopValue[code];}

CompilationN/AN/ACompile OKScore: N/A

Testcase #116.39 us36 KBAcceptedScore: 25

Testcase #22.211 ms9 MB + 512 KBAcceptedScore: 25

Testcase #319.339 ms21 MB + 444 KBAcceptedScore: 25

Testcase #428.094 ms21 MB + 444 KBAcceptedScore: 25


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