Cod sursa(job #1629419)

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

using namespace std;

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

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);
}

int 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 = y;
    for (step; step; step >>= 1)
        if (ind-step >= x && sum(x, ind-step) > sum(ind-step+1, 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;
}