【NOIp2019模拟】—荷马史诗(哈夫曼树)

传送门


NoiNoi那道的加强版
实际也比较简单

考虑每次排序后加入的就是一段连续的区间

提前排序之后维护一下当前新出现的节点的权值
每次暴力比一下新出现的这次会不会加进去

复杂度是调和级数的O(nlogn)O(nlogn)

#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
	static char ibuf[RLEN],*ib,*ob;
	(ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
	return (ob==ib)?EOF:*ib++;
}
#define gc getchar
inline int read(){
	char ch=gc();
	int res=0,f=1;
	while(!isdigit(ch))f^=ch=='-',ch=gc();
	while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
	return f?res:-res;
}
#define ll long long
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define cs const
const int mod=998244353,g=3;
inline int add(int a,int b){return a+b>=mod?a+b-mod:a+b;}
inline void Add(int &a,int b){a=add(a,b);}
inline int dec(int a,int b){return a>=b?a-b:a-b+mod;}
inline void Dec(int &a,int b){a=dec(a,b);}
inline int mul(int a,int b){return 1ll*a*b>=mod?1ll*a*b%mod:a*b;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,a=mul(a,a))(b&1)?(res=mul(res,a)):0;return res;}
inline void chemx(int &a,int b){a<b?a=b:0;}
inline void chemn(int &a,int b){a>b?a=b:0;}
cs int N=2000005;
int n,a[N];
ll s[N],ans;
queue<ll> q;
inline ll solve(int k){
	ll res=0;
	int st=n+1-(k-1-(n-1)%(k-1))%(k-1);
	while(st-q.size()<2*n){
		int des=st+k-1;ll val=0;
		while(q.size()&&(des>2*n||q.front()<a[des]))des--,val+=q.front(),q.pop();
		val+=s[des]-s[st-1];
		st=des+1,q.push(val),res+=val;
	}
	while(q.size())q.pop();
	return res;
}
int main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	sort(a+1,a+n+n+1);
	for(int i=n+1;i<=2*n;i++)s[i]=s[i-1]+a[i];
	for(int i=2;i<=n;i++)ans^=solve(i);
	cout<<ans;	
}
posted @ 2019-08-21 18:32  Stargazer_cykoi  阅读(142)  评论(0编辑  收藏  举报