gym 100589A queries on the Tree 树状数组 + 分块
题目大意:
给定一颗根节点为1的树,有两种操作,第一种操作是将与根节点距离为L的节点权值全部加上val,第二个操作是查询以x为根节点的子树的权重。
思路:
思考后发现,以dfs序建立树状数组,方便查询,不方便修改,以bfs序建立树状数组,方便修改,不方便查询。
在计算子树权重的时候发现,如果我能算出 所有层 属于这棵子树的 点数*对应层需要加上的val,那么就得到了这棵树的总权重。但是显然暴力统计点数会超时,于是我们把用一个分块的想法,对于一层来说,如果这层的总点数小于块的大小,就暴力树状数组修改,如果大于快,就用一个数组记录一下修改的值,并且把这一层保存到一个large的vector里面,表示这些层我是没有计算到树状数组里的。
在查询的时候,先用dfs序弄出每一个节点的(l,r)区间,统计树状数组里的值,然后对large里面的层来说,由于我实现记录好了每个层节点的dfs序号,并且是按照从小打到的顺序的,对于每个large层来说,只要是大于等于序号 l ,小于等于序号 r 的节点都属于这个点,所以用upper_bound和lower_bound来计算就得到了点数,乘以每一层对应的val就可以了。
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<map>
#include<queue>
#include<vector>
#define CLR(a,b) memset(a,b,sizeof(a))
#define PI acos(-1)
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=;
struct edge{
int to,Next;
}e[maxn];
int n,m;
int tot,head[maxn],u,v,L[maxn],R[maxn],time,dfn[maxn];
ll val[maxn];
int limit;
ll c[maxn];
inline int lowbit(int x){
return x&(-x);
}
inline void add(int x,ll val){
while(x<=n){
c[x]+=val;
x+=lowbit(x);
}
}
inline ll getsum(int x){
ll ans=;
while(x>)
{
ans+=c[x];
x-=lowbit(x);
}
return ans;
}
vector<int >large;//大的层
vector<int >pos[maxn];//每一层有哪些点
inline void init(){
CLR(head,-),tot=,time=;
large.clear();
CLR(c,),CLR(val,);
for(int i=;i<=n;i++){
pos[i].clear();
}
limit=; }
inline void addv(int u,int v){
e[++tot]={v,head[u]};
head[u]=tot;
} inline void dfs(int u,int deep){
dfn[u]=++time;
pos[deep].push_back(time);
L[u]=time;
for(int i=head[u];i!=-;i=e[i].Next)
{
int v=e[i].to;
dfs(v,deep+);
}
R[u]=time;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i=;i<n;i++)
{
scanf("%d%d",&u,&v);
addv(u,v);
}
dfs(,);
for(int i=;i<=n;i++)
{
if(pos[i].size()>limit)
{
large.push_back(i);
}
}
int dis,x,op;
ll y;
while(m--)
{
scanf("%d",&op);
if(op==)
{
scanf("%d%lld",&dis,&y);
if(pos[dis].size()<=limit)
{
for(int i=;i<pos[dis].size();i++)
{
add(pos[dis][i],y);
}
}
else{
val[dis]+=y;
}
}else{
scanf("%d",&x);
// printf("l:%d r:%d\n",L[x],R[x]);
ll ans=getsum(R[x])-getsum(L[x]-);
for(int i=;i<large.size();i++)
{
ans+=val[large[i]]*(upper_bound(pos[large[i]].begin(),pos[large[i]].end(),R[x])-lower_bound(pos[large[i]].begin(),pos[large[i]].end(),L[x]));
}
printf("%lld\n",ans);
}
} }
} /*
1 6
1 0 1
2 1
1 0 3
2 1
1 0 1
1 0 1
*/
2.0 s
512 MB
standard input
standard output
You are given a directed tree with N with nodes numbered 1 to N and rooted at node 1. Each node initially contains 0 coins.
You have to handle a total of M operations:
- 1 L Y : Increase by Y the coins of all nodes which are at a distance L from root.
- 2 X : Report the sum of coins of all nodes in subtree rooted at node X.
First line contains N and M. Each of the next N - 1 lines contains u and v denoting directed edge from node numbered u to v.
Each of the next M lines contain queries of either Type 1 or 2.
For each query of Type 2, print the required sum.
Constraints
- 1 ≤ N ≤ 105
- 1 ≤ M ≤ 104
- 0 ≤ L ≤ Maximum height of tree
- 0 ≤ Y ≤ 109
- 1 ≤ X, u, v ≤ N
5 4
1 2
1 3
3 4
3 5
1 1 2
1 2 3
2 3
2 1
8
10
In first update nodes 2 and 3 are increased by 2 coins each.
In second update nodes 4 and 5 are increased by 3 each.
gym 100589A queries on the Tree 树状数组 + 分块的更多相关文章
- BZOJ_2141_排队_树状数组+分块
BZOJ2141_排队_树状数组+分块 Description 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家 乐和和.红星幼儿园的小朋友们排起了 ...
- 【bzoj4889】[Tjoi2017]不勤劳的图书管理员 树状数组+分块+二分
题目描述(转自洛谷) 加里敦大学有个帝国图书馆,小豆是图书馆阅览室的一个书籍管理员.他的任务是把书排成有序的,所以无序的书让他产生厌烦,两本乱序的书会让小豆产生这两本书页数的和的厌烦度.现在有n本被打 ...
- HDU3333 Turing Tree 树状数组+离线处理
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- POJ 3321 Apple Tree(树状数组)
Apple Tree Time Limit: 2000MS Memory Lim ...
- HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)
题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...
- POJ--3321 Apple Tree(树状数组+dfs(序列))
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...
- POJ 3321:Apple Tree 树状数组
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22131 Accepted: 6715 Descr ...
- E - Apple Tree(树状数组+DFS序)
There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...
- POJ3321 Apple Tree(树状数组)
先做一次dfs求得每个节点为根的子树在树状数组中编号的起始值和结束值,再树状数组做区间查询 与单点更新. #include<cstdio> #include<iostream> ...
随机推荐
- ROS 不能安装 Ros Packages
我的linux版本是16.04,安装的是kinetic 1. E: Some index files failed to download. They have been ignored, or ol ...
- 多重if else和switch case的区别
int main(void) { int id; scanf_s("%d",&id); switch(id) { case 2: printf("John\n&q ...
- aspx页面上输出xml的问题
在aspx页面上输出xml,需要在后台输出编码格式和编码类型,如下代码: Response.Charset = "utf-8";//格式Response.ContentType = ...
- jqentitydetail
using System;using System.Collections;using System.Collections.Generic;using System.Linq;using Syste ...
- oracle获取表和列的备注
using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Runti ...
- 事物处理service层的方法
package cn.lijun.service; import java.sql.Connection;import java.sql.SQLException; import cn.lijun.d ...
- wpf附加属性理解
WPF附加属性 http://www.cnblogs.com/tianyou/archive/2012/12/27/2835670.html WPF属性(二)附加属性 http://blog.csdn ...
- POJ3259 Wormholes(SPFA判断负环)
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- JVM调用类的main方法的过程
jvm先装载类,再链接类,再初始化类(以代码的文本顺序执行类变量初始化器.类静态初始化方法或接口的属性(field)初始化器),完成类的初始化后,才执行类的main方法.在链接过程中,可以静态链接(解 ...
- vba实现excel多表合并
Excel多表合并之vba实现 需求 保留列名,复制每一个excel里的数据,合并到一个excel 操作步骤 将要合并的文件放在同一文件夹下,复制过来就好(ps:最好不要直接操作原数据文件,避免操作失 ...