fork download
  1. /**
  2. - Solution of: https://m...content-available-to-author-only...j.com/problem/59
  3. - Programming language: C++ 11/14/17/Themis
  4. - Catetory: Prefix-sum, Basic
  5. **/
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8. #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
  9. #define mofile(s) freopen(s,"r",stdin)
  10. #define outfile(s) freopen(s,"w",stdout)
  11. #define ll long long
  12. #define ii pair<int,int>
  13. #define iii pair<int,ii>
  14. #define fi first
  15. #define se second
  16. #define tf bool
  17. #define ST stack
  18. #define DQ deque
  19. #define Q queue
  20. #define S string
  21. #define Ma map
  22. #define UM unordered_map
  23. #define SE set
  24. #define str(x) to_string(x)
  25. #define all(a) (a).begin(),(a).end()
  26. #define FOR(i,l,r,d) for(int i=l;i<=r;i+=d)
  27. #define FOD(i,l,r,d) for(int i=r;i>=l;i-=d)
  28. #define xuong cout<<"\n"
  29. #define debug(x) cout<<(x)<<" "
  30. #define ppcnt(x) __builtin_popcountll(x)
  31. #define parity(x) __builtin_parityll(x)
  32. #define lead0(x) __builtin_clzll(x)
  33. #define LOG2 __lg(x)
  34. #define tr0(x) __builtin_ctzll(x)
  35. #define fiset(x) __builtin_ffsll(x)
  36. #define MASK(k) (1LL<<(k))
  37. #define BIT(x,k) ((x)>>(k)&1)
  38. #define pb push_back
  39. #define tron(x) setprecision(x)
  40. #define het return 0
  41. #define base_ 1000000000
  42. const int maxn=1e6+5;
  43. const ll tle=2e8;
  44. const int base=31;
  45. string bcc="abcdefghijklmnopqrstuvwxyz";
  46. int dx[]={-1,0,1,0};
  47. int dy[]={0,1,0,-1};
  48. bool sang[10000005];
  49. ll pref[1005][1005],mt[1005][1005];
  50. void sieve(){
  51. for(int i=1;i<=10000000;++i) sang[i]=1;
  52. sang[0]=sang[1]=0;
  53. for(int i=2;i*i<=10000000;++i){
  54. if(sang[i]){
  55. for(int j=i*i;j<=10000000;j+=i) sang[j]=0;
  56. }
  57. }
  58. }
  59. void lis(){
  60. vector<int>t;
  61. vector<int>a;
  62. int n; cin>>n;
  63. for(int i=1;i<=n;++i){
  64. int ai; cin>>ai;
  65. a.pb(ai);
  66. }
  67. for(int x:a){
  68. auto it=lower_bound(all(t),x);
  69. if(it==t.end()) t.pb(x);
  70. else *it=x;
  71. }
  72. }
  73. void pfs2d(){
  74. int n,m,k; cin>>n>>k; m=n;
  75. for(int i=1;i<=n;++i){
  76. for(int j=1;j<=m;++j) cin>>mt[i][j];
  77. }
  78. for(int i=1;i<=n;++i){
  79. for(int j=1;j<=m;++j) pref[i][j]=mt[i][j]+pref[i-1][j]+pref[i][j-1]-pref[i-1][j-1];
  80. }
  81. }
  82. ll qu2d(int x1,int y1,int x2,int y2){
  83. return pref[x2][y2]-pref[x1-1][y2]-pref[x2][y1-1]+pref[x1-1][y1-1];
  84. }
  85. void open(){
  86. if(fopen("tongARTKEY.INP","r")){
  87. mofile("tongARTKEY.INP");
  88. outfile("tongARTKEY.OUT");
  89. }
  90. }
  91. ll a[maxn],pfs[maxn];
  92. int main(){
  93. fast;
  94. int n,q; cin>>n>>q;
  95. for(int i=1;i<=n;++i) cin>>a[i];
  96. for(int i=1;i<=n;++i) pfs[i]=pfs[i-1]+a[i];
  97. while(q--){
  98. int l,r; cin>>l>>r;
  99. cout<<pfs[r]-pfs[l-1]<<"\n";
  100. }
  101. het;
  102. }
  103. // Prefix-sum formula: sum(l,r) = pfs[r]-pfs[l-1]
Success #stdin #stdout 0s 5624KB
stdin
5 3
1 3 -2 3 4
2 3
1 4
3 5
stdout
1
5
5