2019-Acyclic_SD/paste1

从 Trac 迁移的文章

这是从旧校内 Wiki 迁移的文章,可能存在一些样式问题,您可以向 memset0 反馈。

原文章内容如下:

{{{
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int cas,n;
ll ans;
const int K=5;
map<int,ll>T[K];
void ins(int o,int x,ll p){
    if(o>=K)return ;
    T[o][x]+=p;
    ans+=p;
    while(p){
        map<int,ll>:: iterator it=T[o].lower_bound(x+1);
        if(it==T[o].end())return;
        ll t=min(p,it->second);
        ans-=t;
        p-=t;
        ins(o+1,it->first,t);
        if(t==it->second)T[o].erase(it);
            else it->second-=t;
    }
}
int main(){
    scanf("%d",&cas);
    while(cas--){
        scanf("%d",&n);
        ans=0;
        for(int i=0;i<K;i++)T[i].clear();
        for(int i=1;i<=n;i++){
            int x;scanf("%d",&x);
            ins(0,x,x);
            printf("%lld ",ans);
        }
        printf("\n");
    }
    return 0;
}

/*
1
8 
8 7 6 5 1 3 2 4 
*/
}}}
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int cas,n;
ll ans;
const int K=5;
map<int,ll>T[K];
void ins(int o,int x,ll p){
    if(o>=K)return ;
    T[o][x]+=p;
    ans+=p;
    while(p){
        map<int,ll>:: iterator it=T[o].lower_bound(x+1);
        if(it==T[o].end())return;
        ll t=min(p,it->second);
        ans-=t;
        p-=t;
        ins(o+1,it->first,t);
        if(t==it->second)T[o].erase(it);
            else it->second-=t;
    }
}
int main(){
    scanf("%d",&cas);
    while(cas--){
        scanf("%d",&n);
        ans=0;
        for(int i=0;i<K;i++)T[i].clear();
        for(int i=1;i<=n;i++){
            int x;scanf("%d",&x);
            ins(0,x,x);
            printf("%lld ",ans);
        }
        printf("\n");
    }
    return 0;
}
/*
1
8 
8 7 6 5 1 3 2 4 
*/