Hdu 5405 Sometimes Naive 树链剖分+线段树
来源:互联网 发布:vb控件加到窗体上 编辑:程序博客网 时间:2024/05/22 07:51
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 withn 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 operationu,w , you need to change the weight of u -th node into w .
For a query operationu,v , you should output ∑ni=1∑nj=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
She has a tree with
You need to support two kinds of operations: modification and query.
For a modification operation
For a query operation
Input
There are multiple test cases.
For each test case, the first line contains two numbersn,m(1≤n,m≤105) .
There aren numbers in the next line, the i -th means wi(0≤wi≤109) .
Nextn−1 lines contain two numbers each, ui and vi , that means that there is an edge between ui and vi .
The following arem lines. Each line indicates an operation, and the format is "1 u w "(modification) or "2 u v "(query)(0≤w≤109)
For each test case, the first line contains two numbers
There are
Next
The following are
Output
For each test case, print the answer for each query operation.
Sample Input
6 51 2 3 4 5 61 21 32 42 54 62 3 51 5 62 2 31 1 72 2 4
Sample Output
341348612
Author
xudyh
Source
2015 Multi-University Training Contest 9
一棵树,有点权,每次询问给出一条从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
- Hdu 5405 Sometimes Naive 树链剖分+线段树
- HDU 5405 Sometimes Naive 2015多校联合训练赛#9 LCT 树链剖分
- hdu5405 Sometimes Naive
- HDU5405 Sometimes Naive
- hdu 3966 (树链剖分+线段树)
- hdu 3804(树链剖分+线段树)
- hdu 3804 树链剖分+线段树
- hdu 3966 树链剖分+线段树
- hdu 5029树链剖分+线段树
- CIA3 Sometimes Naive(莫比乌斯反演+积性函数前缀和)
- HDU-3966 (树链剖分+线段树)
- hdu 5029 Relief grain (树链剖分+线段树)
- hdu 5029 Relief grain(树链剖分+线段树)
- hdu-3966(树链剖分+线段树)
- HDU-5242 Game (贪心&&树链剖分&&线段树)
- 【HDU 5221】 Occupation 【树链剖分+线段树】
- HDU 5029Relief grain(树链剖分+线段树)
- hdu 1754 线段树
- 【Spring in action】SpringMVC基于xml及java配置的简单运用
- 【安卓学习之常见问题】 apk崩溃,找不到so文件(dex file "couldn't find "libSDL.so)
- API Hook
- Java数据结构与算法之树_动力节点Java学院整理
- 常用的可视化数据展示工具有哪些
- Hdu 5405 Sometimes Naive 树链剖分+线段树
- 动态规划——最长公共子序列问题(LCS)
- Windows 7系统想要查看事件日志时候要如何查看
- Mac上搭建Xcode9.0+appium1.6.5过程及链接模拟器测试app
- jQuery 遍历
- curl 模拟表单发送
- 理解OAuth 2.0
- NLP概率图模型之隐马可夫模型需要的基础知识
- inline 函数相关说明