提交时间:2022-10-12 17:04:17
运行 ID: 59458
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN=500005; int n,m,q,x,op,mod,c[MAXN],fa[MAXN],b[MAXN],t,ans[MAXN]; ll res[MAXN]; struct node { int u,v,w; bool operator<(const node b) const { return w<b.w; } } p[MAXN]; inline int Ffind(int x) { static int t,d; for(t=fa[x]; t^fa[t]; t=fa[t]); for(; x^fa[x]; d=fa[x],fa[x]=t,x=d); return t; } bitset<501>s[MAXN]; int main() { scanf("%d%d%d%d%d",&n,&m,&q,&x,&op); if(op==1) scanf("%d",&mod); for(int i=1; i<=n; ++i) scanf("%d",&c[i]),fa[i]=i,s[i][c[i]]=1; for(int i=1; i<=m; ++i) { scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].w); b[++t]=p[i].w; } sort(p+1,p+m+1),sort(b+1,b+t+1),t=unique(b+1,b+t+1)-b-1; b[++t]=2e9; for(int i=1; i<=m; ++i)p[i].w=lower_bound(b+1,b+t+1,p[i].w)-b; for(int i=1,j=1,u,v; i<=t; ++i) { for(; p[j].w==i&&j<=m; ++j) { u=Ffind(p[j].u),v=Ffind(p[j].v); if(u^v) { s[u]|=s[v]; fa[v]=u; } } ans[i]=s[Ffind(x)].count(); res[i]=ans[i]*1ll*(b[i+1]-b[i])+res[i-1];//res[i]: the answer of [1,b[i+1]-1] } ll lst=0; for(int i=1,l,r; i<=q; ++i) { scanf("%d%d",&l,&r); if(op==1) l=(l^lst)%mod+1,r=(r^lst)%mod+1; if(l>r)swap(l,r); int L=lower_bound(b+1,b+t+1,l)-b,R=lower_bound(b+1,b+t+1,r)-b; if(b[R]>r)--R; if(b[L]>l)--L; if(L==R) { printf("%lld\n",lst=ans[L]*(r-l+1ll)); } else { printf("%lld\n",lst=ans[L]*1ll*(b[L+1]-l)+ans[R]*(r-b[R]+1)+res[R-1]-res[L]); } //ans[l,r]= ans[l,b[L+1]-1]+ans[b[R],r]+ans[b[L+1],b[R]-1] } return 0; }