提交记录 4150


用户 题目 状态 得分 用时 内存 语言 代码长度
tkandi noi18a. 【NOI2018】归程 Accepted 100 2.36 s 181136 KB C++ 4.29 KB
提交时间 评测时间
2018-07-18 21:35:44 2020-07-31 22:33:25
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cctype>

using namespace std;

int Read() {
    char c;
    while (c = getchar(), !isdigit(c));
    int x = c - 48;
    while (c = getchar(), isdigit(c)) x = x * 10 + c - 48;
    return x;
}

void Write(int x) {
    int a[10], k = 0;
    do a[++k] = x % 10;
    while (x /= 10);
    while (k) putchar(a[k--] + 48);
    putchar('\n');
    return;
}

typedef pair<int, int> PII;

#define fi first
#define se second
#define mp make_pair

const int N = 200010, M = 400010;

int n, m, q, k, s;
int tot, head[N];
struct Edge {
    int p, nxt, w;
    Edge(int p = 0, int nxt = 0, int w = 0) : p(p), nxt(nxt), w(w) {}
} edge[M * 2];
inline void Add(int u, int v, int w) {
    edge[++tot] = Edge(v, head[u], w);
    head[u] = tot;
    return;
}
void Init() {
    tot = -1;
    memset(head, -1, sizeof(head));
    return;
}
struct Node {
    int u, v, w;
    bool operator < (const Node &a) const {
        return w < a.w;
    }
} a[M];

vector<int> va;

priority_queue<PII> pq;
int d[N];

void Dijkstra() {
    memset(d, 0x7f, sizeof(d));
    pq.push(mp(-(d[1] = 0), 1));
    while (!pq.empty()) {
        PII cur = pq.top();
        pq.pop();
        int u = cur.se;
        if (d[u] < -cur.fi) continue;
        for (int i = head[u]; ~i; i = edge[i].nxt) {
            int v = edge[i].p, w = edge[i].w;
            if (d[v] > d[u] + w) pq.push(mp(-(d[v] = d[u] + w), v));
        }
    }
    return;
}

namespace ST {

    int tot;
    struct Node {
        int lc, rc, l, r, t;
    } p[N * 4 + M * 40];
    void Init() {
        tot = 0;
        return;
    }

    void Build(int &k, int l, int r) {
        k = ++tot;
        p[k].l = l;
        p[k].r = r;
        if (l == r) {
            p[k].t = -d[l];
            return;
        }
        int m = (l + r) / 2;
        Build(p[k].lc, l, m);
        Build(p[k].rc, m + 1, r);
        return;
    }

    void Modify(int &k, int _k, int x, int t) {
        p[k = ++tot] = p[_k];
        if (p[k].l == p[k].r) {
            p[k].t = t;
            return;
        }
        if (x <= p[p[k].lc].r) Modify(p[k].lc, p[_k].lc, x, t);
        else Modify(p[k].rc, p[_k].rc, x, t);
        return;
    }

    int Query(int k, int x) {
        while (p[k].l != p[k].r)
            k = (x <= p[p[k].lc].r) ? (p[k].lc) : (p[k].rc);
        return p[k].t;
        /*
        if (p[k].l == p[k].r) return p[k].t;
        if (x <= p[p[k].lc].r) return Query(p[k].lc, x);
        return Query(p[k].rc, x);
        */
    }
    
}

int Gf(int v, int k) {
    int t = ST::Query(v, k);
    return (t <= 0) ? -t : Gf(v, t);
}

int f[N], size[N];
int Gf(int k) {
    return (f[k] != k) ? (f[k] = Gf(f[k])) : k;
}

int root[M];

void Solve() {
    scanf("%d%d", &n, &m);
    Init();
    va.clear();
    for (int i = 1; i <= m; ++i) {
        int u = Read(), v = Read(), l = Read(), a = Read();
        //int u, v, l, a;
        //scanf("%d%d%d%d", &u, &v, &l, &a);
        Add(u, v, l);
        Add(v, u, l);
        ::a[i].u = u;
        ::a[i].v = v;
        ::a[i].w = a;
        va.push_back(a);
    }
    sort(a + 1, a + m + 1);
    sort(va.begin(), va.end());
    va.erase(unique(va.begin(), va.end()), va.end());
    Dijkstra();
    int t = va.size();
    ST::Init();
    ST::Build(root[t], 1, n);
    for (int i = 1; i <= n; ++i) {
        f[i] = i;
        size[i] = 1;
    }
    for (int i = t - 1, j = m; ~i; --i) {
        root[i] = root[i + 1];
        for (; j && a[j].w >= va[i]; --j) {
            int x = Gf(a[j].u), y = Gf(a[j].v);
            if (x != y) {
                if (size[x] < size[y]) swap(x, y);
                f[y] = x;
                size[x] += size[y];
                d[x] = min(d[x], d[y]);
                ST::Modify(root[i], root[i], y, x);
                ST::Modify(root[i], root[i], x, -d[x]);
            }
        }
    }
    int q, k, s, la = 0;
    scanf("%d%d%d", &q, &k, &s);
    while (q--) {
        int v = Read(), p = Read();
        //int v, p;
        //scanf("%d%d", &v, &p);
        v = (v + k * la - 1) % n + 1;
        p = (p + k * la) % (s + 1);
        int j = upper_bound(va.begin(), va.end(), p) - va.begin();
        la = Gf(root[j], v);
        Write(la);
        //printf("%d\n", la);
    }
    return;
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) Solve();
    return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #11.512 ms10 MB + 740 KBAcceptedScore: 5

Testcase #21.522 ms10 MB + 744 KBAcceptedScore: 5

Testcase #31.615 ms10 MB + 764 KBAcceptedScore: 5

Testcase #41.727 ms10 MB + 792 KBAcceptedScore: 5

Testcase #54.928 ms11 MB + 580 KBAcceptedScore: 5

Testcase #6962.464 ms171 MB + 960 KBAcceptedScore: 5

Testcase #74.965 ms11 MB + 548 KBAcceptedScore: 5

Testcase #84.971 ms11 MB + 552 KBAcceptedScore: 5

Testcase #94.98 ms11 MB + 548 KBAcceptedScore: 5

Testcase #101.249 s169 MB + 736 KBAcceptedScore: 5

Testcase #111.247 s169 MB + 748 KBAcceptedScore: 5

Testcase #121.23 s171 MB + 708 KBAcceptedScore: 5

Testcase #131.233 s173 MB + 192 KBAcceptedScore: 5

Testcase #141.233 s173 MB + 216 KBAcceptedScore: 5

Testcase #156.649 ms11 MB + 584 KBAcceptedScore: 5

Testcase #166.609 ms11 MB + 588 KBAcceptedScore: 5

Testcase #171.234 s173 MB + 380 KBAcceptedScore: 5

Testcase #181.235 s173 MB + 316 KBAcceptedScore: 5

Testcase #192.36 s175 MB + 120 KBAcceptedScore: 5

Testcase #202.351 s176 MB + 912 KBAcceptedScore: 5


Judge Duck Online | 评测鸭在线
Server Time: 2024-12-05 10:19:43 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠