Pagini recente » Cod sursa (job #797733) | Cod sursa (job #2428082) | Cod sursa (job #2043690) | Cod sursa (job #1361302) | Cod sursa (job #421052)
Cod sursa(job #421052)
#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;
ll movesSt = costCubSt[poz] - (poz - x) * nrCubSt[x - 1] - costCubSt[x];
ll movesDr = costCubDr[poz] - (y - poz) * nrCubDr[y + 1] - costCubDr[y];
ll ad = movesSt + movesDr;
if ((med == fr || ad + nrCubDr[med] - nrCubDr[y + 1] - nrCubSt[med - 1] + nrCubSt[x - 1] >= ad) && (med == ls || ad + nrCubSt[med] - nrCubSt[x - 1] - nrCubDr[med + 1] + nrCubDr[y + 1] >= ad))
poz = med;
else if (med != fr && ad + vctNrCub[med] - vctNrCub[med - 1] <= ad)
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;
}