Kali_prasad

subarray sum

Apr 3rd, 2022 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #pragma GCC optimize ("O3")
  2. #pragma GCC target ("sse4")
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7. typedef long long ll;
  8. typedef pair<int, int> pii;
  9. typedef pair<string,int> psi;
  10. typedef vector<int> vi;
  11. typedef vector<string> vs;
  12. typedef vector<char> vc;
  13. typedef vector<ll> vll;
  14. typedef vector<vector<int>> vvi;
  15. typedef vector<vector<string>> vvs;
  16. typedef vector<vector<ll>> vvll;
  17.  
  18. #define FOR(i, a, b) for (auto i=a; i<=(b); i++)
  19. #define FORd(i,b,a) for (int i =b; i >= a; i--)
  20. #define sz(x) (int)(x).size()
  21. #define mp make_pair
  22. #define pb push_back
  23. #define f first
  24. #define s second
  25. #define ins insert
  26.  
  27. const int MOD = 1000000007;
  28. //type functions here
  29.  
  30.  
  31.  int main()
  32. {
  33.      std::ios::sync_with_stdio(false);
  34.     cin.tie(NULL);
  35.     cout.tie(0);
  36.     vll v,ps;
  37.      ll size,sum=0;
  38.      cin>>size;
  39.      FOR(q,1,size)
  40.      {
  41.          ll temp;
  42.          cin>>temp;
  43.          v.pb(temp);
  44.          sum+=temp;
  45.          ps.pb(sum);
  46.      }
  47.     ll i,j,ans;
  48.  
  49.    
  50.      ll z;
  51.      cin>>z;
  52.      FOR(w,1,z){
  53.          cin>>i>>j;
  54.       if(i==0)
  55.           cout<<ps[j]<<endl;
  56.       else
  57.       {ans=ps[j]-(ps[i-1]);
  58.        cout<<ans<<endl;
  59.       }
  60.      }
  61.    
  62. }
  63.  
  64.  
  65.  
  66.  
Add Comment
Please, Sign In to add comment