QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#424331#2070. Heavy StonesNt_YesterWA 126ms262064kbC++144.0kb2024-05-29 07:23:082024-05-29 07:23:08

Judging History

你现在查看的是最新测评结果

  • [2024-05-29 07:23:08]
  • 评测
  • 测评结果:WA
  • 用时:126ms
  • 内存:262064kb
  • [2024-05-29 07:23:08]
  • 提交

answer

#include <iostream>

#include <cstdio>

#include <vector>

#include <map>



#define N 200010

#define int long long

#define INF 0x3f3f3f3f



using namespace std;

int n,a[N];



struct P {

    int val,sum,l,r,siz;

    

    P (int _val=0,int _sum=0,int _l=INF,int _r=0) { val=_val,sum=_sum,l=_l,r=_r,siz=max(_r-_l+1,0ll); }

};

vector<P> stk;

vector<pair<P,int> > pre,suf;



P operator+(P t1,P t2) { return P(t1.val+t2.val+t1.sum*t2.siz,t1.sum+t2.sum,min(t1.l,t2.l),max(t1.r,t2.r)); }

bool operator==(P t1,P t2) { return t1.val==t2.val and t1.l==t2.l and t1.r==t2.r; }

bool operator<(P t1,P t2) {

    if (t1.sum*t2.siz!=t2.sum*t1.siz) return t1.sum*t2.siz<t2.sum*t1.siz;

    return t1.l<t2.l;

}

bool operator<=(P t1,P t2) { return t1<t2 or t1==t2; }

bool operator>(P t1,P t2) { return t2<t1; }

bool operator>=(P t1,P t2) { return t2<t1 or t2==t1; }



const double A=0.75;

int tcnt,rt,flatcnt,flata[N<<3];

struct Tree { P val,sum; int ls,rs,num,siz,sizv,siznd; }t[N<<3];



void upd(int x) {

    t[x].siz=t[t[x].ls].siz+t[t[x].rs].siz+t[x].num;

    t[x].sizv=t[t[x].ls].sizv+t[t[x].rs].sizv+1;

    t[x].siznd=t[t[x].ls].siznd+t[t[x].rs].siznd+(t[x].num>0);

    if (t[x].num>0) t[x].sum=(t[t[x].ls].sum+t[x].val)+t[t[x].rs].sum;

    else t[x].sum=t[t[x].ls].sum+t[t[x].rs].sum;

}



bool ifflat(int x) { return (t[x].num>0 and (t[x].siz*A<=(double)std::max(t[t[x].ls].siz,t[t[x].rs].siz) or (t[x].sizv*A>=(double)t[x].siznd))); }



void flatdfs(int x) {

    if (!x) return ;

    flatdfs(t[x].ls);

    if (t[x].num>0) flata[++flatcnt]=x;

    flatdfs(t[x].rs);

}



void flatrebuild(int &x,int l,int r) {

    if (l>r) return void(x=0);

    int mid=(l+r>>1);

    x=flata[mid];

    flatrebuild(t[x].ls,l,mid-1),flatrebuild(t[x].rs,mid+1,r);

    upd(x);

}



void flat(int &x) { flatcnt=0; flatdfs(x); flatrebuild(x,1,flatcnt); }



void ins(int &x,P p) {

    if (!x) {

        t[x=++tcnt]={p,p,0,0,1,1,1,1};

        upd(x); return;

    } else {

        if (t[x].val==p) t[x].num++;

        else if (p<t[x].val) ins(t[x].ls,p);

        else ins(t[x].rs,p);

        upd(x);

        if (ifflat(x)) flat(x);

    }

}



void del(int &x,P p) {

    if (!x) return;

    if (t[x].val==p) t[x].num--;

    else if (p<t[x].val) del(t[x].ls,p);

    else del(t[x].rs,p);

    upd(x);

    if (ifflat(x)) flat(x);

}



void dfs(int x) {

    if (!x) return;

    if (t[x].num>1) printf("!!!!!!!\n");

    dfs(t[x].ls); dfs(t[x].rs);

}



signed main() {

    // freopen("freight.in","r",stdin);

    // freopen("freight.out","w",stdout);



    scanf("%lld",&n);

    for (int i=1;i<=n;i++) scanf("%lld",a+i);

    for (int i=1;i<=n;i++) {

        P x=P(a[i],a[i],i,i);

        while (!stk.empty() and stk.back()<=x) {

            P y=stk.back(); stk.pop_back();

            pre.push_back({y,-1}); x=x+y;

        }

        stk.push_back(x); pre.push_back({x,1});



    }

    stk.clear();

    for (int i=n;i;i--) {

        P x=P(a[i],a[i],i,i);

        while (!stk.empty() and stk.back()<=x) {

            P y=stk.back(); stk.pop_back();

            suf.push_back({y,1}); x=x+y;

        }

        stk.push_back(x); suf.push_back({x,-1});

    }

    for (auto i:stk) ins(rt,i);

    int precnt=-1,sufcnt=suf.size()-1;

    for (int i=1;i<=n;i++) {

        while (sufcnt>=0 and (suf[sufcnt].second!=-1 or suf[sufcnt].first.l!=i+1)) {

            if (suf[sufcnt].second==-1) del(rt,suf[sufcnt].first);

            else ins(rt,suf[sufcnt].first);

            --sufcnt;

        }

        printf("%lld ",t[rt].sum.val+a[i]*(n-1));

        while (precnt==-1 or pre[precnt].first.r!=i) {

            ++precnt;

            if (pre[precnt].second==-1) del(rt,pre[precnt].first);

            else ins(rt,pre[precnt].first);

        }

        // dfs(rt);

    }

    printf("\n");

    return 0;

}

详细

Test #1:

score: 100
Accepted
time: 8ms
memory: 203852kb

input:

5
2 1 3 5 4

output:

35 35 36 43 49 

result:

ok single line: '35 35 36 43 49 '

Test #2:

score: 0
Accepted
time: 4ms
memory: 203892kb

input:

10
18 37 81 6 58 99 87 34 75 9

output:

2637 2637 2657 2657 2695 2949 2995 2905 2880 2880 

result:

ok single line: '2637 2637 2657 2657 2695 2949 2995 2905 2880 2880 '

Test #3:

score: -100
Wrong Answer
time: 126ms
memory: 262064kb

input:

200000
479735 430060 863649 878892 889364 241972 927862 32858 714406 674598 88114 438434 167733 457299 951288 510096 635193 504974 649221 617914 223711 812277 364169 746142 836563 825312 994240 198961 567906 905208 509572 744710 891294 928176 833980 985385 858440 50835 86130 324862 796245 935992 383...

output:

9992833979971052 9992833979971052 9992833981215086 29959859213292234 29959505818052926 29959279633848866 29959142641515942 29958853817208176 29958717693670780 29958725644191706 29958842784342444 29958772811725940 29958826878104186 29958769044343754 29958670384994336 29958758516563358 299587335385451...

result:

wrong answer 1st lines differ - expected: '9992833979971052 9992833979971...4203005830427 10004203006781811', found: '9992833979971052 9992833979971...203007453895 10004203006781811 '