一维树状数组

Apple Tree http://poj.org/problem?id=3321

 1 #include<cstdio>
 2 #include<cstring>
 3 #define mt(a,b) memset(a,b,sizeof(a))
 4 const int M=100010;
 5 struct G{
 6     struct E{
 7         int u,v,next;
 8     }e[M<<1];
 9     int le,head[M];
10     void init(){
11         le=0;
12         mt(head,-1);
13     }
14     void add(int u,int v){
15         e[le].u=u;
16         e[le].v=v;
17         e[le].next=head[u];
18         head[u]=le++;
19     }
20 }g;
21 struct P{
22     int l,r;
23 }p[M];
24 int Clock;
25 void dfs(int u,int fa){
26     p[u].l=++Clock;
27     for(int i=g.head[u];~i;i=g.e[i].next){
28         int v=g.e[i].v;
29         if(v!=fa){
30             dfs(v,u);
31         }
32     }
33     p[u].r=Clock;
34 }
35 class One_Tree_Array { //一维树状数组
36     typedef int typev;
37     typev a[M];
38 public:
39     void init() {
40         mt(a,0);
41     }
42     int lowb(int t) {
43         return t&(-t);
44     }
45     void add(int i,typev v) {
46         for(; i<M; a[i]+=v,i+=lowb(i));
47     }
48     typev sum(int i) {
49         typev s=0;
50         for(; i>0; s+=a[i],i-=lowb(i));
51         return s;
52     }
53 }gx;
54 int now[M];
55 int main(){
56     int n,m,u,v;
57     while(~scanf("%d",&n)){
58         g.init();
59         for(int i=0;i<n-1;i++){
60             scanf("%d%d",&u,&v);
61             g.add(u,v);
62             g.add(v,u);
63         }
64         Clock=0;
65         dfs(1,-1);
66         scanf("%d",&m);
67         gx.init();
68         for(int i=1;i<=n;i++){
69             now[i]=1;
70             gx.add(i,1);
71         }
72         while(m--){
73             char op[4];
74             scanf("%s%d",op,&u);
75             if(op[0]==‘C‘){
76                 gx.add(p[u].l,-now[u]);
77                 now[u]=-now[u];
78             }
79             else{
80                 printf("%d\n",gx.sum(p[u].r)-gx.sum(p[u].l-1));
81             }
82         }
83     }
84     return 0;
85 }

end

一维树状数组

时间: 2024-08-30 11:09:53

一维树状数组的相关文章

一维树状数组(HD1166)

#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<string.h> using namespace std; #define BITMAX 50001 //数组大小 typedef int valueType; //元素类型定义 valueType BITree[BITMAX]; //一维树状数组,初始化 /* 2^k k表示节点编号 x 二进制末尾 0 的个数 */ inline int lowbit(int

HDU - 1556 Color the ball (一维树状数组 + 区间修改 + 单点求值)

HDU - 1556 Color the ball Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色.但是N次以后lele已经忘记了第I个气

一维树状数组入门

个人觉得非常棒的博客:https://www.cnblogs.com/xenny/p/9739600.html 第一类:单点更新,区间查询 例题:http://acm.hdu.edu.cn/showproblem.php?pid=1166 AC代码: 1 /* */ 2 # include <iostream> 3 # include <cstdio> 4 # include <cstring> 5 # include <string> 6 # includ

一维 + 二维树状数组 + 单点更新 + 区间更新 详解

树状数组详解: 假设一维数组为A[i](i=1,2,...n),则与它对应的树状数组C[i](i=1,2,...n)是这样定义的: C1 = A1 C2 = A1 + A2 C3 = A3 C4 = A1 + A2 + A3 + A4 C5 = A5 C6 = A5 + A6 ................. C8 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8 ................ 如图可知: 为奇数的时候他是代表他本身,而为偶数的时候则是代表着自

树状数组(二叉索引树 BIT Fenwick树) *【一维基础模板】(查询区间和+修改更新)

刘汝佳:<训练指南>Page(194) #include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; //一维树状数组基础模板 int lowbit(int x) { return x&(-x); } int c[1001]; int sum(int x) //计算从1到x的数组元素的和 { int

POJ 1195 Mobile phones(二维树状数组)

题目链接:POJ 1195 题意: 给出一个S*S的矩阵(行.列号从1开始),每个元素初始值为0,有两种操作:一种是第X行第Y列元素值加A:另一种是查询给定范围矩阵的所有元素之和(L<=X<=R,B<=Y<=T). 分析: 查询给定范围矩阵的所有元素之和是二维区间和,可以转换为二维前缀和求值.类比一维前缀和求法,二维区间和S(L, B, R, T) = S(1, 1, R, T) - S(1 ,1, L-1, T) - S(1, 1, R, B-1) + S(1, 1, L-1,

深入理解树状数组

树状数组(Binary Indexed Tree(BIT), Fenwick Tree)是一个查询和修改复杂度都为log(n)的数据结构.主要用于查询任意两位之间的所有元素之和,但是每次只能修改一个元素的值:经过简单修改可以在log(n)的复杂度下进行范围修改,但是这时只能查询其中一个元素的值(如果加入多个辅助数组则可以实现区间修改与区间查询). 百度上给出了令人难以理解的概念,其实这个东西我也是琢磨了一天,参考了大量博客的笔记才搞清楚了大致思路和原理,说说心得吧! 假设数组a[1..n],那么

hdu 5480|| bestcoder   #57 div 2 Conturbatio(前缀和||树状数组)

Conturbatio Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 211    Accepted Submission(s): 99 Problem Description There are many rook on a chessboard, a rook can attack the row and column it bel

树状数组入门

用office做了一张pdf - - 这是一维的情形,如果是二维,可以把每一行的一维树状数组看成一个节点,然后再把二维树状数组看成一维树状数组. 好文章:https://www.topcoder.com/community/data-science/data-science-tutorials/binary-indexed-trees/#prob 两道入门题:http://acm.hdu.edu.cn/showproblem.php?pid=1556 http://poj.org/problem