poj3321 Apple Tree

来源:互联网 发布:php搭建网页服务器 编辑:程序博客网 时间:2024/05/19 09:15

 There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree.

    The tree has N forks which are connected by branches. Kaka numbers the forks by 1 to N and the root is always numbered by 1. Apples will grow on the forks and two apple won't grow on the same fork. kaka wants to know how many apples are there in a sub-tree, for his study of the produce ability of the apple tree.

    The trouble is that a new apple may grow on an empty fork some time and kaka may pick an apple from the tree for his dessert. Can you help kaka?


题解:

ª将问题转化为了区间操作
wC x 将某一点的权值改变
wQ x 询问区间[st[x], en[x]]的和
ª树状数组可以轻松实现这些操作。
代码

with tree[a] do  
  begin  
    if (l=s) and (r=s) then  
      begin  
        sum:=sum+b;  
        exit;  
      end;  
    m:=(l+r) div 2;  
    if s<=m then instree(p*2,b,s) else  
      if m+1<=s then instree(p*2+1,b,s);  
    sum:=sum+b;  
  end;  


3 0
原创粉丝点击