Cod sursa(job #254359)

Utilizator raduzerRadu Zernoveanu raduzer Data 7 februarie 2009 11:29:09
Problema Cuburi2 Scor 20
Compilator cpp Status done
Runda Stelele Informaticii 2009, clasele 9-10, ziua 2 Marime 0.8 kb
#include <cstdio>

const int MAX_N = 250010;
const int INF = 10000000000LL;

int n, m, z;
long long a[MAX_N], b[MAX_N], c[MAX_N];

int main()
{
	int i, x, y;
	freopen("cuburi2.in", "r", stdin);
	freopen("cuburi2.out", "w", stdout);
	
	scanf("%d %d", &n, &m);
	
	for (i = 1; i <= n; ++i) 
	{
		scanf("%lld", &a[i]);
		b[i] = b[i - 1] + a[i];
	}
	
	for (i = n; i; --i) c[i] = c[i + 1] + a[i];
	
	for (; m; --m)
	{
		scanf("%d %d", &x, &y);
		if (x > y) x ^= y ^= x ^= y;
		
		long long s1 = 0, s2 = 0;
		for (i = y - 1; i >= x; --i) s2 += (c[i + 1] - c[y + 1]);
		
		long long mn = INF;
		int p = 0;
		
		for (i = x; i <= y; ++i)
		{
			if (s1 + s2 < mn) mn = s1 + s2, p = i;
			s1 += (b[i] - b[x - 1]);
			s2 -= (c[i + 1] - c[y + 1]);
		}
		
		printf("%d %lld\n", p, mn);
	}
}