提交记录 31566


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_dsh_260814 noi18a. 【NOI2018】归程 Accepted 100 846.074 ms 58700 KB C++17 5.75 KB
提交时间 评测时间
2026-08-14 10:00:09 2026-08-14 10:00:24
// NOI2018 归程 (noi18a) - v3: node-major lifting, binary heap, fast 32-bit mod.
#include <sys/auxv.h>
#include <stdint.h>
#include <string.h>

typedef unsigned int u32;
typedef unsigned long long u64;

#define MAXN 200002
#define MAXM 400002
#define LOG 18

struct DuckInfo {
  u64 abi_version;
  const char *stdin_ptr; u64 stdin_size;
  char *stdout_ptr; u64 stdout_limit; u64 stdout_size;
  char *stderr_ptr; u64 stderr_limit; u64 stderr_size;
  const char *IB_ptr; u64 IB_limit;
  char *OB_ptr; u64 OB_limit;
  u64 tsc_frequency;
} __attribute__((packed));

static const char *sp;

static inline int rd() {
  const char *p = sp;
  while (*p < '0' || *p > '9') p++;
  int x = 0;
  while (*p >= '0' && *p <= '9') { x = x * 10 + (*p - '0'); p++; }
  sp = p;
  return x;
}

static int head[MAXN];
static int to[2 * MAXM];
static u32 wt[2 * MAXM];
static int nxt[2 * MAXM];

static int eu[MAXM], ev[MAXM];
static u32 el[MAXM], ea[MAXM];

static int order[MAXM], tmp[MAXM];

static u32 dist[MAXN];
static u64 heap[2 * MAXM + 2];

static int dsu[MAXN], sz[MAXN], rep[MAXN];
static u32 val[2 * MAXN];
static u32 mn[2 * MAXN];
static int up[2 * MAXN][LOG];

static char *op;

static inline void put_u32(u32 x) {
  char *o = op;
  if (x == 0) { *o++ = '0'; *o++ = '\n'; op = o; return; }
  char buf[16];
  int n = 0;
  while (x) { buf[n++] = (char)('0' + (x % 10)); x /= 10; }
  while (n) *o++ = buf[--n];
  *o++ = '\n';
  op = o;
}

static inline int find(int x) {
  int r = x;
  while (dsu[r] != r) r = dsu[r];
  while (dsu[x] != x) { int nx = dsu[x]; dsu[x] = r; x = nx; }
  return r;
}

int main() {
  struct DuckInfo *di = (struct DuckInfo *)getauxval(0x6b637564);
  sp = di->stdin_ptr;
  op = di->stdout_ptr;

  int T = rd();
  while (T--) {
    int n = rd(), m = rd();
    for (int i = 0; i < m; i++) {
      eu[i] = rd(); ev[i] = rd(); el[i] = (u32)rd(); ea[i] = (u32)rd();
    }
    int Q = rd(), K = rd();
    u32 S = (u32)rd();

    for (int i = 1; i <= n; i++) head[i] = 0;
    int ecnt = 0;
    for (int i = 0; i < m; i++) {
      int u = eu[i], v = ev[i];
      u32 l = el[i];
      to[++ecnt] = v; wt[ecnt] = l; nxt[ecnt] = head[u]; head[u] = ecnt;
      to[++ecnt] = u; wt[ecnt] = l; nxt[ecnt] = head[v]; head[v] = ecnt;
    }

    for (int i = 1; i <= n; i++) dist[i] = 0xFFFFFFFFu;
    dist[1] = 0;
    int hsz = 1;
    heap[1] = 1ull;
    while (hsz) {
      u64 key = heap[1];
      int u = (int)(key & 0xFFFFFFFFu);
      u32 d = (u32)(key >> 32);
      if (d != dist[u]) {
        u64 last = heap[hsz--];
        int i = 1;
        while (1) {
          int l = i << 1;
          if (l > hsz) break;
          int r = l + 1;
          int c = (r <= hsz && heap[r] < heap[l]) ? r : l;
          if (last <= heap[c]) break;
          heap[i] = heap[c]; i = c;
        }
        heap[i] = last;
        continue;
      }
      {
        u64 last = heap[hsz--];
        int i = 1;
        while (1) {
          int l = i << 1;
          if (l > hsz) break;
          int r = l + 1;
          int c = (r <= hsz && heap[r] < heap[l]) ? r : l;
          if (last <= heap[c]) break;
          heap[i] = heap[c]; i = c;
        }
        heap[i] = last;
      }
      for (int e = head[u]; e; e = nxt[e]) {
        int v = to[e];
        u32 nd = d + wt[e];
        if (nd < dist[v]) {
          dist[v] = nd;
          u64 nk = ((u64)nd << 32) | (u64)v;
          int i = ++hsz;
          while (i > 1) {
            int par = i >> 1;
            if (heap[par] <= nk) break;
            heap[i] = heap[par]; i = par;
          }
          heap[i] = nk;
        }
      }
    }

    for (int i = 1; i <= n; i++) {
      dsu[i] = i; sz[i] = 1; rep[i] = i;
      mn[i] = dist[i];
      val[i] = 0;
      up[i][0] = 0;
    }
    for (int i = 0; i < m; i++) order[i] = i;

    {
      int *src = order, *dst = tmp;
      for (int shift = 0; shift < 32; shift += 8) {
        int cnt[256];
        memset(cnt, 0, sizeof(cnt));
        for (int i = 0; i < m; i++) cnt[(ea[src[i]] >> shift) & 0xff]++;
        int sum = 0;
        for (int c = 0; c < 256; c++) { int t = cnt[c]; cnt[c] = sum; sum += t; }
        for (int i = 0; i < m; i++) dst[cnt[(ea[src[i]] >> shift) & 0xff]++] = src[i];
        int *t = src; src = dst; dst = t;
      }
      if (src != order) {
        for (int i = 0; i < m; i++) order[i] = tmp[i];
      }
    }

    int tot = n;
    for (int idx = m - 1; idx >= 0; idx--) {
      int e = order[idx];
      u32 a = ea[e];
      int ru = find(eu[e]), rv = find(ev[e]);
      if (ru == rv) continue;
      ++tot;
      val[tot] = a;
      int t1 = rep[ru], t2 = rep[rv];
      up[t1][0] = tot;
      up[t2][0] = tot;
      u32 m1 = mn[t1], m2 = mn[t2];
      mn[tot] = (m1 < m2) ? m1 : m2;
      if (sz[ru] < sz[rv]) { int t = ru; ru = rv; rv = t; }
      dsu[rv] = ru; sz[ru] += sz[rv]; rep[ru] = tot;
    }
    up[tot][0] = 0;

    for (int j = 1; j < LOG; j++) {
      for (int i = 1; i <= tot; i++) up[i][j] = up[up[i][j - 1]][j - 1];
    }

    if (K == 0) {
      for (int q = 0; q < Q; q++) {
        int v = rd();
        int p = rd();
        int cur = v;
        for (int j = LOG - 1; j >= 0; j--) {
          int anc = up[cur][j];
          if (val[anc] > (u32)p) cur = anc;
        }
        put_u32(mn[cur]);
      }
    } else {
      u32 lastans = 0;
      u32 sp1 = S + 1;
      for (int q = 0; q < Q; q++) {
        int v0 = rd(), p0 = rd();
        int v = (int)(((u32)v0 + lastans - 1u) % (u32)n) + 1;
        int p = (int)(((u32)p0 + lastans) % sp1);
        int cur = v;
        for (int j = LOG - 1; j >= 0; j--) {
          int anc = up[cur][j];
          if (val[anc] > (u32)p) cur = anc;
        }
        lastans = mn[cur];
        put_u32(lastans);
      }
    }
  }

  di->stdout_size = (u64)(op - di->stdout_ptr);
  __asm__ __volatile__("mov $60, %%rax; xor %%rdi, %%rdi; syscall" ::: "rax", "rdi", "memory");
  __builtin_unreachable();
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #19.5 us44 KBAcceptedScore: 5

Testcase #217.14 us88 KBAcceptedScore: 5

Testcase #369.15 us100 KBAcceptedScore: 5

Testcase #4134.87 us116 KBAcceptedScore: 5

Testcase #51.799 ms564 KBAcceptedScore: 5

Testcase #6427.37 ms54 MB + 280 KBAcceptedScore: 5

Testcase #71.125 ms456 KBAcceptedScore: 5

Testcase #81.117 ms460 KBAcceptedScore: 5

Testcase #91.114 ms456 KBAcceptedScore: 5

Testcase #10371.493 ms46 MB + 72 KBAcceptedScore: 5

Testcase #11372.586 ms46 MB + 76 KBAcceptedScore: 5

Testcase #12545.491 ms53 MB + 868 KBAcceptedScore: 5

Testcase #13544.823 ms53 MB + 864 KBAcceptedScore: 5

Testcase #14544.997 ms53 MB + 876 KBAcceptedScore: 5

Testcase #151.854 ms556 KBAcceptedScore: 5

Testcase #161.848 ms556 KBAcceptedScore: 5

Testcase #17550.381 ms53 MB + 868 KBAcceptedScore: 5

Testcase #18550.41 ms53 MB + 868 KBAcceptedScore: 5

Testcase #19843.722 ms57 MB + 292 KBAcceptedScore: 5

Testcase #20846.074 ms57 MB + 332 KBAcceptedScore: 5


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