摸清一、二维树状数组与线段树

先一维:

通过修改点,统计区间.

输入Q 5

输入C 2 6

5在这个区间里了。所以输出1.

再输入Q 3

输入C 2 6

输入当然是2.

通过修改区间,统计点。

翻纸牌差不多。

There is an array of n cards. Each card is putted face down on table. You have two queries:  1. T i j (turn cards from index i to index j, include i-th and j-th card - card which was face down will be face up; card which was face up will be face down)  2. Q i (answer 0 if i-th card is face down else answer 1)
 
This has solution for each query (and 1 and 2) has time complexity O(log n).
In array f (of length n + 1) we will store each query T (i , j) - we set f[i]++ and f[j + 1]--.
For each card k between i and j (include i and j) sum f[1] + f[2] + ... + f[k] will be increased for 1, for all others will be same as before (look at the image  for clarification).
So our solution will be described sum (which is same as cumulative frequency) module 2.
参考代码:

 

 

上面是两个树状数组一维的例子,一个是修改点,一个是修改区间。

二维的树状数组运用:poj 2155 matrix

翻牌的二维使用:

代码:

  

怎么理解二维树状的含义,而为什么又要    

   updata(p2.x,p2.y);
    updata(p1.x-1,p2.y);
    updata(p2.x,p1.y-1);
    updata(p1.x-1,p1.y-1);

 

--------------

二、一维线段树

 

分析:

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

helihui123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值