Cod sursa(job #421108)

Utilizator ProtomanAndrei Purice Protoman Data 21 martie 2010 10:33:22
Problema Cuburi2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <algorithm>
#include <stdio.h>

#define MAX 250010
#define ll long long

using namespace std;

ll n, m, poz, x, y;
ll vctNrCub[MAX], nrCubSt[MAX], nrCubDr[MAX], costCubSt[MAX], costCubDr[MAX];

inline void cautaBinPoz(int fr, int ls)
{
	if (fr > ls)
		return;
	int med = (fr + ls) / 2;

	if (nrCubSt[med - 1] - nrCubSt[x - 1] < nrCubSt[y] - nrCubSt[med - 1])
	{
		poz = med;
		cautaBinPoz(med + 1, ls);
	}
	else cautaBinPoz(fr, med - 1);
}

int main()
{
	freopen("cuburi2.in", "r", stdin);
	freopen("cuburi2.out", "w", stdout);

	scanf("%lld %lld", &n, &m);

	for (int i = 1; i <= n; i++)
	{
		scanf("%lld", &vctNrCub[i]);

		nrCubSt[i] = nrCubSt[i - 1] + vctNrCub[i];
		costCubSt[i] = costCubSt[i - 1] + nrCubSt[i - 1];
	}

	for (int i = n; i; i--)
	{
		nrCubDr[i] = nrCubDr[i + 1] + vctNrCub[i];
		costCubDr[i] = costCubDr[i + 1] + nrCubDr[i + 1];
	}

	for (; m; m--)
	{
		scanf("%lld %lld", &x, &y);
		if (x > y)
			swap(x, y);

		cautaBinPoz(x, y);

		ll movesSt = costCubSt[poz] - (poz - x) * nrCubSt[x - 1] - costCubSt[x];
		ll movesDr = costCubDr[poz] - (y - poz) * nrCubDr[y + 1] - costCubDr[y];

		printf("%lld %lld\n", poz, movesSt + movesDr);
	}

	fclose(stdin);
	fclose(stdout);
	return 0;
}