Sometimes Naive

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=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

 
Input
There are multiple test cases.

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

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

Next n−1 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)(0≤w≤109)

 
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
 

题意:

    给你一棵n个节点的树,有点权。

  要求支持两种操作:

    操作1:更改某个节点的权值。

    操作2:给定u,v, 求 Σw[i][j]   i , j 为任意两点且i到j的路径与u到v的路径相交。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 1e6+, M = 1e3+, mod = 1e9+, inf = 2e9; LL aa[N],bb[N],in[N],out[N];
int dep[N],head[N],t=,sz[N],fa[N],indexS,top[N],pos[N],son[N];
struct ss{int to,next;}e[N*];
int n;
void add(int u,int v)
{e[t].to = v;e[t].next = head[u];head[u] = t++;} void dfs(int u) {
int k = ;
sz[u] = ;
if(u!=)dep[u] = dep[fa[u]] + ;
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if(to == fa[u]) continue;
fa[to] = u;
dfs(to);
sz[u] += sz[to];
if(sz[to] > sz[k]) k = to;
}
if(k) son[u] = k;
}
void dfs(int u,int chain) {
int k = ;
pos[u] = ++indexS;
in[u] = indexS;
top[u] = chain;
if(son[u] > )
dfs(son[u],chain);
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if(dep[to] > dep[u] && son[u] != to)
dfs(to,to);
}
out[u] = indexS;
} void update(int x,LL val,LL *sum) {
for(int i = x; i < N; i += i&(-i)) {
sum[i] += val;
sum[i] %= mod;
}
}
LL ask(int x,LL *sum) {
LL res = ;
for(int i = x; i; i-=i&(-i)) res += sum[i],res %= mod;
return res;
} LL allsum,C_tree[N],C_sqtree[N],a[N];
void update(int x,LL val) {
int u = top[x];
while(fa[u] > ) {
LL all = ask(out[u],C_tree) - ask(in[u]-,C_tree);
LL newall = (val - a[x])*(val - a[x])%mod + 1LL**all*(val-a[x])%mod;
update(in[fa[u]],newall%mod,C_sqtree);
u = top[fa[u]];
}
update(in[x],val-a[x],C_tree);
a[x] = val;
}
LL query(int x,int y) {
LL res = ,hav = ;
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x,y);
res += ask(in[x],C_sqtree) - ask(in[top[x]]-,C_sqtree);
res %= mod;
if(son[x]>) {
LL all = ask(out[son[x]],C_tree) - ask(in[son[x]]-,C_tree);
res += all*all;
res %= mod;
}
if(hav>) {
LL all = ask(out[hav],C_tree) - ask(in[hav]-,C_tree);
res -= all * all;
res %= mod;
}
hav = top[x];
x = fa[hav];
}
if(dep[x] < dep[y]) swap(x,y);
res += ask(in[x],C_sqtree) - ask(in[y]-,C_sqtree);
res %= mod;
if(fa[y] > ) {
LL all = ((ask(out[],C_tree) - ask(out[y],C_tree) + ask(in[y]-,C_tree))%mod+mod)%mod;
res += (all*all)%mod;
res %= mod;
}
if(son[x]>) {
LL all = ask(out[son[x]],C_tree) - ask(in[son[x]]-,C_tree);
res += all*all;
res %= mod;
}
if(hav>) {
LL all = ask(out[hav],C_tree) - ask(in[hav]-,C_tree);
res -= all * all;
res %= mod;
}
return res;
}
int Q;
void init() {
memset(head,,sizeof(head));
t = ;
indexS = ;
allsum = ;
memset(son,-,sizeof(son));
son[] = ;
memset(C_tree,,sizeof(C_tree));
memset(C_sqtree,,sizeof(C_sqtree));
}
int main() {
while(scanf("%d%d",&n,&Q)!=EOF) {
init();
for(int i = ; i <= n; ++i)
scanf("%I64d",&a[i]);
allsum = allsum*allsum%mod;
for(int i = ; i < n; ++i) {
int x,y;
scanf("%d%d",&x,&y);
add(x,y),add(y,x);
}
fa[] = -;
dfs();
dfs(,);
for(int i = ; i <= n; ++i) {
LL uu = a[i];
a[i] = ;
update(i,uu);
}
while(Q--) {
int op,x;
LL y;
LL all = ask(out[],C_tree);
scanf("%d%d%I64d",&op,&x,&y);
if(op == ) {
update(x,y);
}
else
printf("%I64d\n",((all*all%mod - query(x,y)) % mod + mod)%mod);
}
}
return ;
}

HDU 5405 Sometimes Naive 树链剖分+bit*****的更多相关文章

  1. 2015多校第9场 HDU 5405 Sometimes Naive 树链剖分

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5405 题意: 给你一棵n个节点的树,有点权.        要求支持两种操作: 操作1:更改某个节点的 ...

  2. HDU 5029 Relief grain --树链剖分第一题

    题意:给一棵树,每次给两个节点间的所有节点发放第k种东西,问最后每个节点拿到的最多的东西是哪种. 解法:解决树的路径上的修改查询问题一般用到的是树链剖分+线段树,以前不会写,后来学了一下树链剖分,感觉 ...

  3. hdu 5029 Relief grain(树链剖分+线段树)

    题目链接:hdu 5029 Relief grain 题目大意:给定一棵树,然后每次操作在uv路径上为每一个节点加入一个数w,最后输出每一个节点个数最多的那个数. 解题思路:由于是在树的路径上做操作, ...

  4. HDU 5029 Relief grain 树链剖分打标记 线段树区间最大值

    Relief grain Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  5. HDU 4366 Successor(树链剖分+zkw线段树+扫描线)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4366 [题目大意] 有一个公司,每个员工都有一个上司,所有的人呈树状关系,现在给出每个人的忠诚值和 ...

  6. HDU 5044 Tree(树链剖分)

    HDU 5044 Tree field=problem&key=2014+ACM%2FICPC+Asia+Regional+Shanghai+Online&source=1&s ...

  7. hdu 5242——Game——————【树链剖分思想】

    Game Time Limit:1500MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status  ...

  8. hdu 5242 Game(树链剖分,贪心¥)

    Game Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  9. HDU 3237 Tree(树链剖分)(线段树区间取反,最大值)

    Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 9123   Accepted: 2411 Description ...

随机推荐

  1. python 03 8/25-8/27 range 、randint

    import random """字符串的操作中 三种方法,只包含左索引,不包含右索引""" hi= "bokeyuan pyth ...

  2. Canal使用报错解决办法

    1. [destination = test_cancal , address = /127.0.0.1:3306 , EventParser] WARN c.a.o.s.a.i.setl.zooke ...

  3. Ubuntu16.04进入无限登录状态的解决办法

    具体来说就是,输入密码之后又到了登录界面,无限循环(也许可能不能输入密码,这种状态我没有测试) 此方案仅适用于安装过NVIDIA显卡驱动的系统并且在登录界面会发现分辨率变了 如果你没有安装过NVIDI ...

  4. 实验:iscsi共享存储

    实验名称: iscsi共享存储 实验环境: 我们需要准备一个磁盘,对于这个磁盘我们需要使用,将这个磁盘空间共享给iscsi客户端: 实验需求: 我们这里使用两台服务器来实现iscsi共享存储: 1.指 ...

  5. 安装SecureCRT注册

    注册方法 1.首先运行压缩包中的安装程序进行安装原版程序!2.安装完成后记得先不要运行程序!3.复制压缩包中的keygen.exe程序到安装目录!4.运行keygen.exe,点击Patch按钮对源程 ...

  6. python3 时间复杂度

    时间复杂度 (1)时间频度 一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才能知道.但我们不可能也没有必要对每个算法都上机测试,只需知道哪个算法花费的时间多,哪个算法花费的时间少就 ...

  7. Bone Collector II(01背包kth)

    The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...

  8. 洛谷P2414 - [NOI2011]阿狸的打字机

    Portal Description 首先给出一个只包含小写字母和'B'.'P'的操作序列\(s_0(|s_0|\leq10^5)\).初始时我们有一个空串\(t\),依次按\(s_0\)的每一位进行 ...

  9. msp430项目编程15

    msp430中项目---简易红外遥控系统 1.红外工作原理 2.电路原理说明 3.代码(显示部分) 4.代码(功能实现) 5.项目总结 msp430项目编程 msp430入门学习

  10. angularjs ngRoute的使用简单例子

    很丑的小例子,刚学angularjs,写下来方面以后看. 1.例子的工程目录如下: 2.index.html代码如下: <!DOCTYPE html><html><hea ...