Description

"奋战三星期,造台计算机"。小G响应号召,花了三小时造了台普通计算姬。普通计算姬比普通计算机要厉害一些
。普通计算机能计算数列区间和,而普通计算姬能计算树中子树和。更具体地,小G的计算姬可以解决这么个问题
:给定一棵n个节点的带权树,节点编号为1到n,以root为根,设sum[p]表示以点p为根的这棵子树中所有节点的权
值和。计算姬支持下列两种操作:
1 给定两个整数u,v,修改点u的权值为v。
2 给定两个整数l,r,计算sum[l]+sum[l+1]+....+sum[r-1]+sum[r]
尽管计算姬可以很快完成这个问题,可是小G并不知道它的答案是否正确,你能帮助他吗?

Input

第一行两个整数n,m,表示树的节点数与操作次数。
接下来一行n个整数,第i个整数di表示点i的初始权值。
接下来n行每行两个整数ai,bi,表示一条树上的边,若ai=0则说明bi是根。
接下来m行每行三个整数,第一个整数op表示操作类型。
若op=1则接下来两个整数u,v表示将点u的权值修改为v。
若op=2则接下来两个整数l,r表示询问。
N<=10^5,M<=10^5
0<=Di,V<2^31,1<=L<=R<=N,1<=U<=N

Output

对每个操作类型2输出一行一个整数表示答案。

Sample Input

6 4
0 0 3 4 0 1
0 1
1 2
2 3
2 4
3 5
5 6
2 1 2
1 1 1
2 3 6
2 3 5

Sample Output

16
10
9

HINT

Source

感受到树上分块的邪恶力量!!!  %%%XLightGod;

貌似这题有很多种做法,主要是连续编号的子树和不是很好搞!!!

直接讲树上分块的做法好了,不想绕圈子:

子树和依据我们以前打树链剖分的时候(其实应该叫轻重链剖分,今天听到了一位NOI金牌爷说了个叫长链剖分的鬼玩意);

我们知道一个点的子树其实就是一段连续的dfs序;

首先对[1,n]分块,想到分块查询的基本思想

那么每次询问相当与是若干个整块加上剩下的几个点;

我们一步一步来解决:

首先对于每一个块的可以通过统计每个点在子树中出现的次数,那么我们可以通过O(n)的时间计算出整块贡献;

接下来的瓶颈就在于解决如何快速O(1)求出每个点的子树和

根据子树是dfs序中连续的一段我们可以考虑对dfs序进行分块,然后统计所有块的前缀和以及每个块自己内部的前缀和,通过前缀和的基本操作可以O(1)求解

附上代码:

// MADE BY QT666
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<queue>
#include<set>
#include<cstdlib>
#include<cstring>
#include<string>
#include<ctime>
#define lson num<<1
#define rson num<<1|1
using namespace std;
typedef long long ll;
const int N=100001;
int gi()
{
int x=0,flag=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') flag=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*flag;
}
int head[N],to[N*2],nxt[N*2];
int v[N],pos[N],kp[N],dfn[N],id[N],block,num[320][N],fa[N],end[N];
int n,m,cnt,tt,cnt2,root;
unsigned long long tot1[N],tot2[320],tot3[320];
void dfs(int x,int f){
dfn[x]=++tt;id[tt]=x;
for(int i=head[x];i;i=nxt[i]){
int y=to[i];
if(y!=f){
fa[y]=x;dfs(y,x);
}
}
end[x]=tt;
}
inline void lnk(int x,int y){
to[++cnt]=y,nxt[cnt]=head[x],head[x]=cnt;
to[++cnt]=x,nxt[cnt]=head[y],head[y]=cnt;
}
unsigned long long query(int x){return tot3[pos[x]-1]+tot1[x];}
inline void make_tot1(){
for(int i=1;i<=n;i++) tot1[dfn[i]]=v[i];
for(int i=1;i<=n;i++) {if(kp[i]!=1) tot1[i]+=tot1[i-1];}
}
inline void make_tot2(){
for(int i=1;i<=cnt2;i++)
for(int j=1;j<=n;j++)
num[i][id[j]]=num[i][fa[id[j]]]+(pos[id[j]]==i);
for(int i=1;i<=cnt2;i++)
for(int j=1;j<=n;j++)
tot2[i]+=(unsigned long long)1ll*num[i][j]*v[j];
}
inline void make_tot3(){
for(int i=1;i<=n;i++) tot3[pos[dfn[i]]]+=v[i];
for(int i=1;i<=cnt2;i++) tot3[i]+=tot3[i-1];
}
int main()
{
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
n=gi(),m=gi();int x,y;
for(int i=1;i<=n;i++) v[i]=gi();
for(int i=1;i<=n;i++){
x=gi(),y=gi();
if(x!=0) lnk(x,y);
else root=y;
}
int block=sqrt(n);
if(n%block) cnt2=n/block+1;
else cnt2=n/block;
for(int i=1;i<=n;i++){
pos[i]=(i-1)/block+1;
kp[i]=(i-1)%block+1;
}
dfs(root,0);
make_tot1();make_tot2();make_tot3();int flag;
while(m--){
flag=gi();
if(flag==1){
x=gi(),y=gi()-v[x];
for(int i=dfn[x];i<=n&&pos[i]==pos[dfn[x]];i++) tot1[i]+=y;
for(int i=1;i<=cnt2;i++) tot2[i]+=(unsigned long long)1ll*num[i][x]*y;
for(int i=pos[dfn[x]];i<=cnt2;i++) tot3[i]+=y;
v[x]+=y;
}
else{
int l=gi(),r=gi();unsigned long long ans=0;
if(pos[l]==pos[r]){
for(int i=l;i<=r;i++)
ans+=query(end[i])-query(dfn[i]-1);
}
else{
for(int i=l;pos[i]==pos[l];i++) ans+=query(end[i])-query(dfn[i]-1);
for(int i=r;pos[i]==pos[r];i--) ans+=query(end[i])-query(dfn[i]-1);
for(int i=pos[l]+1;i<pos[r];i++) ans+=tot2[i];
}
printf("%llu\n",ans);
}
}
return 0;
}

bzoj 4765: 普通计算姬的更多相关文章

  1. BZOJ 4765 普通计算姬 (分块 + BIT)

    4765: 普通计算姬 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 1547  Solved: 329[Submit][Status][Discus ...

  2. bzoj 4765 普通计算姬 dfs序 + 分块

    题目链接 Description "奋战三星期,造台计算机".小G响应号召,花了三小时造了台普通计算姬.普通计算姬比普通计算机要厉害一些.普通计算机能计算数列区间和,而普通计算姬能 ...

  3. bzoj 4765: 普通计算姬 主席树+替罪羊树思想

    题目大意: 给定一棵\(n\)个节点的带权树有根树,设\(sum_p\)表示以点\(p\)为根的这棵子树中所有节点的权 计算姬支持下列两种操作: 给定两个整数\(u,v\),修改点\(u\)的权值为\ ...

  4. bzoj 4765 普通计算姬(树状数组 + 分块)

    http://www.lydsy.com/JudgeOnline/problem.php?id=4765 很nice的一道题啊(可能是因为卡了n久终于做出来了 题意就是给你一棵带点权的有根树,sum( ...

  5. BZOJ 4765 普通计算姬 dfs序+分块+树状数组+好题!!!

    真是道好题...感到灵魂的升华... 按dfs序建树状数组,拿前缀和去求解散块: 按点的标号分块,分成一个个区间,记录区间子树和 的 总和... 具体地,需要记录每个点u修改后,对每一个块i的贡献,记 ...

  6. BZOJ 4765: 普通计算姬 [分块 树状数组 DFS序]

    传送门 题意: 一棵树,支持单点修改和询问以$[l,r]$为根的子树的权值和的和 只有我这种不会分块的沙茶不会做这道题吗? 说一点总结: 子树和当然上$dfs$序了,询问原序列一段区间所有子树和,对原 ...

  7. BZOJ 4765: 普通计算姬 (分块+树状数组)

    传送门 解题思路 树上的分块题,,对于修改操作,每次修改只会对他父亲到根这条链上的元素有影响:对于查询操作,每次查询[l,r]内所有元素的子树,所以就考虑dfn序,进标记一次,出标记一次,然后子树就是 ...

  8. bzoj 4766: 文艺计算姬 -- 快速乘

    4766: 文艺计算姬 Time Limit: 1 Sec  Memory Limit: 128 MB Description "奋战三星期,造台计算机".小W响应号召,花了三星期 ...

  9. BZOJ 4766: 文艺计算姬

    4766: 文艺计算姬 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 456  Solved: 239[Submit][Status][Discuss] ...

随机推荐

  1. C#图片压缩上传

    /// <summary> /// 压缩图片 /// </summary> /// <param name="iSource">图片文件< ...

  2. scrapy初试水 day01

    1.安装pip install Scrapy#一定要以管理员身份运行dos窗口conda install scrapy2.创建项目scrapy startproject hello3.在hello/s ...

  3. pyhton 关于 configparser 配置 模块 实践使用中碰到的坑

    今天做一个ATM的练习,想要用configparser模块,写一个配置文件,存放用户信息. 结果状况连连,叫苦不迭. 我用configparser模块,想要对配置文件,进行读.写.改.查 功能. 其中 ...

  4. 驱动调试-根据oops定位错误代码行

    1.当驱动有误时,比如,访问的内存地址是非法的,便会打印一大串的oops出来 1.1以LED驱动为例 将open()函数里的ioremap()屏蔽掉,直接使用物理地址的GPIOF,如下图所示: 1.2 ...

  5. 乘积最大洛谷p1018

    题目描述 今年是国际数学联盟确定的“2000――世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你的一个好朋友XZ也有幸得 ...

  6. ACM个人零散知识点整理

    ACM个人零散知识点整理 杂项: 1.输入输出外挂 //读入优化 int 整数 inline int read(){ int x=0,f=1; char ch=getchar(); while(ch& ...

  7. 有关nginx的配置文件 之server

    下面是vhost中的其中一个xxxx.conf文件 . [Shell] 纯文本查看 复制代码 ? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 ...

  8. 配置scrapy-splash+python爬取医院信息(利用了scrapy-splash)

    北京艾丽斯妇科医院(http://fuke.fuke120.com/) 首先先说一下配置splash 1.利用pip安装scrapy-splash库 pip install scrapy-splash ...

  9. Zepto中的Swipe事件失效

    需要阻止浏览器默认滑动的事件 document.addEventListener('touchmove', function (event) { event.preventDefault(); }, ...

  10. springmvc关于前台日期作为实体类对象参数类型转换错误

    页面报错: 后台错误: Field error in object 'user' on field 'birthday': rejected value [2013-06-24]; codes [ty ...