Pagini recente » Cod sursa (job #521446) | Cod sursa (job #1335406) | Cod sursa (job #2565876) | Cod sursa (job #2455477) | Cod sursa (job #980965)
Cod sursa(job #980965)
#include <fstream>
using namespace std;
const int MAX_N = 250002;
const long long int INF = 999999999999999999;
int N, M;
int v[MAX_N];
long long int st[MAX_N], dr[MAX_N], a[MAX_N], b[MAX_N];
int main() {
ifstream f("cuburi2.in");
ofstream g("cuburi2.out");
f >> N >> M;
for(int i = 1; i <= N; ++i) {
f >> v[i];
a[i] = v[i] + a[i-1], st[i] = st[i-1] + a[i-1];
}
for(int i = N; i; --i)
b[i] = v[i] + b[i+1], dr[i] = dr[i+1] + b[i+1];
while(M--) {
int x, y, ind, l, r, m;
f >> x >> y;
l = x, r = y;
long long int BestCost, Cost1, Cost2;
while(l <= r) {
m = (l+r)/2;
if(m == y) {
ind = y;
break;
}
Cost1 = st[m] - st[x] - a[x-1]*(m-x) + dr[m] - dr[y] - b[y+1]*(y-m), ind = m;
Cost2 = st[m+1] - st[x] - a[x-1]*(m+1-x) + dr[m+1] - dr[y] - b[y+1]*(y-m+1);
if(Cost1 < Cost2)
ind = m, r = m-1;
else l = m+1;
}
BestCost = st[ind] - st[x] - a[x-1]*(ind-x) + dr[ind] - dr[y] - b[y+1]*(y-ind);
g << ind << " " << BestCost << "\n";
}
f.close();
g.close();
return 0;
}