正文
增加 19 行删除 19 行
77 行未改动7879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314477 行未改动int hd=1, tl, j=1, tr[5*MN];int v[MN], s[MN], e[MN], f[MN];struct dat {int fir, sec, t,c, g;dat() {} // 存各种信息判重,实际上有更优美的写法dat(int x,int y,int tt,int cc,int gg){ fir=x, sec=y, t=tt, c=cc, g=gg; }};typedef const dat cd;priority_queue<dat> q;// 小根堆bool operator<(cd x,cd y) { return x.fir>y.fir; }void upt(int p,int s,int e,int x,int v) { // 单点修if(s==e) { tr[p]=v; return; }int mid=(s+e)>>1;if(x<=mid) upt(ls(p),s,mid,x,v);else upt(rs(p),mid+1,e,x,v);tr[p]=min(tr[ls(p)],tr[rs(p)]);}int qur(int p,int s,int e,int l,int r) { // 区间查 minif(l==r&&l==0) return inf;if(l<=s&&e<=r) return tr[p];int mid=(s+e)>>1, res=inf;if(l<=mid) res=qur(ls(p),s,mid,l,r);if(r>mid) res=min(res,qur(rs(p),mid+1,e,l,r));return res;}bool check() { return hd<=q.top().sec&&q.top().sec<=tl&&s[q.top().sec]==q.top().t&&e[q.top().sec]==q.top().c&&v[q.top().sec]==q.top().g; } // 合法(?signed main() {ios::sync_with_stdio(0);cin.tie(0);memset(tr,0x3f,sizeof(tr));cin >> n >> len;for(int i=1; i<=n; ++i) {cin >> h[i] >> w[i];w[i]+=w[i-1]; // 前缀和,好判区间合法while(w[i]-w[j-1]>len) {j++, s[hd]++;if(s[hd]>e[hd]) hd++;} // W 的限制if(i!=1&&v[tl]<h[i]) { // 有可能改|不改边以前的int l=hd, r=tl;while(l<r) {int mid=(l+r)>>1;if(v[mid]<h[i]) r=mid;else l=mid+1;} // 二分出 Hi 可改区间修改tl=r, e[tl]=i, v[tl]=h[i];}else v[++tl]=h[i], s[tl]=e[tl]=i; // 新加区间q.push(dat(v[hd]+qur(1,1,n,s[hd]-1,e[hd]-1),hd,s[hd],e[hd],v[hd])); // 塞小q.push(dat(v[tl]+qur(1,1,n,s[tl]-1,e[tl]-1),tl,s[tl],e[tl],v[tl])); // 根堆while(q.size()&&!check()) q.pop(); // 被 W 砍掉的不要if(check()) f[i]=q.top().fir; // 算答案f[i]=min(f[i],f[i-1]+h[i]); // 上一个捏if(w[i]<=len) f[i]=min(v[hd],f[i]); // 从 0 开始upt(1,1,n,i,f[i]); // 加入 f[i]q.push(dat(v[tl]+qur(1,1,n,s[tl]-1,e[tl]-1),tl,s[tl],e[tl],v[tl]));}cout << f[n]; // 恭喜您写完了(喜return 0;}```77 行未改动7879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314477 行未改动int hd=1, tl, j=1, tr[5*MN];int v[MN], s[MN], e[MN], f[MN];struct dat {int fir, sec, t,c, g;dat() {} // 存各种信息判重,实际上有更优美的写法dat(int x,int y,int tt,int cc,int gg){ fir=x, sec=y, t=tt, c=cc, g=gg; }};typedef const dat cd;priority_queue<dat> q;// 小根堆bool operator<(cd x,cd y) { return x.fir>y.fir; }void upt(int p,int s,int e,int x,int v) { // 单点修if(s==e) { tr[p]=v; return; }int mid=(s+e)>>1;if(x<=mid) upt(ls(p),s,mid,x,v);else upt(rs(p),mid+1,e,x,v);tr[p]=min(tr[ls(p)],tr[rs(p)]);}int qur(int p,int s,int e,int l,int r) { // 区间查 minif(l==r&&l==0) return inf;if(l<=s&&e<=r) return tr[p];int mid=(s+e)>>1, res=inf;if(l<=mid) res=qur(ls(p),s,mid,l,r);if(r>mid) res=min(res,qur(rs(p),mid+1,e,l,r));return res;}bool check() { return hd<=q.top().sec&&q.top().sec<=tl&&s[q.top().sec]==q.top().t&&e[q.top().sec]==q.top().c&&v[q.top().sec]==q.top().g; } // 合法(?signed main() {ios::sync_with_stdio(0);cin.tie(0);memset(tr,0x3f,sizeof(tr));cin >> n >> len;for(int i=1; i<=n; ++i) {cin >> h[i] >> w[i];w[i]+=w[i-1]; // 前缀和,好判区间合法while(w[i]-w[j-1]>len) {j++, s[hd]++;if(s[hd]>e[hd]) hd++;} // W 的限制if(i!=1&&v[tl]<h[i]) { // 有可能改|不改边以前的int l=hd, r=tl;while(l<r) {int mid=(l+r)>>1;if(v[mid]<h[i]) r=mid;else l=mid+1;} // 二分出 Hi 可改区间修改tl=r, e[tl]=i, v[tl]=h[i];}else v[++tl]=h[i], s[tl]=e[tl]=i; // 新加区间q.push(dat(v[hd]+qur(1,1,n,s[hd]-1,e[hd]-1),hd,s[hd],e[hd],v[hd])); // 塞小q.push(dat(v[tl]+qur(1,1,n,s[tl]-1,e[tl]-1),tl,s[tl],e[tl],v[tl])); // 根堆while(q.size()&&!check()) q.pop(); // 被 W 砍掉的不要if(check()) f[i]=q.top().fir; // 算答案f[i]=min(f[i],f[i-1]+h[i]); // 上一个捏if(w[i]<=len) f[i]=min(v[hd],f[i]); // 从 0 开始upt(1,1,n,i,f[i]); // 加入 f[i]q.push(dat(v[tl]+qur(1,1,n,s[tl]-1,e[tl]-1),tl,s[tl],e[tl],v[tl]));}cout << f[n]; // 恭喜您写完了(喜return 0;}```