Pagini recente » Cod sursa (job #2538080) | Cod sursa (job #507098) | Cod sursa (job #612071) | Cod sursa (job #1899920) | Cod sursa (job #770245)
Cod sursa(job #770245)
#include <iostream>
#include <fstream>
using namespace std;
#define nmax 250005
#define inf (1LL<<61)
ifstream f("cuburi2.in");
ofstream g("cuburi2.out");
int n, m, high[nmax];
typedef struct{
long long cost;
int cnt;
}camp;
camp st[nmax], dr[nmax];
void citeste(){
f >> n >> m;
for(int i=1; i<=n; i++) f >> high[i];
}
void rezolva(){
//st[i].cost = costul de a muta toate cuburile din partea stanga a cubului i pe cubul i
//st[i].cnt = numarul de cuburi pana la cubul i(inclusiv)
//dr[i].cost/dr[i].cnt la fel...;
st[1].cost = 0;
st[1].cnt = high[1];
for(int i=2; i<=n; i++){
st[i].cost = st[i-1].cost + st[i-1].cnt;
st[i].cnt = st[i-1].cnt + high[i];
}
dr[n].cost = 0;
dr[n].cnt = high[n];
for(int i=n-1; i>=1; i--){
dr[i].cost = dr[i+1].cost + dr[i+1].cnt;
dr[i].cnt = dr[i+1].cnt + high[i];
}
//pentru un interval [x,y] ma intereseaza care element are MIN(st[i].cost - st[x].cost + dr[i].cost - dr[y].cost), cu i = x,y;
for(int i=1; i<=m; i++){
int x,y;
f >> x >> y;
long long rez = inf;
int poz = 0;
for(int i=x; i<=y; i++){
int _st = st[i].cost - st[x].cost - ((i-x)*st[x-1].cnt);
int _dr = dr[i].cost - dr[y].cost - ((y-i)*dr[y+1].cnt);
if (rez > _st+_dr){
rez = _st + _dr;
poz = i;
}
}
g << poz << " " << rez << "\n";
}
}
int main(){
citeste();
rezolva();
f.close();
g.close();
return 0;
}