nefu 1330 树上计算 (dfs序+树状数组)

2024-04-09 13:08

本文主要是介绍nefu 1330 树上计算 (dfs序+树状数组),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

树上计算

Problem:1330
Time Limit:1000ms
Memory Limit:65535K

Description

给出一棵以 1 为根的树,初始每个顶点的权值为 0 。
现有两种操作: 1 x 代表将顶点 x 的权值加一 2 x 询问顶点 x 的子树(包含x本身)的权值和是多少

Input

第一行样例个数 T (T<=10)
对于每组样例
第一行是一个数 N 代表顶点的个数(1 <= N <= 4e4)
随后 N - 1 行每行有两个数 x y 代表顶点 x y 由一条边相连·
接下来一行是一个数 M 代表操作的次数(1<= M <=4e4)
最后 M 行,每行两个数 1 x 或 2 x 代表操作(1 <= x <= N)

Output

每次询问输出答案,每个答案占一行。

Sample Input

10
9
1 2
1 3
2 4
2 5
3 6
6 8
6 7
8 9
9
2 1
1 3
2 6
1 6
1 3
2 1
2 8
1 2
2 1
9
1 2
1 3
2 4
2 5
3 6
3 7
6 8
6 9
5
1 6
2 8
1 3
1 4
2 1

Sample Output

0
0
3
0
4
0
3

Hint

Source

DT2131
题意:

中文题。

思路:

先用dfs求出他们的dfs序,ls到rs是他们的子树包括他们自己本身。这样我们就掌握了每个节点他们的子树是什么了。我们只需要一个能单点更新和区间查询的数据结构就行了。树状数组比较好写,我们就用树状数组来完成这件事。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn=4e4+500;
int ls[maxn],rs[maxn];
vector<int> v[maxn];
int cnt;
int tr[maxn];
int lowbit(int i)
{return i&(-i);
}
void update(int i,int n,int c)
{while(i<=n){tr[i]+=c;i+=lowbit(i);}
}
int query(int n)
{int sum=0;while(n>0){sum+=tr[n];n-=lowbit(n);}return sum;
}
void dfs(int now,int fa)
{ls[now]=++cnt;for(int i=0;i<v[now].size();i++){if(v[now][i]==fa)continue;dfs(v[now][i],now);}rs[now]=cnt;
}
int main()
{int t,n,x,y,m,o;scanf("%d",&t);while(t--){memset(tr,0,sizeof(tr));scanf("%d",&n);cnt=0;for(int i=1;i<=n;i++)v[i].clear();for(int i=1;i<n;i++){scanf("%d%d",&x,&y);v[x].push_back(y);v[y].push_back(x);}dfs(1,0);scanf("%d",&m);for(int i=1;i<=m;i++){scanf("%d%d",&o,&x);if(o==1){update(ls[x],n,1);}else{printf("%d\n",query(rs[x])-query(ls[x]-1));}}}return 0;
}


这篇关于nefu 1330 树上计算 (dfs序+树状数组)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/888237

相关文章

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

hdu 2489 (dfs枚举 + prim)

题意: 对于一棵顶点和边都有权值的树,使用下面的等式来计算Ratio 给定一个n 个顶点的完全图及它所有顶点和边的权值,找到一个该图含有m 个顶点的子图,并且让这个子图的Ratio 值在所有m 个顶点的树中最小。 解析: 因为数据量不大,先用dfs枚举搭配出m个子节点,算出点和,然后套个prim算出边和,每次比较大小即可。 dfs没有写好,A的老泪纵横。 错在把index在d

poj 1113 凸包+简单几何计算

题意: 给N个平面上的点,现在要在离点外L米处建城墙,使得城墙把所有点都包含进去且城墙的长度最短。 解析: 韬哥出的某次训练赛上A出的第一道计算几何,算是大水题吧。 用convexhull算法把凸包求出来,然后加加减减就A了。 计算见下图: 好久没玩画图了啊好开心。 代码: #include <iostream>#include <cstdio>#inclu

uva 1342 欧拉定理(计算几何模板)

题意: 给几个点,把这几个点用直线连起来,求这些直线把平面分成了几个。 解析: 欧拉定理: 顶点数 + 面数 - 边数= 2。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#inc

uva 11178 计算集合模板题

题意: 求三角形行三个角三等分点射线交出的内三角形坐标。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <

XTU 1237 计算几何

题面: Magic Triangle Problem Description: Huangriq is a respectful acmer in ACM team of XTU because he brought the best place in regional contest in history of XTU. Huangriq works in a big compa

poj 3050 dfs + set的妙用

题意: 给一个5x5的矩阵,求由多少个由连续6个元素组成的不一样的字符的个数。 解析: dfs + set去重搞定。 代码: #include <iostream>#include <cstdio>#include <set>#include <cstdlib>#include <algorithm>#include <cstring>#include <cm

ural 1149. Sinus Dances dfs

1149. Sinus Dances Time limit: 1.0 second Memory limit: 64 MB Let  An = sin(1–sin(2+sin(3–sin(4+…sin( n))…) Let  Sn = (…( A 1+ n) A 2+ n–1) A 3+…+2) An+1 For given  N print  SN Input One

hdu 6198 dfs枚举找规律+矩阵乘法

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description We define a sequence  F : ⋅   F0=0,F1=1 ; ⋅   Fn=Fn