HDU 5405 Sometimes Naive 树链剖分+bit*****
Sometimes Naive
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
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)
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
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*****的更多相关文章
- 2015多校第9场 HDU 5405 Sometimes Naive 树链剖分
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5405 题意: 给你一棵n个节点的树,有点权. 要求支持两种操作: 操作1:更改某个节点的 ...
- HDU 5029 Relief grain --树链剖分第一题
题意:给一棵树,每次给两个节点间的所有节点发放第k种东西,问最后每个节点拿到的最多的东西是哪种. 解法:解决树的路径上的修改查询问题一般用到的是树链剖分+线段树,以前不会写,后来学了一下树链剖分,感觉 ...
- hdu 5029 Relief grain(树链剖分+线段树)
题目链接:hdu 5029 Relief grain 题目大意:给定一棵树,然后每次操作在uv路径上为每一个节点加入一个数w,最后输出每一个节点个数最多的那个数. 解题思路:由于是在树的路径上做操作, ...
- HDU 5029 Relief grain 树链剖分打标记 线段树区间最大值
Relief grain Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- HDU 4366 Successor(树链剖分+zkw线段树+扫描线)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4366 [题目大意] 有一个公司,每个员工都有一个上司,所有的人呈树状关系,现在给出每个人的忠诚值和 ...
- HDU 5044 Tree(树链剖分)
HDU 5044 Tree field=problem&key=2014+ACM%2FICPC+Asia+Regional+Shanghai+Online&source=1&s ...
- hdu 5242——Game——————【树链剖分思想】
Game Time Limit:1500MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- hdu 5242 Game(树链剖分,贪心¥)
Game Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 3237 Tree(树链剖分)(线段树区间取反,最大值)
Tree Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 9123 Accepted: 2411 Description ...
随机推荐
- Objective-C-------(1)创建并使用对象
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...
- C#中对泛型List进行分组输出元素
背景:在输出列表时,往往需要按照某一字段进行分组,比如在输出城市列表时,按照首字母进行分组,输出学生列表时,按照年级进行分组,然后再对分组的结果按照其他的字段进行排序. 如存在以下STU学生类,代码如 ...
- SpringMVC最核心
如图所示:
- IDEA将Maven项目中src源代码下的xml配置文件编译进classes
遇到这样的情况,maven项目启动报错,src中某个包下面的xml文件找不到. eclipse编译项目会自动将xml配置文件编译进classes,IDEA却不行 在报错项目的pom.xml文件中添加: ...
- You Are the One (区间DP)
The TV shows such as You Are the One has been very popular. In order to meet the need of boys who ar ...
- 66. No EntityManager with actual transaction available for current thread【从零开始学】
[从零开始学习Spirng Boot-常见异常汇总] 具体异常信息: org.springframework.dao.InvalidDataAccessApiUsageException: No En ...
- shell脚本简单密码加密
#!/bin/sh #输入密码 echo "请输入原密码:" read resultFirst firstPWD=$resultFirst echo "请再次输入原密码: ...
- [POJ1797] Heavy Transportation(最大生成树 || 最短路变形)
传送门 1.最大生成树 可以求出最大生成树,其中权值最小的边即为答案. 2.最短路 只需改变spfa里面的松弛操作就可以求出答案. ——代码 #include <queue> #inclu ...
- bzoj 2326 矩阵乘法
[HNOI2011]数学作业 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2415 Solved: 1413[Submit][Status][Di ...
- [NOIP1998] 提高组 洛谷P1012 拼数
题目描述 设有n个正整数(n≤20),将它们联接成一排,组成一个最大的多位整数. 例如:n=3时,3个整数13,312,343联接成的最大整数为:34331213 又如:n=4时,4个整数7,13,4 ...