Tsiying has a sequence of positive integers with a length of n and quickly calculates all the factors of each number. In order to exercise his factor calculation ability, he has selected q consecutive subsequences from the sequence and found a positive integer p greater than 1 for each subsequence, so that p can divide as many numbers in this subsequence as possible. He has also found that p may have more than one.
So the question is, how many numbers in each subsequence can be divided at most?
Input
The first line contains an integer T (1≤T≤5×104), indicating that there is T test cases next.
The first line of each test cases has two positive integers n (1≤n≤5×104), q (1≤q≤5×104).
Next line n integers ai (1≤i≤n,1≤ai≤1×106), which representing the numbers in this sequence. The two adjacent numbers are separated by a space.
Each of the next q lines contains two integers l,r (1≤l≤r≤n), representing a subsequence being queried, al,al+1,⋯,ar, and l,r are separated by a space.
The input guarantees that the sum of n does not exceed 5×104 and the sum of q does not exceed 5×104.
Output
For each test case, output q lines, each line contains a positive integer, indicating the answer.
有n个人组成有n-1条边的无根树,点权代表自己做作业的时间,边权代表去抄作业需要花的时间。
问让所有人都做完作业需要花费的时间。
f
[
u
]
=
m
a
x
(
w
[
u
]
,
f
[
v
]
+
d
(
u
,
v
)
)
f[u]=max(w[u], f[v]+d(u,v))
f[u]=max(w[u],f[v]+d(u,v))
换根dp。
#include<bits/stdc++.h>
using namespace std;
#define read(x) scanf("%d",&x)
#define maxn int(1e5)
#define ll long long
struct Edge{
int y,z;
Edge(){}
Edge(int _y,int _z) {y=_y,z=_z;}
};
struct ID{
int x,y,p1,p2;
ID() {}
ID(int _x,int _y,int _p1,int _p2) {x=_x,y=_y,p1=_p1,p2=_p2;}
};
int n,q;
int w[maxn+5];
vector<Edge> a[maxn+5];
ID id[maxn+5]; //第i条边在vector中的位置
ll f[maxn+5]; //完成作业的最小时间
void dfs1(int x,int fa) {
f[x]=w[x]; //自己做作业
for(int i=0;i<a[x].size();i++) {
int y=a[x][i].y,z=a[x][i].z;
if(y==fa) continue;
dfs1(y,x);
f[x]=min(f[x],f[y]+z);
}
}
void dfs2(int x,int fa) {
for(int i=0;i<a[x].size();i++) {
int y=a[x][i].y,z=a[x][i].z;
if(y==fa) continue;
f[y]=min(f[y],f[x]+z);
dfs2(y,x);
}
}
int main() {
read(n),read(q);
for(int i=1;i<=n;i++) read(w[i]);
for(int i=1;i<n;i++) {
int x,y,z;
read(x),read(y),read(z);
a[x].push_back(Edge(y,z));
a[y].push_back(Edge(x,z));
id[i]=ID(x,y,a[x].size()-1,a[y].size()-1);
}
while(q--) {
int opr;
read(opr);
if(opr==1) {
int x,d;
read(x),read(d);
w[x]=d;
} else if(opr==2) {
int e,d;
read(e),read(d);
int x=id[e].x,y=id[e].y,p1=id[e].p1,p2=id[e].p2;
a[x][p1].z=d,a[y][p2].z=d;
} else {
dfs1(1,0);
dfs2(1,0);
ll ans=0;
for(int i=1;i<=n;i++) ans^=f[i];
printf("%lld\n",ans);
}
}
return 0;
}
给定一个 nn 个点的树,请求出一个结点,使得以这个结点为根时,所有结点的深度之和最大。
一个结点的深度之定义为该节点到根的简单路径上边的数量。
#include<bits/stdc++.h>
using namespace std;
#define read(x) scanf("%d",&x)
#define maxn int(1e6)
#define ll long long
int n;
vector<int> a[maxn+5];
ll sz[maxn+5],d[maxn+5],f[maxn+5]; //子树的大小,对于节点1的深度,答案
void dfs1(int x,int fa) {
sz[x]=1;
for(int i=0;i<a[x].size();i++) {
int y=a[x][i];
if(y==fa) continue;
d[y]=d[x]+1;
dfs1(y,x);
sz[x]+=sz[y];
}
}
void dfs2(int x,int fa) { //当前x为新根
for(int i=0;i<a[x].size();i++) {
int y=a[x][i];
if(y==fa) continue;
f[y]=f[x]+(n-sz[y])-sz[y];
dfs2(y,x);
}
}
int main() {
read(n);
for(int i=1;i<n;i++) {
int x,y;
read(x),read(y);
a[x].push_back(y);
a[y].push_back(x);
}
d[1]=1;
dfs1(1,0);
for(int i=1;i<=n;i++) f[1]+=d[i];
dfs2(1,0);
int ans=1;
ll m=-1;
for(int i=1;i<=n;i++) {
if(f[i]>m) {
m=f[i],ans=i;
}
}
printf("%d",ans);
return 0;
}