QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#720372#8050. Random Permutationqzez#AC ✓1233ms437336kbC++143.6kb2024-11-07 12:14:112024-11-07 12:14:12

Judging History

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

  • [2024-11-07 12:14:12]
  • 评测
  • 测评结果:AC
  • 用时:1233ms
  • 内存:437336kb
  • [2024-11-07 12:14:11]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#ifdef DEBUG
template<class T>
ostream& operator << (ostream& out,vector<T>a){
    out<<'[';
    for(auto x:a)out<<x<<',';
    return out<<']';
}
template<class T>
auto ary(T *a,int l,int r){
    return vector<T>{a+l,a+1+r};
}
template<class T>
void debug(T x){
    cerr<<x<<endl;
}
template<class T,class...S>
void debug(T x,S...y){
    cerr<<x<<' ',debug(y...);
}
#else
#define debug(...) void()
#endif
const int N=3e5+10,V=50;
int n,a[N],b[N];
int mn[N<<2],mx[N<<2],laz[N<<2];
int tn[N<<2][V],tx[N<<2][V];
void pushup(int rt){
    mn[rt]=min(mn[rt<<1],mn[rt<<1|1]);
    mx[rt]=max(mx[rt<<1],mx[rt<<1|1]);
    memset(tn[rt],0,sizeof tn[rt]);
    memset(tx[rt],0,sizeof tx[rt]);
    for(int v:{rt<<1,rt<<1|1}){
        for(int i=0;i+mn[v]-mn[rt]<V;i++){
            tn[rt][i+mn[v]-mn[rt]]+=tn[v][i];
        }
    }
    for(int v:{rt<<1,rt<<1|1}){
        for(int i=0;mx[rt]-(mx[v]-i)<V;i++){
            tx[rt][mx[rt]-(mx[v]-i)]+=tx[v][i];
        }
    }
    mn[rt]+=laz[rt],mx[rt]+=laz[rt];
}
void build(int l=0,int r=n,int rt=1){
    if(l==r){
        mn[rt]=mx[rt]=l;
        tx[rt][0]=tn[rt][0]=1;
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    pushup(rt);
}
void update(int L,int R,int x,int l=0,int r=n,int rt=1){
    if(L<=l&&r<=R){
        mx[rt]+=x,mn[rt]+=x,laz[rt]+=x;
        return;
    }
    int mid=(l+r)>>1;
    if(L<=mid)update(L,R,x,l,mid,rt<<1);
    if(mid<R)update(L,R,x,mid+1,r,rt<<1|1);
    pushup(rt);
}
void query(int *t,int L,int R,int vl,int vr,int l=0,int r=n,int rt=1,int s=0){
    if(R<l||r<L||mx[rt]+s<vl||vr<mn[rt]+s)return;
    if(L<=l&&r<=R&&max(vl,mn[rt]+s+V)>min(vr,mx[rt]+s-V)){
        int i;
        for(i=max(mn[rt]+s,vl);i<=min(mn[rt]+s+V-1,vr);i++){
            t[i]+=tn[rt][i-mn[rt]-s];
        }
        for(i=max(i,mx[rt]+s-V+1);i<=min(vr,mx[rt]+s);i++){
            t[i]+=tx[rt][mx[rt]+s-i];
        }
        return;
    }
    int mid=(l+r)>>1;
    s+=laz[rt];
    query(t,L,R,vl,vr,l,mid,rt<<1,s);
    query(t,L,R,vl,vr,mid+1,r,rt<<1|1,s);
}
void range(int L,int R,int &vl,int &vr,int l=0,int r=n,int rt=1,int s=0){
    if(R<l||r<L)return;
    if(L<=l&&r<=R){
        vl=min(vl,mn[rt]+s);
        vr=max(vr,mx[rt]+s);
        return;
    }
    int mid=(l+r)>>1;
    s+=laz[rt];
    range(L,R,vl,vr,l,mid,rt<<1,s);
    range(L,R,vl,vr,mid+1,r,rt<<1|1,s);
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]),b[a[i]]=i;
    build();
    ll cur=n*(n+1ll)/2,ans=cur*n;
    for(int w=n;w>1;w--){
        int i=b[w];
        int l1=INT_MAX,r1=INT_MIN;
        range(0,i-1,l1,r1);
        int l2=INT_MAX,r2=INT_MIN;
        range(i,n,l2,r2);
        int l=max(l1,l2-1),r=min(r1+1,r2);
        if(l<=r){
            static int tl[N+N],tr[N+N];
            // static int s[N];
            // for(int i=1;i<=n;i++){
            //     s[i]=s[i-1]+(a[i]>w?-1:1);
            // }
            // debug(ary(s,1,n));
            query(tl+N,0,i-1,l,r);
            query(tr+N,i,n,l,r);
            // debug(i,l,r,ary(tl,l,r),ary(tr,l,r));
            for(int i=l;i<=r;i++){
                cur-=1ll*tl[N+i]*tr[N+i];
            }
            for(int i=l;i<r;i++){
                cur-=1ll*tl[N+i]*tr[N+i+1];
            }
            for(int i=l;i<=r;i++){
                tl[N+i]=tr[N+i]=0;
            }
            update(i,n,-2);
        }
        ans-=cur;
        // debug(w,cur);
    }
    printf("%lld\n",ans);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 16236kb

input:

4
1 4 2 3

output:

22

result:

ok 1 number(s): "22"

Test #2:

score: 0
Accepted
time: 347ms
memory: 125332kb

input:

100000
56449 21738 74917 44834 36187 96576 37204 28451 3444 13029 66039 8955 51445 30706 27229 37159 66052 16691 70389 29935 44984 3648 75082 73600 76621 28345 5298 37940 49412 85260 92029 18185 84398 10233 79227 98312 96649 30680 65206 38879 75397 26951 11294 58085 37297 97167 59252 44104 4058 3796...

output:

250202478701074

result:

ok 1 number(s): "250202478701074"

Test #3:

score: 0
Accepted
time: 781ms
memory: 229148kb

input:

200000
70900 189146 39056 135530 191967 133789 108558 81993 132081 113826 54456 127761 27836 64897 87159 105191 109446 81230 75525 90396 75756 50200 43091 151358 100791 193998 157161 119352 176873 120724 134471 155040 138534 182263 161855 4577 124893 199710 60764 146460 75314 43696 129155 64816 1390...

output:

1998651699409551

result:

ok 1 number(s): "1998651699409551"

Test #4:

score: 0
Accepted
time: 1202ms
memory: 436536kb

input:

300000
291335 150122 292004 129126 26666 242892 124454 146198 257953 17400 245611 108131 68345 266915 44851 146376 110324 211968 251982 127849 152791 204625 213884 144643 137727 113838 115018 225220 169217 151 284989 29747 37230 110534 124886 224954 253706 175150 103605 289983 165895 113248 32809 28...

output:

6755672893227829

result:

ok 1 number(s): "6755672893227829"

Test #5:

score: 0
Accepted
time: 1210ms
memory: 437336kb

input:

300000
135644 13106 249143 129835 242825 74615 166757 71595 226579 131061 189926 40413 77323 17706 196799 169583 219200 209681 151434 49233 129178 224147 186722 191982 220492 56613 175968 271850 218400 184533 71674 154830 181350 255732 62120 226122 154464 226934 60637 136046 167000 92035 262614 9832...

output:

6746554421237129

result:

ok 1 number(s): "6746554421237129"

Test #6:

score: 0
Accepted
time: 1190ms
memory: 436448kb

input:

300000
189478 283852 258947 31348 224765 203671 261674 251940 297781 47729 264024 140556 64712 65329 33007 217376 256454 7108 193648 207448 30334 55505 233835 169783 122652 8287 221157 294162 28017 123578 182903 111273 256282 221382 226523 102597 92176 289023 194843 176331 146753 109909 203621 23653...

output:

6744650229539119

result:

ok 1 number(s): "6744650229539119"

Test #7:

score: 0
Accepted
time: 1233ms
memory: 436076kb

input:

300000
297166 147453 149518 253853 36673 111100 115095 268581 202709 123274 73614 32274 9213 158043 286751 18138 148135 233128 244201 204040 47482 84963 55753 24462 136735 244953 69955 201154 21813 258319 174664 180484 74655 127941 243093 126973 24249 53767 73020 190571 293640 47219 21100 5151 18041...

output:

6747146480507599

result:

ok 1 number(s): "6747146480507599"

Test #8:

score: 0
Accepted
time: 1195ms
memory: 435216kb

input:

300000
276007 81788 125851 250182 49157 63717 162041 119971 129464 191475 60148 79868 215065 84330 126823 178157 209704 296285 15458 138677 54812 258153 72736 5193 296170 148711 140398 94742 258782 190293 171271 215973 279570 163564 141291 235329 65136 5245 74353 246536 57963 282154 254193 93054 386...

output:

6751420194437954

result:

ok 1 number(s): "6751420194437954"

Test #9:

score: 0
Accepted
time: 0ms
memory: 14112kb

input:

1
1

output:

1

result:

ok 1 number(s): "1"

Test #10:

score: 0
Accepted
time: 0ms
memory: 16236kb

input:

2
1 2

output:

4

result:

ok 1 number(s): "4"

Test #11:

score: 0
Accepted
time: 2ms
memory: 16236kb

input:

3
1 2 3

output:

11

result:

ok 1 number(s): "11"

Test #12:

score: 0
Accepted
time: 0ms
memory: 18280kb

input:

5
1 4 2 3 5

output:

39

result:

ok 1 number(s): "39"

Test #13:

score: 0
Accepted
time: 0ms
memory: 18232kb

input:

10
5 1 6 2 8 3 4 10 9 7

output:

257

result:

ok 1 number(s): "257"

Test #14:

score: 0
Accepted
time: 2ms
memory: 14140kb

input:

50
43 40 28 32 50 29 45 49 2 5 23 7 31 35 38 4 16 8 20 33 13 39 9 17 18 37 42 25 14 41 10 27 3 48 26 19 47 24 21 6 11 34 30 22 44 46 1 12 36 15

output:

28612

result:

ok 1 number(s): "28612"

Test #15:

score: 0
Accepted
time: 1ms
memory: 16220kb

input:

100
98 52 63 2 18 96 31 58 84 40 41 45 66 100 46 71 26 48 81 20 73 91 68 76 13 93 17 29 64 95 79 21 55 75 19 85 54 51 89 78 15 87 43 59 36 1 90 35 65 56 62 28 86 5 82 49 3 99 33 9 92 32 74 69 27 22 77 16 44 94 34 6 57 70 23 12 61 25 8 11 67 47 83 88 10 14 30 7 97 60 42 37 24 38 53 50 4 80 72 39

output:

245932

result:

ok 1 number(s): "245932"

Test #16:

score: 0
Accepted
time: 0ms
memory: 18480kb

input:

500
412 284 370 109 204 381 27 218 236 374 149 354 111 306 404 216 178 185 293 117 47 151 266 193 387 169 58 74 97 382 327 199 411 25 227 271 357 308 472 129 71 405 457 334 344 122 380 148 318 278 303 5 459 261 338 72 384 89 83 497 400 231 213 158 134 263 225 50 360 53 12 300 320 168 202 150 127 248...

output:

31614049

result:

ok 1 number(s): "31614049"

Test #17:

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

input:

1000
123 288 721 19 450 669 251 321 897 417 403 455 746 837 841 249 581 626 468 18 928 437 187 122 513 985 192 143 121 694 94 32 643 361 916 258 430 827 24 125 273 299 707 549 896 588 728 893 444 967 483 824 942 622 278 147 835 445 495 412 850 710 256 575 769 394 358 169 625 628 848 234 633 540 476 ...

output:

246022494

result:

ok 1 number(s): "246022494"

Test #18:

score: 0
Accepted
time: 11ms
memory: 26176kb

input:

5000
580 4555 4654 1420 53 1076 1226 2733 2285 348 2104 2293 3447 4208 710 307 1763 1142 3027 2151 3182 1546 3398 867 2380 830 4211 3117 3058 2251 1890 3961 4003 3991 4167 4976 1765 3235 2644 4070 4644 3645 875 3005 4769 4934 3846 2941 255 946 4164 1372 1193 3056 4472 508 3949 2473 4490 88 4014 2953...

output:

31148534632

result:

ok 1 number(s): "31148534632"

Test #19:

score: 0
Accepted
time: 27ms
memory: 32984kb

input:

10000
3905 5537 2138 6193 1368 6237 7641 2323 6717 4239 6529 5474 1310 4413 9583 8853 3201 5733 1079 3744 1650 5327 8284 2727 6044 8310 58 2129 6982 5675 5369 8051 2802 6405 2744 9287 3305 5564 3272 3155 3508 6289 4051 4257 1128 4292 948 7477 5652 6622 4408 1735 9201 1016 4457 2372 7358 5553 2012 20...

output:

249988272890

result:

ok 1 number(s): "249988272890"

Test #20:

score: 0
Accepted
time: 149ms
memory: 72172kb

input:

50000
18500 29922 29661 12166 1965 19535 12692 725 14510 10747 34507 14838 10113 49332 31879 33788 10149 36025 21570 48612 17698 36297 15409 33223 47068 32573 20569 38963 4662 3348 28073 9940 40320 31477 16622 7432 33861 40271 34814 2459 9999 7418 15089 28795 1611 39375 44276 32296 42610 3931 43199 ...

output:

31216323169118

result:

ok 1 number(s): "31216323169118"

Extra Test:

score: 0
Extra Test Passed