Pagini recente » Cod sursa (job #1365279) | Cod sursa (job #2746606) | Cod sursa (job #1722864) | Cod sursa (job #1422216) | Cod sursa (job #3339050)
#include <fstream>
using namespace std;
ifstream fin ("cuburi2.in");
ofstream fout ("cuburi2.out");
const int DIM = 250000;
int n, q;
long long s[DIM + 1], si[DIM + 1];
int main()
{
int x, y;
fin >> n >> q;
for (int i = 1; i <= n; i++)
{
fin >> s[i];
si[i] = si[i - 1] + s[i] * i;
s[i] += s[i - 1];
}
while (q--)
{
fin >> x >> y;
long long sum = (s[y] - s[x - 1] + 1) / 2;
int l = x, r = y, mid;
while (l <= r)
{
mid = (l + r) / 2;
if (s[mid] - s[x - 1] >= sum)
r = mid - 1;
else
l = mid + 1;
}
int pos = l;
///s[i] * (pos - i) = s[i] * pos - s[i] * i -> x ... pos - 1
///s[i] * (i - pos) = s[i] * i - s[i] * pos -> pos + 1 ... y
long long sl = ((s[pos - 1] - s[x - 1]) * pos - (si[pos - 1] - si[x - 1]));
long long sr = ((si[y] - si[pos]) - (s[y] - s[pos]) * pos);
fout << pos << " " << sl + sr << "\n";
}
return 0;
}