Pagini recente » Cod sursa (job #1691544) | Cod sursa (job #2891650) | Istoria paginii runda/preoji_bv_1/clasament | Cod sursa (job #2032041) | Cod sursa (job #607316)
Cod sursa(job #607316)
/* Robert Rocks */
# include <cstdio>
typedef long long ll;
const char *FIN = "cuburi2.in", *FOU = "cuburi2.out";
const int MAX = 250005;
int N, M;
ll S[3][MAX];
int main (void) {
freopen (FIN, "r", stdin);
freopen (FOU, "w", stdout);
scanf ("%d %d", &N, &M);
for (int i = 1, R; i <= N; ++i) {
scanf ("%d", &R);
S[0][i] = S[0][i - 1] + R;
S[1][i] = S[1][i - 1] + S[0][i - 1];
}
for (int i = N; i; --i)
S[2][i] = S[2][i + 1] + S[0][N] - S[0][i];
for (int i = 1, x, y, cnt, sol; i <= M; ++i) {
scanf ("%d %d", &x, &y);
for (cnt = 1; cnt < y - x; cnt <<= 1);
for (sol = x; cnt; cnt >>= 1)
if (sol + cnt <= y && S[0][sol + cnt - 1] - S[0][x - 1] < S[0][y] - S[0][sol + cnt - 1])
sol += cnt;
printf ("%d %lld\n", sol, S[1][sol] - S[1][x] - S[0][x - 1] * (sol - x) + S[2][sol] - S[2][y] - (y - sol) * (S[0][N] - S[0][y]));
}
}