P1631 序列合并
*题解
*代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> #define ri register int #define ll long long #define For(i,l,r) for(ri i=l;i<=r;i++) #define Dfor(i,r,l) for(ri i=r;i>=l;i--) using namespace std; const int M=100005; int n,sum,a[M],b[M],from[M],step[M],tmp; ll heap[M]; inline ll read(){ ll f=1,sum=0; char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch)){sum=(sum<<1)+(sum<<3)+(ch^48);ch=getchar();} return f*sum; } inline void write(ll x){ if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); } inline void swap(int a,int b){ tmp=heap[a];heap[a]=heap[b];heap[b]=tmp; tmp=from[a];from[a]=from[b];from[b]=tmp; } int main(){ n=read(); For(i,1,n){a[i]=read();} For(i,1,n){b[i]=read();} For(i,1,n){heap[i]=a[i]+b[1];step[i]++;from[i]=i;} while(sum<n){ printf("%lld ",heap[1]); int t=from[1]; step[t]++; heap[1]=a[t]+b[step[t]]; int x=1,s; while(x<<1<=n){ s=x<<1; if(heap[s]>heap[s+1]&&s+1<=n) s++; if(heap[x]>heap[s]){ swap(x,s);x=s; } else break; } sum++; } return 0; }