Cod sursa(job #1207593)

Utilizator tudormaximTudor Maxim tudormaxim Data 13 iulie 2014 14:24:52
Problema Cuburi2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <cstdio>
#define MAX 250005
#define ll long long
int N, M;
ll S[3][MAX];
int main ()
{
    freopen ("cuburi2.in", "r", stdin);
    freopen ("cuburi2.out", "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]));
    }
    fclose(stdin);
    fclose(stdout);
}