【问题标题】:Incorrect update elements in Fenwick TreeFenwick 树中的更新元素不正确
【发布时间】:2014-12-21 01:36:28
【问题描述】:

我写了一个程序,它通过获取命令 '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 树,但它对我不起作用。

【问题讨论】:

    标签: fenwick-tree


    【解决方案1】:

    已修复:

    #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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-09
      相关资源
      最近更新 更多