题意:给出有n个结点的一棵树,并给出参数k,除了0结点外每个节点i与I/k连有一条权值为i的边,现在有q组询问,每组询问查询[I,j]中任意两个节点之间的距离之和。
思路:假设当前查询区间为[L,R],那么每个结点u往上连的那条边对答案的贡献度为d[u]*(R-L+1)*u,其中d[u]表示u有多少子孙结点在[L,R]中,
然后对于这n-1条边的贡献度求和即可。
然后就可以用莫队算法来做了,首先因为每次查询区间的大小是变的,所以用一个变量sum_d_mul_i记录当前sigma(d[u]*I),然后莫队的时候动态维护sum_d_mul_i和d[I]即可。
具体见代码:
#include<bits/stdc++.h>
#define eps 1e-6
#define LL long long
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int MAXN = 10010;
//const int INF = 0x3f3f3f3f;
int n, k, q, unit;
int d[MAXN];
LL ans[MAXN], ret, sum_d_mul_i, len;
struct Query
{
int l, r;
int id;
} query[MAXN];
bool cmp(const Query& A, const Query& B) {
if (A.l/unit != B.l/unit) return A.l/unit < B.l/unit;
return A.r < B.r;
}
void inc(int u) {
while (u) {
ret -= d[u]*(len-d[u])*u;
d[u]++;
ret += d[u]*(len-d[u])*u;
sum_d_mul_i += u;
u /= k;
}
}
void dec(int u) {
while (u) {
ret -= d[u]*(len-d[u])*u;
d[u]--;
ret += d[u]*(len-d[u])*u;
sum_d_mul_i -= u;
u /= k;
}
}
void work() {
ret = sum_d_mul_i = 0;
len = 1;
int L = 0, R = 0;
for (int i = 1; i <= q; i++) {
while (R < query[i].r) {
len++;
ret += sum_d_mul_i;
R++;
inc(R);
}
while (R > query[i].r) {
len--;
ret -= sum_d_mul_i;
dec(R);
R--;
}
while (L < query[i].l) {
len--;
ret -= sum_d_mul_i;
dec(L);
L++;
}
while (L > query[i].l) {
len++;
ret += sum_d_mul_i;
L--;
inc(L);
}
ans[query[i].id] = ret;
}
}
int main()
{
//freopen("input.txt", "r", stdin);
while (scanf("%d%d%d", &n, &k, &q) == 3) {
memset(d, 0, sizeof(d));
for (int i = 1; i <= q; i++) {
scanf("%d%d", &query[i].l, &query[i].r);
query[i].id = i;
}
query[0].id = 0, query[0].l = 0, query[0].r = 0;
unit = (int)sqrt(n);
sort(query, query+q+1, cmp);
work();
for (int i = 1; i <= q; i++)
printf("%I64d\n", ans[i]);
}
return 0;
}