DSA
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
1.4 KiB

  1. #include <bits/stdc++.h>
  2. #define F first
  3. #define S second
  4. #define pb(x) push_back(x)
  5. #define iosb ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
  6. #define BIT(x) __builtin_popcount(x)
  7. using namespace std ;
  8. const int MAXN = 1e6+1 ;
  9. const int MaxN = 1e5+1 ;
  10. const int N = 1e4+1 ;
  11. const int M = 1e3+1 ;
  12. const int MOD = 1e9+7 ;
  13. const int inf = 2e9+7 ;
  14. const long long INF = 2e18+7 ;
  15. int t[MaxN] , n , a[MaxN] ;
  16. int sum(int v)
  17. {
  18. int res = 0 ;
  19. if(v==-1)
  20. return 0 ;
  21. for(; v > 0 ; v = (v & (v + 1)) - 1)
  22. res += t[v] ;
  23. return res ;
  24. }
  25. void upd(int pos , int delta)
  26. {
  27. a[pos] += delta ;
  28. for( ; pos < n ; pos |= pos+1)
  29. t[pos] += delta ;
  30. }
  31. /*
  32. int get_min(int v)
  33. {
  34. int res = inf ;
  35. for (; v >= 0; v = (v & (v + 1)) - 1)
  36. res = min(res , f[v]) ;
  37. return res ;
  38. }
  39. void assign(int pos, int delta)
  40. {
  41. a[pos] = delta ;
  42. for (; pos < n ; pos |= pos + 1)
  43. f[pos] = min(f[pos] , delta) ;
  44. }
  45. */
  46. main()
  47. {
  48. // freopen(".in" , "r" , stdin) ;
  49. // freopen(".out" , "w" , stdout) ;
  50. cin >> n ;
  51. for(int i = 0 ; i < n ; ++i)
  52. {
  53. int x ;
  54. cin >> x ;
  55. upd(i , x) ;
  56. }
  57. /*
  58. for(int i = 0 ; i < n ; ++i)
  59. f[i] = inf ;
  60. for(int i = 0 ; i < n ; ++i)
  61. {
  62. int t ;
  63. cin >> t ;
  64. assign(i , t) ;
  65. }
  66. */
  67. int q ;
  68. cin >> q ;
  69. while(q--)
  70. {
  71. int l , r ;
  72. cin >> l >> r ;
  73. cout << sum(r-1) - sum(l-2) << '\n' ;
  74. }
  75. return 0 ;
  76. }