Pagini recente » Cod sursa (job #1993543) | Diferente pentru problema/zigzag intre reviziile 11 si 16 | Cod sursa (job #2104333) | Cod sursa (job #1154989) | Cod sursa (job #3343350)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
#if 1
#define int ll
#define uint ull
#endif
/**
* Problem: Cuburi2
* URL: https://www.infoarena.ro/problema/cuburi2
* TL: 375 ms
* ML: 20 MB
*
* Good Luck!
*/
void preinit() {
}
void tc() {
int n, q;
cin >> n >> q;
vector<ll> a(n + 1), ps(n + 1), psi(n + 1);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
ps[i] = ps[i - 1] + a[i];
psi[i] = psi[i - 1] + 1LL * i * a[i];
}
auto sw = [&](int l, int r) -> ll {
if (l > r) return 0;
return ps[r] - ps[l - 1];
};
auto siw = [&](int l, int r) -> ll {
if (l > r) return 0;
return psi[r] - psi[l - 1];
};
auto cost = [&](int l, int r, int p) -> ll {
ll lw = sw(l, p - 1);
ll liw = siw(l, p - 1);
ll rw = sw(p + 1, r);
ll riw = siw(p + 1, r);
__int128 left = (__int128)p * lw - liw;
__int128 right = riw - (__int128)p * rw;
return (ll)(left + right);
};
while (q--) {
int l, r;
cin >> l >> r;
if (l > r) swap(l, r);
ll tot = sw(l, r);
if (tot == 0) {
cout << l << " " << 0 << "\n";
continue;
}
ll k = tot / 2 + 1;
ll t = ps[l - 1] + k;
int p = int(lower_bound(ps.begin() + l, ps.begin() + r + 1, t) -
ps.begin());
int bp = p;
ll bc = cost(l, r, p);
for (int c : {p - 1, p + 1}) {
if (c >= l && c <= r) {
ll x = cost(l, r, c);
if (x < bc) {
bc = x;
bp = c;
}
}
}
cout << bp << " " << bc << "\n";
}
}
#define MTC 0
#define FIO 1
#define FN "cuburi2"
signed main() {
#if FIO == 1 && !defined(LOCAL)
(void)freopen(FN ".in", "r", stdin);
(void)freopen(FN ".out", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
preinit();
#if MTC == 1
signed tt;
cin >> tt;
while (tt--) tc();
#else
tc();
#endif
return 0;
}