为什么线段树优化过不去???

P1190 [NOIP2010 普及组] 接水问题

你更新的时候应该是从1到m而不是n,代码改完后是这样的: ```cpp #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<functional> using namespace std; int n,m,wi[1000001],tree[100001*4],maxx=-1; bool cmp(const int &x,const int &y) { return x > y; } void build(int node,int l,int r) { if(l == r) { tree[node]=wi[l]; maxx=max(maxx,tree[node]); return; } int m=(l+r)/2; build(node*2,l,m); build(node*2+1,m+1,r); tree[node]=min(tree[node*2],tree[node*2+1]); } void update(int node,int l,int r,int w) { if(l == r) { tree[node] += w; maxx=max(maxx,tree[node]); // cout<<node<<' '<<tree[node]<<' '<<maxx<<endl;检查更新是否正确 return; } int m=(l+r)/2; if(tree[node] == tree[node*2]) update(node*2,l,m,w); else update(node*2+1,m+1,r,w); tree[node]=min(tree[node*2],tree[node*2+1]); } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d",&wi[i]); } // sort(wi+1,wi+n+1,cmp); // for(int i=1;i<=n;i++) printf("%d ",wi[i]); maxx=wi[1]; if( n <= m) cout<<maxx; else { build(1,1,m); // for(int i=1;i<=5;i++) printf("%d ",tree[i]);检查建树是否正确 for(int i=m+1;i<=n;i++) update(1,1,m,wi[i]); cout<<maxx; } return 0; } ```
by Cet6_427 @ 2017-06-13 06:56:01


@[JRicardo](/space/show?uid=17318) 膜拜大神
by 林志杰 @ 2017-06-13 07:16:16


你用我的号 自己和自己说话 **好有趣**
by Cet6_427 @ 2017-06-13 07:54:33


呵呵
by X_XJian @ 2017-07-14 22:02:07


|