100分的做法:
当p>sqrt(n)时, 这时候%p=c的点不超过sqrt(n)个, 直接倍增求路径最值就好了
当p<=sqrt(n)时,将询问离线, 按照p和c从小到大排序,同时处理出所有p相同且c相同的区间询问
对于同一个p[i]和c[i],将所有%p[i]=c[i]的点, 两两(c[j]和c[j]+p)求路径最大值, 并将这个相邻最大值更新到线段树上, 对于一个区间的询问, 直接从线段树上查[l,r]的区间最值
#pragma GCC optimize(2)
#include <cstdio>
#include <algorithm>
#include <vector>
#include <iterator>
#include <cmath>
#include <cstring>
#include <utility>
const int maxn = 5e4 + 5;
struct node
{
int u,v,w;
bool operator<(const node &x)const
{
return w<x.w;
}
}edge[200005];
std::vector<node> v;
std::vector<std::pair<int,int> > G[maxn];
int a[maxn],res[maxn],dep[maxn],fa[maxn],Fa[maxn][17],ma[maxn][17],n,m,q;
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]