Pagini recente » Cod sursa (job #41892) | Cod sursa (job #2705936) | Cod sursa (job #2884648) | Cod sursa (job #1421385) | Cod sursa (job #2476058)
#include <fstream>
#define ll long long
const int MAX=25*1e4;
using namespace std;
ll v[MAX+1],s_st[MAX+1],s_dr[MAX+1],ss_st[MAX+1],ss_dr[MAX+1];
ifstream fin("cuburi2.in");
ofstream fout("cuburi2.out");
int main() {
ll n,i,m,x,y,st,dr,mij,poz;
ll sumst,sumdr;
fin>>n>>m;
for(i=1;i<=n;i++)
fin>>v[i];
for(i=1;i<=n;i++) {
s_st[i]=s_st[i-1]+v[i];
ss_st[i]=ss_st[i-1]+s_st[i-1];
}
for(i=n;i>=1;i--) {
s_dr[i]=s_dr[i+1]+v[i];
ss_dr[i]=ss_dr[i+1]+s_dr[i+1];
}
for(i=1;i<=m;i++) {
fin>>x>>y;
st=x;
dr=y;
while(st<=dr) {
mij=(st+dr)/2;
if(s_st[mij-1]-s_st[x-1]<s_st[y]-s_st[mij-1]) {
st=mij+1;
poz=mij;
} else
dr=mij-1;
}
sumst=ss_st[poz]-ss_st[x]-s_st[x-1]*(poz-x);
sumdr=ss_dr[poz]-ss_dr[y]-s_dr[y+1]*(y-poz);
fout<<poz<<' '<<sumst+sumdr<<'\n';
}
return 0;
}