正文
增加 9 行删除 9 行
14 行未改动151617181920212223242526272829303132339 行未改动4344454647484950515253545556575859606162636465666714 行未改动#include<cstring>using namespace std;int n,k;long long num[500001],ans;//记录权利int left[500001],right[500001];//记录左右坑bool book[500001];//记录遍历过与否struct node{int id;long long w;//w是权利 id是序号bool operator <(node b) const{return w < b.w;}} t;//t用来过渡priority_queue<node>q;int main(){scanf("%d%d",&n,&k);9 行未改动left[n+1]=n;right[0]=1;//补全r,l数组for(int i=1;i<=k;i++){while(book[q.top().id])//如果遍历过则删除 ,注意!一定要用while删除,因为1不知道有几个遍历过。2可能有多次根顶遍历过。q.pop();t=q.top();//取出堆顶元素q.pop();if(t.w<0)//如果堆顶元素小于0则跳出break;ans+=t.w;int c=t.id;num[c]=num[left[c]]+num[right[c]]-num[c];t.w=num[c];//设置新节点,意为若不选x节点选择x节点左右两节点的权利增加量 ,即提供反悔选项。book[left[c]]=book[right[c]]=1;//删除左右的坑left[c]=left[left[c]];right[c]=right[right[c]];right[left[c]]=c;left[right[c]]=c;//更新左右坑的左右坑q.push(t);}printf("%lld",ans);return 0;}```14 行未改动151617181920212223242526272829303132339 行未改动4344454647484950515253545556575859606162636465666714 行未改动#include<cstring>using namespace std;int n,k;long long num[500001],ans;//记录权利int left[500001],right[500001];//记录左右坑bool book[500001];//记录遍历过与否struct node{int id;long long w;//w是权利 id是序号bool operator <(node b) const{return w < b.w;}} t;//t用来过渡priority_queue<node>q;int main(){scanf("%d%d",&n,&k);9 行未改动left[n+1]=n;right[0]=1;//补全r,l数组for(int i=1;i<=k;i++){while(book[q.top().id])//如果遍历过则删除 ,注意!一定要用while删除,因为1不知道有几个遍历过。2可能有多次根顶遍历过。q.pop();t=q.top();//取出堆顶元素q.pop();if(t.w<0)//如果堆顶元素小于0则跳出break;ans+=t.w;int c=t.id;num[c]=num[left[c]]+num[right[c]]-num[c];t.w=num[c];//设置新节点,意为若不选x节点选择x节点左右两节点的权利增加量 ,即提供反悔选项。book[left[c]]=book[right[c]]=1;//删除左右的坑left[c]=left[left[c]];right[c]=right[right[c]];right[left[c]]=c;left[right[c]]=c;//更新左右坑的左右坑q.push(t);}printf("%lld",ans);return 0;}```