Cod sursa(job #421105)

Utilizator ProtomanAndrei Purice Protoman Data 21 martie 2010 10:21:35
Problema Cuburi2 Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 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 ((med == fr || nrCubDr[med] - nrCubDr[y + 1] - nrCubSt[med - 1] + nrCubSt[x - 1] >= 0) 
		&& (med == ls || nrCubSt[med] - nrCubSt[x - 1] - nrCubDr[med + 1] + nrCubDr[y + 1] >= 0))
		poz = med;
	else if (med != fr && nrCubDr[med] - nrCubDr[y + 1] - nrCubSt[med - 1] + nrCubSt[x - 1] <= 0)
		cautaBinPoz(fr, med - 1);
	else cautaBinPoz(med + 1, ls);
}

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;
}