Fenwick Tree中的更新元素不正确

时间:2014-12-20 16:54:19

标签: fenwick-tree

我编写了一个程序,它通过获取命令's'来给出范围的总和,并通过命令'u'更新元素,但它的工作原理不正确。 请帮帮我。

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

template <typename indexT, typename valueT> class FenwickTree{
public:
                FenwickTree(vector<valueT> &buffer){
                                tree.resize(buffer.size());
                                for (indexT i = 0; i < buffer.size(); ++i)
                                                update(i, buffer[i]);
                }
                valueT getSolution(const indexT l, const indexT r) const{
                                return getSolution(r) - getSolution(l - 1);
                }
                void update(indexT position, const valueT v){
                                valueT delta = v - tree[position];
                                for (indexT i = position; i < tree.size(); i = i | (i + 1))
                                                tree[i] += delta;
                }
private:
                vector <valueT> tree;
                valueT getSolution(const indexT r) const{
                                valueT result = 0;
                                for (indexT i = r; i >= 0; i = (i & (i + 1)) - 1)
                                                result = tree[i] + result;
                                return result;
                }
};

int main(){
                ifstream file;
                file.open("D:\\input.txt", ios::in);
                unsigned long long n, k;
                file >> n;
                vector<long long> buffer(n);
                for (long long i = 0; i < n; ++i)
                                file >> buffer[i];
                file >> k;
                FenwickTree<long long, long long> fenwickTree(buffer);
                for (long long i = 0; i < k; ++i){
                                char command;
                                long long a, b;
                                file >> command >> a >> b;
                                if (command == 's')
                                                cout << fenwickTree.getSolution(a - 1, b - 1) << " ";
                                else if (command == 'u')
                                                fenwickTree.update(a - 1, b);
                }
                file.close();
                return 0;
}

正确的工作:

input: 
10
613 263 312 670 216 142 976 355 488 370
10
s 2 7
s 4 8
u 7 969
u 1 558
s 2 7
u 2 731
s 4 9
s 1 3
u 8 76
u 5 377

输出:

2579 2359 2572 2840 1601 

我计算新旧值和新值之间的差值,然后更新fenwick树,但它对我不起作用。

1 个答案:

答案 0 :(得分:0)

修正了它:

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

template <typename indexT, typename valueT> class FenwickTree{
public:
                FenwickTree(vector<valueT> &buffer){
                                tree.resize(buffer.size());
                                for (indexT i = 0; i < buffer.size(); ++i)
                                                update(i, buffer[i]);
                }
                valueT getSolution(const indexT l, const indexT r) const{
                                return getSolution(r) - getSolution(l - 1);
                }
                void update(indexT position, const valueT v){
                                for (indexT i = position; i < tree.size(); i = i | (i + 1))
                                                tree[i] += v;
                }
private:
                vector <valueT> tree;
                valueT getSolution(const indexT r) const{
                                valueT result = 0;
                                for (indexT i = r; i >= 0; i = (i & (i + 1)) - 1)
                                                result += tree[i];
                                return result;
                }
};

int main(){
                ifstream file;
                file.open("D:\\input.txt", ios::in);
                unsigned long long n, k;
                file >> n;
                vector<long long> buffer(n);
                for (long long i = 0; i < n; ++i)
                                file >> buffer[i];
                file >> k;
                FenwickTree<long long, long long> fenwickTree(buffer);
                for (long long i = 0; i < k; ++i){
                                char command;
                                long long a, b;
                                file >> command >> a >> b;
                                if (command == 's')
                                                cout << fenwickTree.getSolution(a - 1, b - 1) << " ";
                                else if (command == 'u'){
                                                fenwickTree.update(a - 1, b - buffer[a - 1]);
                                                buffer[a - 1] = b;
                                }
                }
                file.close();
                int lal = 0;
                cin >> lal;
                return 0;
}