#include<cstdio>
#include<cctype>
#include<vector>
#include<climits>
#include<algorithm>
#include<functional>
#include<ext/pb_ds/priority_queue.hpp>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=2e5+1,S=1e9,M=4e5,Q=4e5;
struct Edge {
int to,l,a;
};
std::vector<Edge> e[N];
inline void add_edge(const int &u,const int &v,const int &l,const int &a) {
e[u].push_back((Edge){v,l,a});
e[v].push_back((Edge){u,l,a});
}
int n,m,q,k,s;
void reset() {
for(register int i=1;i<=n;i++) e[i].clear();
}
namespace subtask_same_elevation {
int elevation,dis[N];
struct Vertex {
int w,id;
bool operator > (const Vertex &rhs) const {
return w>rhs.w;
}
};
void dijkstra() {
static __gnu_pbds::priority_queue<Vertex,std::greater<Vertex> > q;
static __gnu_pbds::priority_queue<Vertex,std::greater<Vertex> >::point_iterator p[N];
for(register int i=1;i<=n;i++) {
p[i]=q.push((Vertex){dis[i]=i==1?0:INT_MAX,i});
}
while(!q.empty()) {
const int x=q.top().id;
q.pop();
for(register unsigned i=0;i<e[x].size();i++) {
const int &y=e[x][i].to,&w=e[x][i].l;
if(dis[x]+w<dis[y]) {
q.modify(p[y],(Vertex){dis[y]=dis[x]+w,y});
}
}
}
}
};
namespace subtask_tree {
int dep[N],sum[N];
class SegmentTree {
#define mid ((b+e)>>1)
private:
static const int SIZE=N*40;
struct Node {
int val,left,right;
};
Node node[SIZE];
int sz,new_node(const int &p) {
node[++sz]=node[p];
return sz;
}
public:
int root[N];
void reset() {
sz=0;
}
void insert(int &p,const int &b,const int &e,const int &x,const int &y) {
p=new_node(p);
if(dep[y]>=dep[node[p].val]) {
node[p].val=y;
}
if(b==e) return;
if(x<=mid) insert(node[p].left,b,mid,x,y);
if(x>mid) insert(node[p].right,mid+1,e,x,y);
}
int query(const int &p,const int &b,const int &e,const int &x) {
if(e==x) return node[p].val;
if(x<=mid) {
return query(node[p].left,b,mid,x);
} else {
const int u=query(node[p].left,b,mid,mid);
const int v=query(node[p].right,mid+1,e,x);
return dep[u]>dep[v]?u:v;
}
}
#undef mid
};
SegmentTree t;
void dfs(const int &x,const int &par) {
for(unsigned i=0;i<e[x].size();i++) {
const int &y=e[x][i].to,&l=e[x][i].l,&a=e[x][i].a;
if(y==par) continue;
sum[y]=sum[x]+l;
dep[y]=dep[x]+1;
t.insert(t.root[y]=t.root[x],0,S,a,y);
dfs(y,x);
}
}
};
namespace subtask_offline {
using namespace subtask_same_elevation;
struct Edge2 {
int u,v,w;
bool operator > (const Edge2 &rhs) const {
return w>rhs.w;
}
};
struct Query {
int v,p,id;
bool operator > (const Query &rhs) const {
return p>rhs.p;
}
};
int ans[Q];
Edge2 edge[M];
Query que[Q];
class DisjointSet {
private:
int anc[N],val[N];
int find(const int &x) {
return x==anc[x]?x:anc[x]=find(anc[x]);
}
public:
void init() {
for(register int i=1;i<=n;i++) {
anc[i]=i;
val[i]=dis[i];
}
}
void merge(const int &x,const int &y) {
const int p=find(x),q=find(y);
val[q]=std::min(val[q],val[p]);
anc[p]=q;
}
bool same(const int &x,const int &y) {
return find(x)==find(y);
}
int query(const int &x) {
return val[find(x)];
}
};
DisjointSet djs;
};
namespace subtask_n2 {
using namespace subtask_same_elevation;
using namespace subtask_offline;
};
int main() {
for(register int T=getint();T;T--) {
n=getint(),m=getint();
bool is_tree=m==n-1;
bool same_elevation=true;
for(register int i=0;i<m;i++) {
const int u=getint(),v=getint(),l=getint(),a=getint();
subtask_offline::edge[i]=(subtask_offline::Edge2){u,v,a};
if(i!=0&&subtask_same_elevation::elevation!=a) same_elevation=false;
add_edge(u,v,l,subtask_same_elevation::elevation=a);
}
q=getint(),k=getint(),s=getint();
bool offline=k==0;
if(q==0) goto Next;
if(same_elevation&&offline) {
using namespace subtask_same_elevation;
dijkstra();
for(register int i=0;i<q;i++) {
const int v=getint(),p=getint();
printf("%d\n",p<elevation?0:dis[v]);
}
goto Next;
}
if(is_tree) {
using namespace subtask_tree;
dfs(1,0);
for(register int i=0,ans=0;i<q;i++) {
const int v=(getint()+k*ans-1)%n+1;
const int p=(getint()+k*ans)%(s+1);
printf("%d\n",ans=sum[t.query(t.root[v],0,S,p)]);
}
t.reset();
goto Next;
}
if(offline) {
using namespace subtask_offline;
dijkstra();
std::sort(&edge[0],&edge[m],std::greater<Edge2>());
for(register int i=0;i<q;i++) {
const int v=getint(),p=getint();
que[i]=(Query){v,p,i};
}
std::sort(&que[0],&que[q],std::greater<Query>());
djs.init();
for(register int i=0,j=0;j<q;j++) {
for(;edge[i].w>que[j].p;i++) {
const int &u=edge[i].u,&v=edge[i].v;
if(djs.same(u,v)) continue;
djs.merge(u,v);
}
ans[que[j].id]=djs.query(que[j].v);
}
for(register int i=0;i<q;i++) {
printf("%d\n",ans[i]);
}
goto Next;
}
if(n<=1500&&m<=4000&&q<=2000) {
using namespace subtask_n2;
dijkstra();
for(register int i=0,last;i<q;i++) {
const int v=(getint()+k*last-1)%n+1;
const int p=(getint()+k*last)%(s+1);
djs.init();
for(register int i=0;i<m;i++) {
if(edge[i].w<=p) continue;
const int &u=edge[i].u,&v=edge[i].v;
if(djs.same(u,v)) continue;
djs.merge(u,v);
}
printf("%d\n",last=djs.query(v));
}
goto Next;
}
Next:
reset();
}
return 0;
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 722.81 us | 4 MB + 620 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #2 | 1.118 ms | 6 MB + 140 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #3 | 1.056 ms | 6 MB + 152 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #4 | 1.149 ms | 6 MB + 160 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #5 | 3.76 ms | 6 MB + 448 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #6 | 594.524 ms | 41 MB + 84 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #7 | 5.386 ms | 5 MB + 520 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #8 | 5.632 ms | 5 MB + 524 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #9 | 5.409 ms | 5 MB + 520 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #10 | 605.307 ms | 119 MB + 148 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #11 | 604.635 ms | 119 MB + 152 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #12 | 750.228 ms | 43 MB + 732 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #13 | 749.057 ms | 43 MB + 732 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #14 | 749.484 ms | 43 MB + 740 KB | Accepted | Score: 5 | 显示更多 |
| Testcase #15 | 211.326 ms | 6 MB + 456 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #16 | 207.622 ms | 6 MB + 456 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #17 | 86.394 ms | 24 MB + 832 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #18 | 86.467 ms | 24 MB + 840 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #19 | 86.466 ms | 24 MB + 832 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #20 | 86.493 ms | 24 MB + 828 KB | Runtime Error | Score: 0 | 显示更多 |