Cod sursa(job #1629456)

Utilizator TimitocArdelean Andrei Timotei Timitoc Data 4 martie 2016 15:36:47
Problema Cuburi2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <iostream>
#include <cstdio>
#define MAXN 250050

using namespace std;

int a[MAXN], n, m;
long long pre[MAXN], pre2[MAXN], pre3[MAXN], sum(int, int);

void process()
{
    for (int i = 1; i <= n; i++)
        pre[i] = pre[i-1]+a[i];
    for (int i = 1; i <= n; i++)
        pre2[i] = pre2[i-1]+pre[i-1];
    for (int i = n; i ; --i)
        pre3[i] = pre3[i+1]+sum(i+1, n);
}

long long sum(int x, int y)
{
    return pre[y]-pre[x-1];
}

void solve(int x, int y)
{
    int step = 1;
    for (step; (step<<1) <= (y-x); step<<=1);
    int ind = x;
    for (step; step; step >>= 1)
        if (ind+step <= y && sum(x, ind+step-1) < sum(ind+step, y))
            ind += step;
    printf("%d %lld\n", ind, pre2[ind]-pre2[x]-pre[x-1]*(ind-x) +
                            pre3[ind]-pre3[y] - sum(y+1, n)*(y-ind));
}

int main()
{
    freopen("cuburi2.in", "r", stdin);
    freopen("cuburi2.out", "w", stdout);

    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d ", &a[i]);
    int x, y;
    process();
    while (m--) {
        scanf("%d %d", &x, &y);
        solve(x, y);
    }
    return 0;
}