Cod sursa(job #2253757)

Utilizator MoodyFaresFares Mohamad MoodyFares Data 4 octombrie 2018 12:45:22
Problema Cuburi2 Scor 90
Compilator cpp Status done
Runda shimulare_fara_shim Marime 1.14 kb
#include <fstream>

using namespace std;

const int MAX_N = 250000;

ifstream fin("cuburi2.in");
ofstream fout("cuburi2.out");

int a[5 + MAX_N];
long long spLeft[5 + MAX_N];
long long spLeft1[5 + MAX_N];
long long spRight[5 + MAX_N];
long long spRight1[5 + MAX_N];

int main() {
  int n, m;
  fin >> n >> m;
  for (int i = 1; i <= n; i++) {
    fin >> a[i];
    spLeft[i] = spLeft[i - 1] + 1LL * a[i];
    spLeft1[i] = spLeft1[i - 1] + spLeft[i - 1];
  }
  for (int i = n; i >= 1; i--) {
    spRight[i] = spRight[i + 1] + 1LL * a[i];
    spRight1[i] = spRight1[i + 1] + spRight[i + 1];
  }
  for (int i = 1; i <= m; i++) {
    int x, y;
    fin >> x >> y;
    int left, right;
    left = x;
    right = y;
    int last = left;
    while (left <= right) {
      int med = (left + right) >> 1;
      if (spLeft[med - 1] - spLeft[x - 1] < spLeft[y] - spLeft[med - 1]) {
        left = med + 1;
        last = med;
      } else {
        right = med - 1;
      }
    }
    fout << last << " ";
    fout << (spLeft1[last] - spLeft1[x - 1] - spLeft[x - 1] * (last - x + 1)) + (spRight1[last] - spRight1[y + 1] - spRight[y + 1] * (y - last + 1)) << '\n';
  }
  return 0;
}