块链

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int MAXL		=	135000;
const int BLOCKSIZE	=	800;
const int MAXBLOCK	=	2*MAXL/BLOCKSIZE*2+100;

int data[MAXBLOCK][BLOCKSIZE];
int count[MAXBLOCK],next[MAXBLOCK];

struct BlockList
{
    int freelist[MAXBLOCK];
    int freepos;
    int newnode(){
        return freelist[freepos++];
    }
    void delnode(int t){
        freelist[--freepos]=t;
    }
    void init()
    {
        for(int i=1; i<MAXBLOCK; ++i)freelist[i]=i;
        freepos=1;
        next[0]=-1;
        count[0]=0;
    }
    //block list operation
    void find(int &p,int &b){
        for(b=0; b!=-1 && p>count[b]; b=next[b])
            p -= count[b];
    }
    void maintainlist(int b){
        for(; b!=-1; b=next[b])
            for(int t=next[b]; t!=-1 && count[b] + count[t] <= BLOCKSIZE; t=next[b]){
                memcpy(data[b]+count[b], data[t], count[t]*sizeof(int));
                count[b]+=count[t];
                next[b]=next[t];
                delnode(t);
            }
    }
    void blockfill(int b,int str[],int n,int e){
        next[b]=e;
        count[b]=n;
        memcpy(data[b], str, n*sizeof(int));
    }
    void splite(int b,int p){
        if(b==-1 || count[b]==p)return;
        int t = newnode();
        blockfill(t, data[b]+p, count[b]-p, next[b]);
        count[b]= p;
        next[b] = t;
    }
    void insert(int p,int n,int str[]){
        int b,i,t;
        find(p,b);
        splite(b,p);
        for(i=0; i+BLOCKSIZE <= n; i+=BLOCKSIZE){
            t=newnode();
            blockfill(t, str+i, BLOCKSIZE, next[b]);
            next[b]=t;
            b=t;
        }
        if(n-i){
            t=newnode();
            blockfill(t, str+i, n-i, next[b]);
            next[b]=t;
        }
        maintainlist(b);
    }
    void erase(int p,int n){
        int b,e;
        find(p,b);
        splite(b,p);
        for(e=next[b]; e!=-1 && n > count[e]; e=next[e])n -=count[e];
        if(n){splite(e,n);e=next[e];}
        for(int t=next[b]; t!=e; t=next[b]){
            next[b]=next[t];
            delnode(t);
        }
        maintainlist(b);
    }
}blocklist;

int n,m;
int str[MAXL*2];
int getcount(){
	int sum=0;
	for(int b=0; b!=-1; b=next[b])
		sum+=count[b];
	return sum;
}
void get(){
	for(int b=0,i=0; b!=-1; b=next[b]){
		memcpy(str+i, data[b], count[b]*sizeof(int));
		i+=count[b];
	}
}

//uni-set operation
int nextfree[MAXL];

int root(int x){
	if(x>m || x == nextfree[x])return x;
	return nextfree[x]=root(nextfree[x]);
}

//main
void init(){
//	freopen("1009.in","r",stdin);
//	freopen("1009.out","w",stdout);
	scanf("%d%d",&n,&m);
	for(int i=m; i>=0 ; --i)nextfree[i]=i;
	blocklist.init();
	memset(str,0,sizeof(str));
	blocklist.insert(0, m, str);
}
void solve(){
	int s,t,x,i;
	for(s=1; s<=n; ++s){
		scanf("%d",&t);
		x = root(t);
		if(x <= m){
			blocklist.erase(x-1,1);
			++nextfree[x];
		}
		str[0]=s;
		blocklist.insert(t-1, 1, str);
	}
	n = getcount();
	get();
	while(n && str[n-1]==0)--n;
	printf("%d\n",n);
	printf("%d",str[0]);
	for(i=1; i<n; ++i)printf(" %d",str[i]);
	printf("\n");
}
int main(){
	init();
	solve();
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值