提交时间:2022-10-12 17:00:11
运行 ID: 59452
#include <iostream> #include <cstdio> #include <vector> #include <cstring> #include <algorithm> //#include <windows.h> using namespace std; inline int read() { int x=0,c=getchar(); while(c<48||c>57)c=getchar(); while(c>=48&&c<=57)x=(x<<3)+(x<<1)+(c&15),c=getchar(); return x; } const int maxn=500006; long long lst=0,ret; int n,m,q,x,opt,mod; struct line { int to,w; }; int typ[maxn]; vector<line>edge[maxn]; bool vis1[maxn]; bool vis2[maxn]; bool use[maxn]; int history[maxn]; void run(int u,int k) { //printf("u=%d k=%d typ[%d]=%d\n",u,k,u,typ[u]); if(!vis1[typ[u]])vis1[typ[u]]=1,++ret; for(int i=0;i<edge[u].size();++i) { line v=edge[u][i]; if(!vis2[v.to]&&v.w<=k) { vis2[v.to]=1; run(v.to,k); } } } int main() { //freopen("sample.in","r",stdin); //freopen("sample.out","w",stdout); memset(use,0,sizeof(use)); n=read();m=read();q=read();x=read();opt=read(); if(opt)mod=read(); for(int i=1;i<=n;++i)typ[i]=read(); for(int i=0;i<m;++i) { int u=read(),v=read(),w=read(); edge[u].push_back({v,w}); edge[v].push_back({u,w}); } /*for(int i=1;i<=6;++i) { ret=0; memset(vis1,0,sizeof(vis1)); memset(vis2,0,sizeof(vis2)); run(x,i); printf("i=%d ret=%d\n",i,ret); }*/ int l,r; for(int i=0;i<q;++i) { l=read();r=read(); if(opt)l=(l^lst)%mod+1,r=(r^lst)%mod+1; if(l>r)swap(l,r); long long ans=0; for(int j=l;j<=r;++j) { if(!use[j]) { memset(vis1,0,sizeof(vis1)); memset(vis2,0,sizeof(vis2)); ret=0; use[j]=1;run(x,j); history[j]=ret; } ans+=history[j]; } lst=ans; printf("%lld\n",ans); } //for(int i=1;i<10;++i)system("pause"); return 0; }