Cod sursa(job #421001)

Utilizator ProtomanAndrei Purice Protoman Data 20 martie 2010 22:16:10
Problema Cuburi2 Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <algorithm>
#include <stdio.h>

#define MAX 250010
#define ll long long

using namespace std;

int 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)
	{
		poz = ls;

		return;
	}
	int med = (fr + ls) / 2;

	if (nrCubSt[med - 1] - nrCubSt[x - 1] > nrCubDr[med] - nrCubDr[y + 1])
		cautaBinPoz(fr, med - 1);
	else cautaBinPoz(med + 1, ls);
}

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

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

	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &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("%d %d", &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("%d %lld\n", poz, movesSt + movesDr);
	}

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