Pagini recente » Cod sursa (job #939831) | Cod sursa (job #2513611) | Cod sursa (job #1425405) | Cod sursa (job #1320151) | Cod sursa (job #1500390)
#include <cstdio>
using namespace std;
const int MAXN = 250010;
int V[MAXN];
long long A[MAXN], Left[MAXN];
long long B[MAXN], Right[MAXN];
long long f (int X, int mid, int Y)
{
//calculeaza costul pentru a muta toate cuburile din [X, Y] pe turnul de pe pozitia mid
long long ret = 0;
ret = Right[mid] - Right[Y];
ret -= 1LL * B[Y + 1] * (Y - mid);
ret += Left[mid] - Left[X];
ret -= 1LL * A[X - 1] * (mid - X);
return ret;
}
const int BUFF_SIZE = (1 << 18);
char Buffer[BUFF_SIZE];
int at;
inline char getChar ()
{
if (at == BUFF_SIZE){
fread (Buffer,1, BUFF_SIZE, stdin);
at = 0;
}
const char c = Buffer[at];
at ++;
return c;
}
inline int getInt ()
{
int ret = 0;
char c;
do{
c = getChar ();
} while (c < '0' || c > '9');
do{
ret = (ret * 10) + (c - '0');
c = getChar ();
} while (c >= '0' && c <= '9');
return ret;
}
int main ()
{
freopen ("cuburi2.in", "r", stdin);
freopen ("cuburi2.out", "w", stdout);
int N, M;
int i;
at = BUFF_SIZE;
N = getInt ();
M = getInt ();
for (i = 1; i <= N; i ++){
V[i] = getInt ();
A[i] = A[i - 1] + V[i];
Left[i] = Left[i - 1] + A[i - 1];
}
for (i = N; i; i --){
B[i] = B[i + 1] + V[i];
Right[i] = Right[i + 1] + B[i + 1];
}
int x, y;
int step, idx;
while (M --){
x = getInt ();
y = getInt ();
for (step = 1; step < y - x + 1; step <<= 1);
for (i = x - 1; step; step >>= 1)
if (i + step <= y)
if (A[i + step] - A[x - 1] >= A[y] - A[i + step])
idx = i + step;
else
i += step;
printf ("%d %lld\n", idx, f (x, idx, y));
}
return 0;
}