版本比较
题解 P4501 【[ZJOI2018]胖】
正文
增加 12 行删除 12 行
16 行未改动171819202121222324252627282930313132333434353637384 行未改动4344454647474849505152525354555656575859606162636465656667686970717273747575767778798081828384858586878889909192939495959697989910010110210310410510510610710810911011111211311411511511611711811916 行未改动16 行未改动#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>
struct Data{ int p; long long l; bool operator <(const Data &_)const{ return p<_.p; }}a[200010];int n, m, K;long long dis[200010];
namespace ST{ long long st1[200010][18], st2[200010][18];
void init(){ std::sort(a+1, a+K+1); for(int i=1; i<=K; i++){ st1[i][0]=a[i].l-dis[a[i].p];4 行未改动 st1[i][j]=std::min(st1[i][j-1], st1[i+(1<<(j-1))][j-1]); st2[i][j]=std::min(st2[i][j-1], st2[i+(1<<(j-1))][j-1]); } }
int findl(int x){ Data _; _.p=x; return std::lower_bound(a+1, a+K+1, _)-a; }
int findr(int x){ Data _; _.p=x; return std::upper_bound(a+1, a+K+1, _)-a-1; } } long long query1(int l, int r){ if(l>r) std::swap(l, r); l=std::max(1, l), r=std::min(r, n); l=findl(l), r=findr(r); if(l>r) return 0x3f3f3f3f3f3f3f3f; int lim=log2(r-l+1); return std::min(st1[l][lim], st1[r-(1<<lim)+1][lim]); }
long long query2(int l, int r){ if(l>r) std::swap(l, r); l=std::max(1, l), r=std::min(r, n); l=findl(l), r=findr(r); if(l>r) return 0x3f3f3f3f3f3f3f3f; int lim=log2(r-l+1); return std::min(st2[l][lim], st2[r-(1<<lim)+1][lim]); }}
bool check1(int p, int x){ if(x==p) return true; long long qwq1=ST::query1(2*x-p+1, x)+dis[x]; long long qwq2=ST::query2(x, p-1)-dis[x]; long long mst=ST::query2(p, p)-dis[x]; if(qwq1<=mst || qwq2<=mst) return false; if(2*x-p>=1) return ST::query1(2*x-p, 2*x-p)+dis[x]>mst; return true;}
bool check2(int p, int x){ if(x==p) return true; long long qwq1=ST::query1(p+1, x)+dis[x]; long long qwq2=ST::query2(x, 2*x-p-1)-dis[x]; long long mst=ST::query1(p, p)+dis[x]; if(qwq1<=mst || qwq2<=mst) return false; if(2*x-p<=n) return ST::query2(2*x-p, 2*x-p)-dis[x]>=mst; return true;}
int solve1(int p){ int l=1, r=p, ans=p; while(l<=r){ int mid=(l+r)/2; if(check1(p, mid)) r=mid-1, ans=mid; else l=mid+1; } return ans;}
int solve2(int p){ int l=p, r=n, ans=p; while(l<=r){ int mid=(l+r)/2; if(check2(p, mid)) l=mid+1, ans=mid; else r=mid-1; } return ans;}
int main(){ scanf("%d%d", &n, &m); for(int i=2; i<=n; i++){ scanf("%lld", &dis[i]);16 行未改动