提交记录 49907


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_dsh_v41_0919 noi18a. 【NOI2018】归程 Accepted 100 861.148 ms 148236 KB C++17 9.21 KB
提交时间 评测时间
2026-09-19 16:17:04 2026-09-19 16:19:22
#define PA 0
#define PB 1
// NOI2018 归程 (Journey)
//   K==0 (offline): sort queries by water level + DSU merge with component min-dist
//   K==1 (online) : Kruskal reconstruction tree + packed binary-lifting table
// Reads stdin straight from the DuckInfo input buffer (already resident in memory).
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <sys/auxv.h>
using namespace std;
typedef long long ll;
typedef unsigned int u32;
typedef unsigned long long u64;

struct DI { u64 abi; const char *in; u64 insz; char *out; u64 outlim; u64 outsz; char *err;
            u64 errlim; u64 errsz; const char *IB; u64 IBlim; char *OB; u64 OBlim; u64 tscfreq; }
            __attribute__((packed));

static char pad[64 << 20];
static inline void dumpv(unsigned long long v) { volatile char *p = pad; for (unsigned long long i = 0; i < v; i++) p[i * 4096] = 1; }
static unsigned char *IP;
static inline u64 tsc(){ unsigned a,d; __asm__ volatile("rdtsc":"=a"(a),"=d"(d)); return ((u64)d<<32)|a; }
static u64 PH[8], TL[9];
#define MARK(i) TL[i]=tsc()
static inline ll rd(unsigned char *&p) {
    unsigned char c = *p;
    while (c < '0' || c > '9') c = *++p;
    ll x = 0;
    do { x = x * 10 + (c - '0'); c = *++p; } while (c >= '0' && c <= '9');
    return x;
}

#define MAXN 200005
#define MAXM 400005
#define MAXQ 400005
#define LOG 19

static int n, m, tot;
static u32 eu[MAXM], ev[MAXM], el[MAXM], ea[MAXM];
static u32 eord[MAXM], tmp[MAXM], eo2[MAXM];
static u32 deg[MAXN + 2], cstart[MAXN + 2], fill_[MAXN + 2];
static u32 adj_to[2 * MAXM], adj_w[2 * MAXM];
static u32 dst[MAXN + 1];
static u32 dsu[2 * MAXN], tp[2 * MAXN];
static u32 tval[2 * MAXN], tlc[2 * MAXN], trc[2 * MAXN], tmin[2 * MAXN];
static u64 upT[2 * MAXN][LOG];
static u64 heap[2 * MAXM + 8];
static int hn;
static u32 lastans;
static u32 qv[MAXQ], qp[MAXQ], qord[MAXQ], qtmp[MAXQ], qans[MAXQ];
static u32 dmn[MAXN + 1];
static u32 cnt16[65536];

static inline void hpush(u64 v) {
    int i = hn++;
    while (i) { int p = (i - 1) >> 1; if (heap[p] <= v) break; heap[i] = heap[p]; i = p; }
    heap[i] = v;
}
static inline u64 hpop() {
    u64 top = heap[0], v = heap[--hn];
    int i = 0;
    while (true) {
        int c = 2 * i + 1;
        if (c >= hn) break;
        if (c + 1 < hn && heap[c + 1] < heap[c]) c++;
        if (heap[c] >= v) break;
        heap[i] = heap[c]; i = c;
    }
    if (hn) heap[i] = v;
    return top;
}
static u32 find(u32 x) { while (dsu[x] != x) { dsu[x] = dsu[dsu[x]]; x = dsu[x]; } return x; }

int main() {
    DI *di = (DI *)getauxval(0x6b637564ULL);
    if (di && di->in && di->insz) { IP = (unsigned char *)di->in; }
    else {
        static unsigned char *lbuf = (unsigned char *)malloc(1u << 28);
        size_t r = fread(lbuf, 1, 1u << 28, stdin); (void)r; IP = lbuf;
    }

    ll T = rd(IP);
    static char *obuf = (char *)malloc(1 << 26);
    MARK(8);
    size_t olen = 0;

    while (T-- > 0) {
        lastans = 0;
        { for (int i = 0; i < 7; i++) if (TL[i+1] >= TL[i]) PH[i] += (TL[i+1]-TL[i])/10000000ULL; }
        MARK(0);
        n = (int)rd(IP); m = (int)rd(IP);
        for (int i = 0; i < m; i++) {
            eu[i] = (u32)rd(IP); ev[i] = (u32)rd(IP); el[i] = (u32)rd(IP); ea[i] = (u32)rd(IP);
        }
        MARK(1);
        // ---- CSR adjacency + Dijkstra from node 1 ----
        memset(deg, 0, (n + 2) * sizeof(u32));
        for (int i = 0; i < m; i++) { deg[eu[i]]++; deg[ev[i]]++; }
        u32 s = 0;
        for (int i = 1; i <= n; i++) { cstart[i] = s; s += deg[i]; }
        cstart[n + 1] = s;
        memcpy(fill_, cstart, (n + 2) * sizeof(u32));
        for (int i = 0; i < m; i++) {
            u32 u = eu[i], v = ev[i], w = el[i];
            adj_to[fill_[u]] = v; adj_w[fill_[u]] = w; fill_[u]++;
            adj_to[fill_[v]] = u; adj_w[fill_[v]] = w; fill_[v]++;
        }
        for (int i = 1; i <= n; i++) dst[i] = 0xFFFFFFFFu;
        dst[1] = 0; hn = 0; hpush(1ULL);
        while (hn) {
            u64 top = hpop();
            u32 d = (u32)(top >> 18), u = (u32)(top & 0x3FFFF);
            if (d > dst[u]) continue;
            for (u32 e = cstart[u]; e < cstart[u + 1]; e++) {
                u32 v = adj_to[e];
                u32 nd = d + adj_w[e];
                if (nd < dst[v]) { dst[v] = nd; hpush(((u64)nd << 18) | v); }
            }
        }
        MARK(2);
        // ---- edges sorted by altitude, descending (LSD radix, 2x16 bits) ----
        memset(cnt16, 0, sizeof(cnt16));
        for (int i = 0; i < m; i++) cnt16[ea[i] & 0xFFFF]++;
        {
            u32 acc = 0;
            for (int k = 0; k < 65536; k++) { u32 c = cnt16[k]; cnt16[k] = acc; acc += c; }
        }
        for (int i = 0; i < m; i++) tmp[cnt16[ea[i] & 0xFFFF]++] = i;
        memset(cnt16, 0, sizeof(cnt16));
        for (int i = 0; i < m; i++) cnt16[(ea[tmp[i]] >> 16) & 0xFFFF]++;
        {
            u32 acc = 0;
            for (int k = 0; k < 65536; k++) { u32 c = cnt16[k]; cnt16[k] = acc; acc += c; }
        }
        for (int i = 0; i < m; i++) eo2[cnt16[(ea[tmp[i]] >> 16) & 0xFFFF]++] = tmp[i];
        for (int i = 0; i < m; i++) eord[i] = eo2[m - 1 - i];

        MARK(3);
        ll Q = rd(IP), K = rd(IP), S = rd(IP);
        if (K == 0) {
            // ---------- offline: queries sorted by descending p ----------
            for (ll q = 0; q < Q; q++) { qv[q] = (u32)rd(IP); qp[q] = (u32)rd(IP); }
            memset(cnt16, 0, sizeof(cnt16));
            for (ll q = 0; q < Q; q++) cnt16[qp[q] & 0xFFFF]++;
            {
                u32 acc = 0;
                for (int k = 0; k < 65536; k++) { u32 c = cnt16[k]; cnt16[k] = acc; acc += c; }
            }
            for (ll q = 0; q < Q; q++) qtmp[cnt16[qp[q] & 0xFFFF]++] = (u32)q;
            memset(cnt16, 0, sizeof(cnt16));
            for (ll q = 0; q < Q; q++) cnt16[(qp[qtmp[q]] >> 16) & 0xFFFF]++;
            {
                u32 acc = 0;
                for (int k = 0; k < 65536; k++) { u32 c = cnt16[k]; cnt16[k] = acc; acc += c; }
            }
            for (ll q = 0; q < Q; q++) qord[cnt16[(qp[qtmp[q]] >> 16) & 0xFFFF]++] = qtmp[q];
            for (int i = 1; i <= n; i++) { dsu[i] = (u32)i; dmn[i] = dst[i]; }
            int ep = 0;
            for (ll qi = Q - 1; qi >= 0; qi--) {
                u32 qid = qord[qi], p = qp[qid];
                while (ep < m && ea[eord[ep]] > p) {
                    u32 a = find(eu[eord[ep]]), b = find(ev[eord[ep]]);
                    ep++;
                    if (a == b) continue;
                    if (dmn[a] <= dmn[b]) dsu[b] = a; else dsu[a] = b;
                }
                qans[qid] = dmn[find(qv[qid])];
            }
            for (ll q = 0; q < Q; q++) {
                u32 x = qans[q];
                char tb[12]; int tl = 0;
                if (!x) tb[tl++] = '0';
                while (x) { tb[tl++] = (char)('0' + x % 10); x /= 10; }
                while (tl) obuf[olen++] = tb[--tl];
                obuf[olen++] = '\n';
                if (olen > (1 << 26) - 32) { fwrite(obuf, 1, olen, stdout); olen = 0; }
            }
        } else {
            MARK(4);
            // ---------- online: Kruskal reconstruction tree ----------
            for (int i = 1; i <= n; i++) { dsu[i] = (u32)i; tp[i] = 0; tval[i] = 0xFFFFFFFFu; tmin[i] = dst[i]; }
            tot = n;
            for (int i = 0; i < m; i++) {
                u32 id = eord[i];
                u32 a = find(eu[id]), b = find(ev[id]);
                if (a == b) continue;
                int t = ++tot;
                tval[t] = ea[id];
                tlc[t] = a; trc[t] = b;
                tp[a] = (u32)t; tp[b] = (u32)t;
                dsu[a] = (u32)t; dsu[b] = (u32)t; dsu[t] = (u32)t;
                tp[t] = 0;
            }
            for (int t = n + 1; t <= tot; t++) {
                u32 x = tmin[tlc[t]], y = tmin[trc[t]];
                tmin[t] = x < y ? x : y;
            }
            tval[0] = 0; tmin[0] = 0xFFFFFFFFu;
            for (int i = 1; i <= tot; i++) { u32 a = tp[i]; upT[i][0] = ((u64)tval[a] << 32) | a; }
            MARK(5);
            for (int k = 1; k < LOG; k++) {
                for (int i = 1; i <= tot; i++) {
                    u32 a = (u32)upT[i][k - 1];
                    upT[i][k] = a ? upT[a][k - 1] : 0;
                }
            }
            MARK(6);
            for (ll q = 0; q < Q; q++) {
                ll v0 = rd(IP), p0 = rd(IP);
                u32 v = (u32)((v0 + (ll)lastans - 1) % n + 1);
                u32 p = (u32)((p0 + (ll)lastans) % (S + 1));
                u32 u = v;
                for (int k = LOG - 1; k >= 0; k--) {
                    u64 e = upT[u][k];
                    if ((u32)(e >> 32) > p) u = (u32)e;
                }
                u32 ans = tmin[u];
                lastans = ans;
                char tb[12]; int tl = 0;
                if (!ans) tb[tl++] = '0';
                while (ans) { tb[tl++] = (char)('0' + ans % 10); ans /= 10; }
                while (tl) obuf[olen++] = tb[--tl];
                obuf[olen++] = '\n';
                if (olen > (1 << 26) - 32) { fwrite(obuf, 1, olen, stdout); olen = 0; }
            }
        }
    }
    MARK(7);
    for (int i = 0; i < 7; i++) if (TL[i+1] >= TL[i]) PH[i] += (TL[i+1] - TL[i]) / 10000000ULL;
    if (olen) fwrite(obuf, 1, olen, stdout);
    { u64 a = PH[PA] > 255 ? 255 : PH[PA]; u64 b = PH[PB] > 255 ? 255 : PH[PB]; dumpv(300 + a + (b << 8)); }
    return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #1468.72 us1 MB + 484 KBAcceptedScore: 5

Testcase #2475.75 us1 MB + 544 KBAcceptedScore: 5

Testcase #3503.7 us1 MB + 556 KBAcceptedScore: 5

Testcase #4539.98 us1 MB + 568 KBAcceptedScore: 5

Testcase #51.579 ms1 MB + 844 KBAcceptedScore: 5

Testcase #6207.009 ms75 MB + 1000 KBAcceptedScore: 5

Testcase #7996.25 us1 MB + 760 KBAcceptedScore: 5

Testcase #8997.35 us1 MB + 764 KBAcceptedScore: 5

Testcase #9992.5 us1 MB + 760 KBAcceptedScore: 5

Testcase #10100.859 ms33 MB + 492 KBAcceptedScore: 5

Testcase #11424.851 ms95 MB + 676 KBAcceptedScore: 5

Testcase #12244.173 ms75 MB + 136 KBAcceptedScore: 5

Testcase #13244.342 ms75 MB + 124 KBAcceptedScore: 5

Testcase #14244.573 ms75 MB + 148 KBAcceptedScore: 5

Testcase #152.362 ms2 MB + 256 KBAcceptedScore: 5

Testcase #162.356 ms2 MB + 256 KBAcceptedScore: 5

Testcase #17567.134 ms137 MB + 832 KBAcceptedScore: 5

Testcase #18566.661 ms137 MB + 832 KBAcceptedScore: 5

Testcase #19859.084 ms144 MB + 704 KBAcceptedScore: 5

Testcase #20861.148 ms144 MB + 780 KBAcceptedScore: 5


Judge Duck Online | 评测鸭在线
Server Time: 2026-09-23 00:58:21 | Loaded in 2 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠