Cod sursa(job #465350)

Utilizator mathboyDragos-Alin Rotaru mathboy Data 23 iunie 2010 22:16:25
Problema Cuburi2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <cstdio>

#define maxN 250010
using namespace std;

int V[maxN], n, m, x, y, st, dr, mid, sol, aux, i;
long long Cst[maxN], Nst[maxN], Cdr[maxN], Ndr[maxN], cost1, cost2;
int main ()
{
	freopen ("cuburi2.in", "r", stdin);
	freopen ("cuburi2.out", "w", stdout);
	scanf ("%d%d\n", &n, &m);
	for (i = 1; i <= n; i++)
       	{
		scanf ("%d", &V[i]);
		Nst[i] = Nst[i - 1] + V[i];
		Cst[i] = Cst[i - 1] + Nst[i - 1];
	}
	for (i = n; i >= 1; i--)
	{
		Ndr[i] = Ndr[i + 1] + V[i];
		Cdr[i] = Cdr[i + 1] + Ndr[i + 1];
	}
	while (m--)
	{
		scanf ("%d%d\n", &x, &y);
		if (x > y) {
			aux = x;
			x = y;
			y = aux;
		}
		st = 1; dr = n;
		while (st <= dr)
		{
			mid = (st + dr)	>> 1;
			if (Nst[mid - 1] - Nst[x - 1] < Nst[y] - Nst[mid - 1])
				sol = mid, st = mid + 1;
			else dr = mid - 1;
		}
		cost1 = Cst[sol] - (sol - x) * Nst[x - 1] - Cst[x];
		cost2 = Cdr[sol] - (y - sol) * Ndr[y + 1] - Cdr[y];
		printf ("%d %lld\n", sol, cost1 + cost2);
	}
	return 0;
}