#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define for_v(v) for (vector<int>::iterator i=v.begin(); i!=v.end(); i++)
const int N = 200032, M = 800032;
using namespace std;
int d[N], h[N], nx[M], to[M], w[M], e;
inline void adde(int u, int v, int _w) {
to[e] = v; w[e] = _w;
nx[e] = h[u]; h[u] = e++;
}
struct Node {
int d, i;
bool operator < (Node b) const {return d > b.d;}
};
priority_queue<Node> q;
inline void upd(int i, int w) {
if (d[i] > w) q.push((Node){d[i] = w, i});
}
void dijkstra() {
memset(d, 0x7f, sizeof d);
q.push((Node){d[1] = 0, 1});
while (!q.empty()) {
Node x = q.top(); q.pop();
if (x.d > d[x.i]) continue;
for (int i = h[x.i]; i; i = nx[i])
upd(to[i], x.d + w[i]);
}
}
struct sort_t {
int a, i;
bool operator < (sort_t s) const {return a > s.a;}
} seq[M>>1];
int f[N], fa[N], A[N], B[N], D[N];
vector<int> ch[N];
int find(int x) {return f[x] ? f[x] = find(f[x]) : x;}
inline void link(int i, int a) {
int x = find(to[i]), y = find(to[i^1]);
if (x != y) {
if (d[x] < d[y] || x == 1) swap(x,y);
f[x] = fa[x] = y; A[x] = a;
ch[y].push_back(x);
}
}
int son[N], in[N], top[N], tim;
int dfs1(int x) {
int sz = 1, mx = 0;
for_v (ch[x]) {
int s = dfs1(*i);
sz += s;
if (s > mx) mx = s, son[x] = *i;
}
return sz;
}
void dfs2(int x, int tp) {
top[x] = tp;
in[x] = ++tim;
B[tim] = A[son[x]];
D[tim] = d[x];
if (son[x]) {
dfs2(son[x], tp);
for_v (ch[x])
if (*i != son[x])
dfs2(*i, *i);
}
}
int Query(int x, int a) {
int tp = top[x];
while (A[tp] > a) tp = top[x = fa[tp]];
return D[upper_bound(B+in[tp], B+in[x], a) - B];
}
int main() {
int T, n, m, u, v, l, a, k, S, ans = 0;
scanf("%d",&T);
while (T--) {
scanf("%d%d",&n,&m);
e = 2; tim = 0;
memset(h, 0, sizeof h);
memset(f, 0, sizeof f);
memset(son, 0, sizeof son);
for (int i = 1; i <= n; i++)
ch[i].clear();
sort_t *p = seq;
while (m--) {
scanf("%d%d%d%d",&u,&v,&l,&a);
*p++ = (sort_t){a, e};
adde(u,v,l); adde(v,u,l);
}
dijkstra();
sort(seq, p);
for (sort_t *i = seq; i < p; i++)
link(i->i, i->a);
dfs1(1);
dfs2(1, 1);
scanf("%d%d%d",&m,&k,&S); S++;
while (m--) {
scanf("%d%d",&v,&a);
v = (v + k * ans - 1) % n + 1;
a = (a + k * ans) % S;
printf("%d\n",ans = Query(v,a));
}
}
return 0;
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 1.147 ms | 7 MB + 696 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #2 | 1.269 ms | 7 MB + 716 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #3 | 1.337 ms | 7 MB + 724 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #4 | 1.45 ms | 7 MB + 728 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #5 | 4.787 ms | 7 MB + 944 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #6 | 520.279 ms | 31 MB + 364 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #7 | 4.094 ms | 7 MB + 904 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #8 | 4.17 ms | 7 MB + 912 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #9 | 4.06 ms | 7 MB + 908 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #10 | 373.837 ms | 28 MB + 316 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #11 | 374.548 ms | 28 MB + 324 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #12 | 575.405 ms | 30 MB + 700 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #13 | 576.217 ms | 30 MB + 736 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #14 | 577.299 ms | 30 MB + 816 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #15 | 5.292 ms | 7 MB + 948 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #16 | 5.275 ms | 7 MB + 948 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #17 | 576.661 ms | 30 MB + 772 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #18 | 575.683 ms | 30 MB + 708 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #19 | 887.14 ms | 34 MB + 192 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #20 | 887.369 ms | 34 MB + 220 KB | Accepted | Score: 5 | 显示更多 |