Cod sursa(job #2471917)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 11 octombrie 2019 18:49:59
Problema Cuburi2 Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.23 kb
/*
                `-/oo+/-   ``
              .oyhhhhhhyo.`od
             +hhhhyyoooos. h/
            +hhyso++oosy- /s
           .yoooossyyo:``-y`
            ..----.` ``.-/+:.`
                   `````..-::/.
                  `..```.-::///`
                 `-.....--::::/:
                `.......--::////:
               `...`....---:::://:
             `......``..--:::::///:`
            `---.......--:::::////+/`
            ----------::::::/::///++:
            ----:---:::::///////////:`
            .----::::::////////////:-`
            `----::::::::::/::::::::-
             `.-----:::::::::::::::-
               ...----:::::::::/:-`
                 `.---::/+osss+:`
                   ``.:://///-.
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <cmath>
#define INF 0x3f3f3f3f
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

using namespace std;

const int N = 250000;
typedef long long ll;

ll v[5 + N];
ll spst[5 + N], spdr[5 + N];
ll spst2[5 + N], spdr2[5 + N];

int main()
{
    freopen("cuburi2.in","r",stdin);
    freopen("cuburi2.out","w",stdout);
    ll n, m, i;
    scanf("%lld%lld", &n, &m);
    for(i = 1; i <= n; i++) scanf("%lld", &v[i]);
    for(i = 1; i <= n; i++){
        spst[i] = spst[i - 1] + v[i];
        spst2[i] = spst2[i - 1] + spst[i - 1];
    }
    for(i = n; i >= 1; i--){
        spdr[i] = spdr[i + 1] + v[i];
        spdr2[i] = spdr2[i + 1] + spdr[i + 1];
    }
    for(i = 1; i <= m; i++){
        ll x, y, st, dr, mid, rez;
        ll sumst, sumdr;
        sumst = sumdr = 0;
        scanf("%lld%lld", &x, &y);
        st = x;
        dr = y;
        while(st <= dr){
            mid = (st + dr) >> 1;
            if(spst[mid - 1] - spst[x - 1] <= spst[y] - spst[mid - 1]){
                rez = mid;
                st = mid + 1;
            } else dr = mid - 1;
        }
        sumst = spst2[rez] - spst2[x] - (spst[x - 1] * (rez - x));
        sumdr = spdr2[rez] - spdr2[y] - (spdr[y + 1] * (y - rez));
        printf("%lld %lld\n", rez, sumst + sumdr);
    }
    return 0;
}