提交记录 36419
| 提交时间 |
评测时间 |
| 2026-08-15 02:46:36 |
2026-08-15 02:46:43 |
#include "router.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
// Compact 20-4-8 with code L1 (uint16, 2MB): terminal code 0..46, deep = 47+ordinal.
// L2c = uint16[D20*16] (compact /24), L3 = uint8 (/32). 3-pass radix sort, packed rec.
static uint16_t *L1;
static uint16_t *L2;
static uint8_t *L3;
static uint32_t nh_tab[48];
static uint32_t *d24_key, *d24_base;
static uint32_t d24_cnt;
#define NHMAP_SIZE 128
static uint32_t nh_keys[NHMAP_SIZE];
static uint8_t nh_codes[NHMAP_SIZE];
static uint32_t ncode_count;
static inline uint8_t nh_code(uint32_t raw){
uint32_t h = (raw * 2654435761u) >> (32-7);
for(;;){
if(nh_codes[h]==0xFF){ nh_keys[h]=raw; ncode_count++; nh_tab[ncode_count]=raw; nh_codes[h]=(uint8_t)ncode_count; return (uint8_t)ncode_count; }
if(nh_keys[h]==raw) return nh_codes[h];
h = (h+1) & (NHMAP_SIZE-1);
}
}
void init(int n, int q, const RoutingTableEntry *a){
memset(nh_codes, 0xFF, sizeof(nh_codes));
ncode_count = 0; nh_tab[0] = 0;
uint8_t *b20 = (uint8_t*)calloc(1048576/8, 1);
uint8_t *b24 = (uint8_t*)calloc(16777216/8, 1);
uint32_t D20 = 0, D24 = 0;
uint64_t *rec = (uint64_t*)malloc((size_t)n*8);
for(int i=0;i<n;i++){
unsigned v = __builtin_bswap32(a[i].addr);
unsigned len = a[i].len;
unsigned code = nh_code(a[i].nexthop);
rec[i] = ((uint64_t)((len<<6) | code) << 32) | v;
if(len > 20){ unsigned idx=v>>12; if(!((b20[idx>>3]>>(idx&7))&1)){ b20[idx>>3]|=(1u<<(idx&7)); D20++; } }
if(len > 24){ unsigned idx=v>>8; if(!((b24[idx>>3]>>(idx&7))&1)){ b24[idx>>3]|=(1u<<(idx&7)); D24++; } }
}
free(b20); free(b24);
L1 = (uint16_t*)calloc(1u<<20, 2);
L2 = (uint16_t*)malloc((size_t)D20*16*2);
L3 = (uint8_t*)malloc((size_t)D24*256);
d24_key = (uint32_t*)malloc((size_t)D24*4);
d24_base = (uint32_t*)malloc((size_t)D24*4);
d24_cnt = 0;
uint32_t l2cur = 0, l3cur = 0;
uint64_t *tmp = (uint64_t*)malloc((size_t)n*8);
for(int pass=1; pass<4; pass++){
int cnt[256]; for(int j=0;j<256;j++) cnt[j]=0;
for(int i=0;i<n;i++) cnt[(rec[i] >> (pass*8)) & 255]++;
int pos[257]; pos[0]=0; for(int j=0;j<256;j++) pos[j+1]=pos[j]+cnt[j];
for(int i=0;i<n;i++) tmp[pos[(rec[i] >> (pass*8)) & 255]++] = rec[i];
uint64_t *t=rec; rec=tmp; tmp=t;
}
free(tmp);
for(int k=0;k<n;k++){
uint64_t r = rec[k];
unsigned v = (unsigned)r;
unsigned len = (unsigned)(r >> 38);
unsigned code = (unsigned)((r >> 32) & 0x3F);
if(len <= 20){
int shift = 20 - (int)len;
int count = 1 << shift;
int mask = count - 1;
int base = (int)((v>>12) & ~(unsigned)mask), end = (int)((v>>12) | (unsigned)mask);
for(int x=base;x<=end;x++) L1[x] = (uint16_t)code;
} else {
int idx1 = (int)(v>>12);
uint32_t ord;
if(L1[idx1] < 47){
ord = l2cur; l2cur += 16;
uint16_t defc = L1[idx1];
for(int c=0;c<16;c++) L2[ord+c] = defc;
L1[idx1] = (uint16_t)(47 + ord);
} else {
ord = L1[idx1] - 47;
}
if(len <= 24){
int shift = 24 - (int)len;
int count = 1 << shift;
int mask = count - 1;
int base = (int)((v>>8) & ~(unsigned)mask), end = (int)((v>>8) | (unsigned)mask);
for(int x=base;x<=end;x++) L2[ord + (x & 15)] = (uint16_t)code;
} else {
int cell24 = (int)((v>>8) & 15);
if(L2[ord + cell24] != 0xFFFF){
uint16_t defc = L2[ord + cell24];
L2[ord + cell24] = 0xFFFF;
uint32_t b3 = l3cur; l3cur += 256;
for(int c=0;c<256;c++) L3[b3+c] = (uint8_t)defc;
d24_key[d24_cnt] = (uint32_t)(v>>8); d24_base[d24_cnt] = b3; d24_cnt++;
}
uint32_t b3 = d24_base[d24_cnt-1];
int shift = 32 - (int)len;
int count = 1 << shift;
int mask = count - 1;
int base = (int)(v & ~(unsigned)mask), end = (int)(v | (unsigned)mask);
for(int x=base;x<=end;x++) L3[b3 + (x & 255)] = (uint8_t)code;
}
}
}
free(rec);
}
unsigned query(unsigned addr){
unsigned ip = __builtin_bswap32(addr);
unsigned c = L1[ip>>12];
if(c < 47) return nh_tab[c];
c = L2[(c-47) + ((ip>>8)&15)];
if(c != 0xFFFF) return nh_tab[c];
unsigned key = ip>>8;
int lo=0, hi=(int)d24_cnt-1;
while(lo<=hi){
int mid=(lo+hi)>>1;
if(d24_key[mid]==key) return nh_tab[L3[d24_base[mid] + (ip & 255)]];
if(d24_key[mid]<key) lo=mid+1; else hi=mid-1;
}
return 0;
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 293.43 us | 2 MB + 152 KB | Accepted | Score: 25 | 显示更多 |
| Testcase #2 | 21.532 ms | 29 MB + 312 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #3 | 21.58 ms | 29 MB + 312 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #4 | 21.589 ms | 29 MB + 312 KB | Runtime Error | Score: 0 | 显示更多 |
Judge Duck Online | 评测鸭在线
Server Time: 2026-09-07 05:45:25 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠