Cod sursa(job #2254086)

Utilizator stefan_creastaStefan Creasta stefan_creasta Data 4 octombrie 2018 19:26:21
Problema Cuburi2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <cstdio>
using namespace std;
const int NMAX = 250005;
long long spst[NMAX];
long long spstt[NMAX];
long long spdrt[NMAX];

int main() {
  int n, m;
  freopen("cuburi2.in", "r", stdin);
  freopen("cuburi2.out", "w", stdout);
  scanf("%d%d", &n, &m);
  for(int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    spst[i] = x + spst[i - 1];
    spstt[i] = spstt[i - 1] + spst[i];
  }
  for(int i = n; i > 0; i--) {
    spdrt[i] = spdrt[i + 1] + spst[n] - spst[i - 1];
  }
  for(int q = 1; q <= m; q++) {
    int x, y;
    scanf("%d%d", &x, &y);
    int st = x, dr = y, last = x;
    while(st <= dr) {
      int mij = (st + dr) >> 1;
      if(spst[mij - 1] - spst[x - 1] < spst[y] - spst[mij - 1]) {
        st = mij + 1;
        last = mij;
      }
      else {
        dr = mij - 1;
      }
    }
    long long sol = (spstt[last - 1] - spstt[x - 1] - spst[x - 1] * (last - x)) + (spdrt[last + 1] - spdrt[y + 1] - (spst[n] - spst[y]) * (y - last));
    printf("%d %lld\n", last, sol);
  }
  return 0;
}