Hdu 5405 Sometimes Naive 树链剖分+线段树

Sometimes Naive

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 332    Accepted Submission(s): 128


Problem Description
Rhason Cheung had a naive problem, and asked Teacher Mai for help. But Teacher Mai thought this problem was too simple, sometimes naive. So she ask you for help.

She has a tree with  n  vertices, numbered from  1  to  n . The weight of  i -th node is  wi .

You need to support two kinds of operations: modification and query.

For a modification operation  u,w , you need to change the weight of  u -th node into  w .

For a query operation  u,v , you should output  ni=1nj=1f(i,j) . If there is a vertex on the path from  u  to  v  and the path from  i  to  j  in the tree,  f(i,j)=wiwj , otherwise  f(i,j)=0 . The number can be large, so print the number modulo  109+7
 

Input
There are multiple test cases.

For each test case, the first line contains two numbers  n,m(1n,m105) .

There are  n  numbers in the next line, the  i -th means  wi(0wi109) .

Next  n1  lines contain two numbers each,  ui  and  vi , that means that there is an edge between  ui  and  vi .

The following are  m  lines. Each line indicates an operation, and the format is " 1 u w "(modification) or " 2 u v "(query) (0w109)
 

Output
For each test case, print the answer for each query operation.
 

Sample Input
  
  
6 5 1 2 3 4 5 6 1 2 1 3 2 4 2 5 4 6 2 3 5 1 5 6 2 2 3 1 1 7 2 2 4
 

Sample Output
  
  
341 348 612
 

Author
xudyh
 

Source



一棵树,有点权,每次询问给出一条从u到v的路径,求树上所有路径当中和这条路径有共同点的路径的权值和。一条路径的权值定义为起点和终点的点权的乘积。


观察,发现答案就是总的权值和的平方,减去原来的树去掉u到v这条链之后,森林中每棵树的权值和的平方。

那么可以先链剖,再用线段树记录两个值:

sum1表示某点及其子树的权值和,sum2表示某点的所有轻链上点的权值和的平方。

更新时,sum1只需要单点更新,sum2需要更新子树当中含有当前修改点的,且当前修改点在轻链上的点。

查询时,需要同时计算某条重链重儿子的贡献(sum1)和所有轻链的子树的贡献(sum2)。

由于此时的这条重链必定是上一条重链的一个轻链分支,所以沿着重链爬上去时,还要减掉这一部分重复的贡献。


这恐怕是我写得最累的一道题了。。。


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn = 100005, inf = 0x3f3f3f3f;
const ll llinf = 0x3f3f3f3f3f3f3f3f, mod = 1e9 + 7;
const ld pi = acos(-1.0L);
int size[maxn], top[maxn], son[maxn], fa[maxn], dep[maxn], dfn[maxn];
int a[maxn], head[maxn];
bool visit[maxn];
int num;

struct Edge {
	int from, to, pre;
};
Edge edge[maxn * 2];

void addedge(int from, int to) {
	edge[num]= (Edge) { from, to, head[from] };
	head[from] = num++;
	edge[num] = (Edge) { to, from, head[to] };
	head[to] = num++;
}

int dfs(int now,int step) {
	visit[now]=1;son[now]=-1;dep[now]=step;size[now]=1;
	for (int i=head[now];i!=-1;i=edge[i].pre) {
		int to=edge[i].to;
		if (!visit[to]) {
			fa[to]=now;
			size[now]+=dfs(to,step+1);
			if (son[now]==-1||size[to]>size[son[now]]) son[now]=to;
		}
	}
	return size[now];
}

void dfs2(int now,int t) {
	visit[now]=1;top[now]=t;dfn[now]=++num;
	if (son[now]!=-1) dfs2(son[now],t);
	for (int i=head[now];i!=-1;i=edge[i].pre) {
		int to=edge[i].to;
		if (!visit[to]) 
			dfs2(to,to);
	}
}

struct Tree {
	int l, r, lc, rc;
	ll sum1, sum2;
};
Tree tree[4 * maxn];

void build(int now, int l, int r) {
	tree[now].l = l; tree[now].r = r;
	tree[now].sum1 = tree[now].sum2 = 0;
	if (l == r)
		return;
	else {
		num++;
		tree[now].lc = num;
		build(num, l, (l + r) / 2);
		num++;
		tree[now].rc = num;
		build(num, (l + r) / 2 + 1, r);
	}
}

void update(int now, int pos, ll val, int t) {
//	cout << tree[now].l << ' ' << tree[now].r << ' ' << tree[now].sum2 << endl;
	if (tree[now].l == tree[now].r && tree[now].r ==pos) {
		if (t) tree[now].sum2 = ((tree[now].sum2 + val) % mod + mod) % mod;
		else tree[now].sum1 = ((tree[now].sum1 + val) % mod + mod) % mod;
	}
	else {
		if (pos <= (tree[now].l + tree[now].r) / 2)
			update(tree[now].lc, pos, val, t);
		if (pos>(tree[now].l + tree[now].r) / 2)
			update(tree[now].rc, pos, val, t);
		if (t) {
			tree[now].sum2 = tree[tree[now].lc].sum2 + tree[tree[now].rc].sum2;
			tree[now].sum2 %= mod;
		}
		else {
			tree[now].sum1 = tree[tree[now].lc].sum1 + tree[tree[now].rc].sum1;
			tree[now].sum1 %= mod;
		}
	}
//	cout << tree[now].l << ' ' << tree[now].r << ' ' << tree[now].sum2 << endl;
}

ll findsum(int now, int l, int r, int t) {
	if (tree[now].l >= l&&tree[now].r <= r) {
		if (t) return tree[now].sum2; else return tree[now].sum1;
	}
	else {
		ll ans = 0;
		if (l <= (tree[now].l + tree[now].r) / 2)
			ans = findsum(tree[now].lc, l, r, t);
		if (r>(tree[now].l + tree[now].r) / 2)
			ans += findsum(tree[now].rc, l, r, t);
		ans %= mod;
		return ans;
	}
}

ll modify(int pos, ll val) {
	int x = top[pos];
	while (fa[x]) {
		ll q=findsum(0,dfn[top[x]],dfn[top[x]]+size[x]-1,0);
		update(0, dfn[fa[x]], ((2*q*val+val*val)%mod+mod)%mod, 1);
		x = top[fa[x]];
	}
	update(0, dfn[pos],(val+mod)%mod, 0);
}

ll findval(int u, int v) {
	int x = top[u], y = top[v];
	ll ans = 0, d;
	while (x != y) {
		if (dep[x] < dep[y]) {
			swap(x, y);
			swap(u, v);
		}
		ans += findsum(0, dfn[x], dfn[u], 1);
		ans %= mod;
		if (son[u]!=-1) {
			d = findsum(0, dfn[son[u]], dfn[son[u]] + size[son[u]] - 1, 0);
			ans += (d*d) % mod;
			ans %= mod;
		}
		d = findsum(0, dfn[x], dfn[x] + size[x]-1, 0);
		ans -= (d*d) % mod;
		ans = (ans + mod) % mod;
		u = fa[x]; x = top[u];
	}
	if (dep[u] < dep[v]) swap(u, v);
	ans += findsum(0, dfn[v], dfn[u],1); ans %= mod;
	if (son[u]!=-1) {
		d = findsum(0, dfn[son[u]], dfn[son[u]] + size[son[u]] - 1, 0);
		ans += (d*d) % mod;
		ans %= mod;
	}
	if (fa[v]) {
		d = tree[0].sum1-findsum(0,dfn[v],dfn[v]+size[v]-1,0);
		ans += (d*d) % mod;
		ans = (ans + mod) % mod;
	}
	return ans;
}

int main() {
	int n, m;
	while (scanf("%d%d", &n, &m) != EOF) {
		int i, j, x, y, t;
		ll ans, sum = 0;
		num = 0; memset(head, -1, sizeof(head));
		for (i = 1; i <= n; i++) {
			scanf("%d", &a[i]);
			sum = (sum + a[i]) % mod;
		}
		for (i = 1; i < n; i++) {
			scanf("%d%d", &x, &y);
			addedge(x, y);
		}
		fa[1] = 0;
		mem0(visit); dfs(1, 0);
		num = 0;
		mem0(visit); dfs2(1, 1);
		num = 0;
		build(0, 1, n);
		for (i = 1; i <= n; i++) {
			modify(i, a[i]);
		}
		for (i = 1; i <= m; i++) {
			scanf("%d%d%d", &t, &x, &y);
			if (t == 1) {
				sum+=y-a[x];sum=(sum+mod)%mod;
				modify(x, y - a[x]);
				a[x] = y;	
			}
			else {
				ans = (sum*sum)%mod-findval(x, y);
				if (ans < 0) ans += mod;
				printf("%lld\n", ans);
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值