#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[1000001];
int st[4000001];
int n,k;
void build(int id,int l,int r)
{
if (l==r)
{
st[id]=a[l]; // fixed the index here
}
else
{
int mid=(l+r) >> 1;
build(id*2,l,mid);
build(id*2+1,mid+1,r);
st[id]=st[id*2]+st[id*2+1];
}
}
int get(int id,int l,int r,int u,int v)
{
if (u > r || v < l) return 0;
if (l >= u && r <= v) return st[id]; // changed l > u to l >= u
int mid =( l + r ) / 2;
int dp1 = get(id*2,l,mid,u,v);
int dp2 = get(id*2+1,mid+1,r,u,v); // fixed the index here
return dp1 + dp2;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>k;
for (int i=1;i<=n;i++)
{
cin>>a[i];
}
build(1,1,n);
while (k--)
{
int u,v;
cin>>u>>v;
cout<<get(1,1,n,u,v)<<"\n";
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBpbnQgbG9uZyBsb25nCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmludCBhWzEwMDAwMDFdOwppbnQgc3RbNDAwMDAwMV07CmludCBuLGs7CnZvaWQgYnVpbGQoaW50IGlkLGludCBsLGludCByKQp7CiAgICBpZiAobD09cikKICAgIHsKICAgICAgICBzdFtpZF09YVtsXTsgLy8gZml4ZWQgdGhlIGluZGV4IGhlcmUKICAgIH0KICAgIGVsc2UKICAgIHsKICAgICAgICBpbnQgbWlkPShsK3IpID4+IDE7CiAgICAgICAgYnVpbGQoaWQqMixsLG1pZCk7CiAgICAgICAgYnVpbGQoaWQqMisxLG1pZCsxLHIpOwogICAgICAgIHN0W2lkXT1zdFtpZCoyXStzdFtpZCoyKzFdOwogICAgfQp9CmludCBnZXQoaW50IGlkLGludCBsLGludCByLGludCB1LGludCB2KQp7CiAgICBpZiAodSA+IHIgfHwgdiA8IGwpIHJldHVybiAwOwogICAgaWYgKGwgPj0gdSAmJiByIDw9IHYpIHJldHVybiBzdFtpZF07IC8vIGNoYW5nZWQgbCA+IHUgdG8gbCA+PSB1CiAgICBpbnQgbWlkID0oIGwgKyByICkgLyAyOwogICAgaW50IGRwMSA9IGdldChpZCoyLGwsbWlkLHUsdik7CiAgICBpbnQgZHAyID0gZ2V0KGlkKjIrMSxtaWQrMSxyLHUsdik7IC8vIGZpeGVkIHRoZSBpbmRleCBoZXJlCiAgICByZXR1cm4gZHAxICsgZHAyOwp9CnNpZ25lZCBtYWluKCkKewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsKICAgIGNpbi50aWUoMCk7Y291dC50aWUoMCk7CiAgICBjaW4+Pm4+Pms7CiAgICBmb3IgKGludCBpPTE7aTw9bjtpKyspCiAgICB7CiAgICAgICAgY2luPj5hW2ldOwogICAgfQogICAgYnVpbGQoMSwxLG4pOwogICAgd2hpbGUgKGstLSkKICAgIHsKICAgICAgICBpbnQgdSx2OwogICAgICAgIGNpbj4+dT4+djsKICAgICAgICBjb3V0PDxnZXQoMSwxLG4sdSx2KTw8IlxuIjsKICAgIH0KfQo=