#include<cstring>
#include<cstdio>
#include<algorithm>
#define N 400100
#define ll long long
#define fo(i,a,b) for(int i=a,E=b;i<=E;i++)
#define M 20000010
using namespace std;
int l[N+N],v[N+N],nex[N+N],hd[N],top,heap[N],pos[N],d[N],K,n,m,q,a[N],t,S,lg[N];
int fa[N],to[N+N],nxt[N+N],fir[N],tot,cnt,son[M][2],left[M],right[M],opu,opv,opl,opr,rt[N];
int dfl[N],dfr[N],Top,sta[N];
ll f[N],mn[20][N];
bool vis[N];
struct road{int u,v,l,a;}r[N];
bool cmp(road a,road b){return a.a>b.a;}
void link(int x,int y,int val){
l[++top]=val;v[top]=y;nex[top]=hd[x];hd[x]=top;
}
void down(int x){
for(int y=x+x,z=y+1;y<=top;swap(heap[x],heap[y]),pos[heap[x]]=x,pos[heap[y]]=y,x=y,y=x+x,z=y+1){
if(f[heap[y]]>f[heap[z]])y=z;
if(f[heap[x]]<=f[heap[y]])break;
}
}
void up(int x){
for(int y=x>>1;y;swap(heap[x],heap[y]),pos[heap[x]]=x,pos[heap[y]]=y,x=y,y=x>>1)
if(f[heap[x]]>=f[heap[y]])break;
}
int get(){
int r=heap[1];pos[heap[top]]=1;heap[1]=heap[top--];down(1);return r;
}
void put(int x){
heap[++top]=x;pos[x]=top;up(top);
}
void dijk(){
fo(i,1,n)f[i]=1e18,d[i]=i,vis[i]=0;
f[1]=0;top=0;
fo(i,1,n)put(i);
fo(i,1,n){
int x=get();vis[x]=1;
for(int o=hd[x];o;o=nex[o])if(!vis[v[o]])
if(f[v[o]]>f[x]+l[o]){
f[v[o]]=f[x]+l[o];up(pos[v[o]]);
}
}
}
int find(int *a,int v,int l){
int h=1,t=l,m,r=l;
while(h<=t)if(a[m=h+t>>1]<=v)t=m-1,r=m;else h=m+1;
return r;
}
ll getmn(int h,int t){
int l=lg[t-h+1];
return min(mn[l][h],mn[l][t-(1<<l)+1]);
}
int get(int x){
for(;(x=fa[x])!=fa[x];)sta[++Top]=x;
while(Top)fa[sta[Top--]]=x;return x;
}
void con(int x,int y){
to[++top]=y;nxt[top]=fir[x];fir[x]=top;
}
void dfs(int x){
dfl[x]=dfr[x]=++top;
for(int i=fir[x];i;i=nxt[i])dfs(to[i]),dfr[x]=dfr[to[i]];
}
void build(int &w,int h,int t){
w=++cnt;
if(h==t){
left[w]=h;right[w]=t;return;
}left[w]=right[w]=-1;
int m=h+t>>1;
build(son[w][0],h,m);build(son[w][1],m+1,t);
}
void cover(int &u,int v,int h,int t){
u=++cnt;
if(opl<=h && opr>=t){
left[u]=opl;right[u]=opr;return;
}son[u][0]=son[v][0];son[u][1]=son[v][1];
left[u]=right[u]=-1;
int m=h+t>>1;
if(opl<=m)cover(son[u][0],son[v][0],h,m);
if(opr>m)cover(son[u][1],son[v][1],m+1,t);
}
void query(int w,int h,int t){
if(left[w]!=-1){
opu=left[w];opv=right[w];return;
}int m=h+t>>1;
if(opl<=m)query(son[w][0],h,m);else query(son[w][1],m+1,t);
}
void pre(){
top=cnt=0;tot=n;
fo(i,1,n+n)fa[i]=i;
memset(fir,0,sizeof fir);
fo(i,1,m){
int u=r[i].u,v=r[i].v,A=get(u),B=get(v);
if(A==B)continue;f[++tot]=1e18;fa[A]=tot;fa[B]=tot;con(tot,A);con(tot,B);
}top=0;dfs(tot);
fo(i,1,tot)mn[0][dfl[i]]=f[i];
fo(j,1,19)fo(i,1,tot)if(tot-i+1>=(1<<j))mn[j][i]=min(mn[j-1][i],mn[j-1][i+(1<<j-1)]);
fo(i,1,n+n)fa[i]=i;
build(rt[1],1,tot);
int las=rt[1],tmp=n,w=1;
fo(i,1,t){
if(i>1)rt[i]=rt[i-1];
while(w<=m && r[w].a>a[i]){
int u=r[w].u,v=r[w].v,A=get(u),B=get(v);w++;
if(A==B)continue;++tmp;fa[A]=tmp;fa[B]=tmp;
opl=opu=dfl[tmp];opr=opv=dfr[tmp];
cover(rt[i],las,1,tot);las=rt[i];
}
}
}
int main(){
int T;scanf("%d",&T);
lg[1]=0;
fo(i,2,N-1)lg[i]=lg[i>>1]+1;
while(T--){
scanf("%d %d",&n,&m);
top=0;
memset(hd,0,sizeof hd);
fo(i,1,m){
int fr,to,len,lev;scanf("%d %d %d %d",&fr,&to,&len,&lev);
a[i]=lev;r[i]=(road){fr,to,len,lev};link(fr,to,len);link(to,fr,len);
}
sort(r+1,r+m+1,cmp);sort(a+1,a+m+1);fo(i,1,m/2)swap(a[i],a[m+1-i]);
fo(i,1,m)if(i==1)t=1;else if(a[i]^a[i-1])a[++t]=a[i];
a[++t]=0;
dijk();pre();
scanf("%d %d %d",&q,&K,&S);
ll las=0;
fo(i,1,q){
ll u,lev;scanf("%lld %lld",&u,&lev);
u=(u+K*las-1)%n+1;lev=(lev+K*las)%(S+1);
if(i==28){
int yyy=0;
yyy=1;
}
opl=dfl[u];query(rt[find(a,(int)lev,t)],1,tot);
printf("%lld\n",las=getmn(opu,opv));
}
}
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 697.9 us | 4 MB + 664 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 724.29 us | 4 MB + 700 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 882.81 us | 4 MB + 728 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 1.093 ms | 4 MB + 784 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 5.749 ms | 5 MB + 812 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 920.543 ms | 187 MB + 844 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 5.121 ms | 5 MB + 776 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 5.12 ms | 5 MB + 780 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 5.118 ms | 5 MB + 780 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 696.216 ms | 181 MB + 80 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 696.974 ms | 181 MB + 108 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 1.019 s | 234 MB + 240 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 1.02 s | 234 MB + 356 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 1.02 s | 234 MB + 200 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 6.863 ms | 5 MB + 960 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 6.848 ms | 5 MB + 972 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 1.019 s | 234 MB + 256 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 1.019 s | 234 MB + 340 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 1.601 s | 237 MB + 948 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 1.599 s | 237 MB + 604 KB | Accepted | Score: 5 | 显示更多 |