先一维:
通过修改点,统计区间.
输入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.
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.
![](http://hi.csdn.net/attachment/201002/10/772845_1265812791J4IQ.jpg)
参考代码:
上面是两个树状数组一维的例子,一个是修改点,一个是修改区间。
二维的树状数组运用: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);
--------------
二、一维线段树
分析: