Advertisement
uopspop

Untitled

May 10th, 2016
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. class FoodList{
  6.    
  7.     public:
  8.         FoodList(int length_){
  9.             length = length_;      
  10.         }
  11.         void createAccumulatingList(){
  12.             int tmpValue = 0;  
  13.             for (int i = 0; i < length; i++){
  14.                 accumulatingList.push_back(list[i] + tmpValue);
  15.                 tmpValue = accumulatingList[i];    
  16.             }
  17.         }      
  18.         vector<int> list;  
  19.         vector<int> accumulatingList;
  20.     private:   
  21.         int length;    
  22. };
  23.  
  24.  
  25. int main(){
  26.    
  27.     int n, m;
  28.     while(cin >> n){
  29.         cin >> m;
  30.         FoodList myFoodList(n);    
  31.         // food value
  32.         for (int i = 0; i < n; i++){
  33.             int value;
  34.             cin >> value;
  35.             myFoodList.list.push_back(value);
  36.         }
  37.    
  38.         // ValueSumUp
  39.         myFoodList.createAccumulatingList();
  40.  
  41. /*
  42. // debugging
  43.         for (vector<int>::iterator it = myFoodList.accumulatingList.begin();
  44.              it != myFoodList.accumulatingList.end();
  45.              it++ )
  46.         {
  47.             cout << *it << " ";
  48.         }
  49.         cout << endl;
  50. */
  51. // calculate
  52.         for (int i = 0; i < m; i++){
  53.             int sum = 0;
  54.             int start, end;
  55.             cin >> start >> end;
  56.             if ((start-2) < 0)
  57.                 sum =  myFoodList.accumulatingList[end-1];
  58.             else   
  59.                 sum += myFoodList.accumulatingList[end-1] -  myFoodList.accumulatingList[start-2];
  60.             cout << sum << endl;
  61.         }
  62.        
  63.     }// end while
  64.    
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement