Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- class FoodList{
- public:
- FoodList(int length_){
- length = length_;
- }
- void createAccumulatingList(){
- int tmpValue = 0;
- for (int i = 0; i < length; i++){
- accumulatingList.push_back(list[i] + tmpValue);
- tmpValue = accumulatingList[i];
- }
- }
- vector<int> list;
- vector<int> accumulatingList;
- private:
- int length;
- };
- int main(){
- int n, m;
- while(cin >> n){
- cin >> m;
- FoodList myFoodList(n);
- // food value
- for (int i = 0; i < n; i++){
- int value;
- cin >> value;
- myFoodList.list.push_back(value);
- }
- // ValueSumUp
- myFoodList.createAccumulatingList();
- /*
- // debugging
- for (vector<int>::iterator it = myFoodList.accumulatingList.begin();
- it != myFoodList.accumulatingList.end();
- it++ )
- {
- cout << *it << " ";
- }
- cout << endl;
- */
- // calculate
- for (int i = 0; i < m; i++){
- int sum = 0;
- int start, end;
- cin >> start >> end;
- if ((start-2) < 0)
- sum = myFoodList.accumulatingList[end-1];
- else
- sum += myFoodList.accumulatingList[end-1] - myFoodList.accumulatingList[start-2];
- cout << sum << endl;
- }
- }// end while
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement