Cod sursa(job #2064436)

Utilizator lucametehauDart Monkey lucametehau Data 12 noiembrie 2017 12:57:49
Problema Cuburi2 Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <fstream>

using namespace std;

ifstream cin("cuburi2.in");
ofstream cout("cuburi2.out");

const int N_MAX = 250000;

int n, q;
int x, y;

int v[1 + N_MAX];
long long st[1 + N_MAX], dr[1 + N_MAX + 1];
long long move_st[1 + N_MAX + 1], move_dr[1 + N_MAX];

int caut_bin(int a, int b)
{
    int pas, poz;
    for(pas = 1; pas <= b - a + 1; pas <<= 1);
    for(poz = a; pas; pas >>= 1)
        if(poz + pas <= b && st[poz + pas - 1] - st[a - 1] < st[b] - st[poz + pas - 1])
            poz += pas;
    return poz;
}

int main()
{
    cin >> n >> q;
    for(int i = 1; i <= n; i++)
        cin >> v[i];
    st[0] = move_dr[0] = 0;
    for(int i = 1; i <= n; i++)
    {
        st[i] = st[i - 1] + v[i];
        move_dr[i] = move_dr[i - 1] + st[i - 1];
    }
    dr[n + 1] = move_st[n + 1] = 0;
    for(int i = n; i >= 1; i--)
    {
        dr[i] = dr[i + 1] + v[i];
        move_st[i] = move_st[i + 1] + dr[i + 1];
    }
    for(int i = 1; i <= q; i++)
    {
        cin >> x >> y;
        int pozmin = caut_bin(x, y);
        long long sol = 0;
        sol += move_dr[pozmin] - move_dr[x] - st[x - 1] * (pozmin - x); /// stanga
        sol += move_st[pozmin] - move_st[y] - dr[y + 1] * (y - pozmin); /// dreapta
        cout << pozmin << " " << sol << "\n";
    }
    return 0;
}