Cod sursa(job #607316)

Utilizator SpiderManSimoiu Robert SpiderMan Data 11 august 2011 17:05:02
Problema Cuburi2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
/* 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]));
    }
}