0

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

4

1 回答 1

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;
}
于 2014-12-20T17:36:28.083 回答