对于样例,在i = 3时找到的j就开始错了
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 3e5 + 20;
ll n, s, C[N], T[N], f[N];
ll h = 1, t = 1;
struct node{
ll x, y, id;
}q[N];
double slope(double x, double y) {
return y / (x == 0 ? 1e-9 : x);
}
int main (){
scanf("%lld%lld", &n, &s);
for(int i = 1; i <= n; i++) {
ll t, c;
scanf("%lld%lld", &t, &c);
T[i] = T[i-1] + t;
C[i] = C[i-1] + c;
}
q[1]= (node){0, 0, 0};
for(int i = 1; i <= n; i++) {
ll l = 1, r = t;
while(l < r) {
ll mid = (l + r ) >> 1;
if(mid == t) {
l = t;
break;
}
if(slope(1.0 * q[mid + 1].x - q[mid].x, 1.0 * q[mid + 1].y - q[mid].y) <= 1.0 * (T[i] - s)) l = mid + 1;
else r = mid;
}
if(l > t) l = t;
ll j = q[l].id;
f[i] = f[j] + T[i] * (C[i] - C[j]) + s * (C[n] - C[j]);
ll x = C[i], y = f[i];
while(h < t && slope(1.0 * q[t].x - q[t-1].x, 1.0 * q[t].y - q[t-1].y) >= slope(1.0 * x - q[t].x, 1.0 * y - q[t].y)) t--;
q[++t] = (node){x, y, i};
cout << j << endl;
}
printf("%lld", f[n]);
return 0;
}