Advertisement
Korotkodul

ДО A OK

Apr 1st, 2022 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int,int>
  11. #define vec vector
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. using db = double;
  16. void cv(vector <int> &v){
  17.     for (auto x: v) cout<<x<<' ';
  18.     cout<<"\n";
  19. }
  20.  
  21. void cvl(vector <ll> &v){
  22.     for (auto x: v) cout<<x<<' ';
  23.     cout<<"\n";
  24. }
  25.  
  26.  
  27. void cvv(vector <vector <int> > &v){
  28.     for (auto x: v) cv(x);
  29.     cout<<"\n";
  30. }
  31.  
  32. void cvb(vector <bool> v){
  33.     for (bool x: v) cout<<x<<' ';
  34.     cout<<"\n";
  35. }
  36.  
  37. void cvs(vector <string>  v){
  38.     for (auto a: v){
  39.         cout<<a<<"\n";
  40.     }
  41. }
  42.  
  43.  
  44. int N;
  45.  
  46. int L,R;
  47.  
  48. struct tree{
  49.     vector <ll> S;
  50.     void bld(){
  51.         cin>>N;
  52.         int rawN=N;
  53.         int k = log2(N);
  54.         if (pow(2, k) < N){
  55.             k++;
  56.         }
  57.         N = pow(2,k);
  58.         S.assign(2*N-1, 0);
  59.         for (int i = N-1; i < N - 1 + rawN; ++i){
  60.             cin>>S[i];
  61.         }
  62.         for (int i = N-2; i >= 0; --i){
  63.             S[i] = S[2*i+1] + S[2*i+2];
  64.         }
  65.     }
  66.     void sh(){
  67.         int k = log2(N);
  68.         int id=-1;
  69.         for (int i = 0; i <= k;++i){
  70.             for (int j = 0; j < pow(2, i); ++j){
  71.                 id++;
  72.                 cout<<S[id]<<' ';
  73.             }cout<<"\n";
  74.         }
  75.     }
  76.  
  77.     ll getS(int l, int r, int id){
  78.         //cout<<"L R = "<<L<<' '<<R<<"\n";
  79.         //cout<<"l r  = "<<l<<' '<<r<<"\n";
  80.         if (l >= L && r <= R){
  81.             //cout<<"A\n";
  82.             return S[id];
  83.         }
  84.         else if (r < L || l > R){
  85.             //cout<<"B\n";
  86.             return 0;
  87.         }
  88.         //cout<<"C\n";
  89.         int m = (l+r)/2;
  90.         ll A = getS(l, m, 2*id+1), B = getS(m+1, r, 2*id+2);
  91.         return A+B;
  92.     }
  93. };
  94.  
  95. int main()
  96. {
  97.     ios::sync_with_stdio(0);
  98.     cin.tie(0);
  99.     cout.tie(0);
  100.     tree T;
  101.     T.bld();
  102.     //T.sh();
  103.     int K; cin>>K;
  104.     for (int i=0;i<K;++i){
  105.         cin>>L>>R;
  106.         L--; R--;
  107.         ll r = T.getS(0, N-1, 0);
  108.         cout<<r<<' ';
  109.     }
  110.  
  111.  
  112. }
  113. /*
  114. 7
  115. 1 2 3 4 5 6 7
  116. */
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement