QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#886818#9973. 魔法少女网站第二部nullptr_qwqAC ✓2003ms110300kbC++173.9kb2025-02-07 11:42:202025-02-07 11:42:22

Judging History

This is the latest submission verdict.

  • [2025-02-07 11:42:22]
  • Judged
  • Verdict: AC
  • Time: 2003ms
  • Memory: 110300kb
  • [2025-02-07 11:42:20]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

using ui = unsigned;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define per(i,l,r) for(int i=(l);i>=(r);--i)
#define repn(i,n)  for(int i=0;i<(n);++i)
#define sizc(x) ((int)x.size())
#define allc(x) x.begin(),x.end()
#define fir first
#define sec second

char getc(){
    constexpr int SZ=1<<14;
    static char buf[SZ],*p=buf,*q=buf;
    if(p==q){
        p=buf;
        q=buf+fread(buf,1,SZ,stdin);
        if(p==q)return EOF;
    }
    return *p++;
}
int read(){
    int x=0,sg=0;
    char ch=getc();
    while(!isdigit(ch))sg|=ch=='-',ch=getc();
    while(isdigit(ch))x=x*10+(ch-'0'),ch=getc();
    return sg?-x:x;
}


constexpr int N = 2e6+5;

int n,m;
int a[N];
int p[N];

struct query{
    int l,r,id;
}q[N],tq[N];
ll ans[N];

int pre[N],nxt[N],vl[N];

struct segtree{
    ll c[N<<1];
    void add(int x,ll y){
        x+=n-1;
        while(x)c[x]+=y,x>>=4;
    }
    ll sum(int x){
        int l=n,r=n+x-1;
        ll ret=0;
        while((l>>4)!=(r>>4)){
            while(l&15)ret+=c[l++];
            while(r+1&15)ret+=c[r--];
            l>>=4,r>>=4;
        }
        return accumulate(c+l,c+r+1,ret);
    }
}sgt;

void dc(int l,int r,int ql,int qr){
    if(ql>qr||l==r){
        sort(p+l,p+r+1,[&](int x,int y){
            return a[x]<a[y];
        });
        return;
    }
    int mid=l+r>>1;
    int p1=partition(q+ql,q+qr+1,[&](const query &a){
        return a.r<=mid;
    })-q;
    int p2=partition(q+p1,q+qr+1,[&](const query &a){
        return a.l>mid;
    })-q;
    dc(l,mid,ql,p1-1),dc(mid+1,r,p1,p2-1);

    sort(q+p2,q+qr+1,[&](const query &a,const query &b){
        return a.r>b.r;
    });
    int ptr=l;
    ll sum=0;
    rep(i,mid,r){
        int x=i>mid?p[i]:0,y=i<r?p[i+1]:n+1;
        nxt[x]=y,pre[y]=x;
        vl[x]=0;
        while(ptr<=mid&&a[p[ptr]]<a[y])vl[x]=max(vl[x],p[ptr++]);
        int v1=x+(y>n?0:y),v2=x>0&&y<=n?abs(x-y):0;
        sum+=v1,sgt.add(vl[x]+1,v2-v1);
    }
    ptr=p2;
    per(i,r,mid+1){
        while(ptr<=qr&&q[ptr].r==i){
            ans[q[ptr].id]+=sum+sgt.sum(q[ptr].l);
            ++ptr;
        }
        int x=pre[i],y=nxt[i];
        int v1=x+i,v2=x>0?i-x:0;
        sum-=v1,sgt.add(vl[x]+1,v1-v2);
        v1=i+(y>n?0:y),v2=y<=n?i-y:0;
        sum-=v1,sgt.add(vl[i]+1,v1-v2);
        vl[x]=max(vl[x],vl[i]);
        v1=x+(y>n?0:y),v2=x>0&&y<=n?abs(x-y):0;
        sum+=v1,sgt.add(vl[x]+1,v2-v1);
        nxt[x]=y,pre[y]=x;
    }

    sort(q+p2,q+qr+1,[&](const query &a,const query &b){
        return a.l<b.l;
    });
    ptr=mid+1,sum=0;
    rep(i,l-1,mid){
        int x=i<l?0:p[i],y=i<mid?p[i+1]:n+1;
        nxt[x]=y,pre[y]=x;
        vl[x]=n+1;
        while(ptr<=r&&a[p[ptr]]<a[y])vl[x]=min(vl[x],p[ptr++]);
        int v1=-x-(y>n?0:y),v2=x>0&&y<=n?abs(x-y):0;
        sum+=v2,sgt.add(vl[x],v1-v2);
    }
    ptr=p2;
    rep(i,l,mid){
        while(ptr<=qr&&q[ptr].l==i){
            ans[q[ptr].id]+=sum+sgt.sum(q[ptr].r);
            ++ptr;
        }
        int x=pre[i],y=nxt[i];
        int v1=-x-i,v2=x>0?x-i:0;
        sum-=v2,sgt.add(vl[x],v2-v1);
        v1=-i-(y>n?0:y),v2=y<=n?y-i:0;
        sum-=v2,sgt.add(vl[i],v2-v1);
        vl[x]=min(vl[x],vl[i]);
        v1=-x-(y>n?0:y),v2=x>0&&y<=n?abs(x-y):0;
        sum+=v2,sgt.add(vl[x],v1-v2);
        nxt[x]=y,pre[y]=x;
    }

    inplace_merge(p+l,p+mid+1,p+r+1,[&](int x,int y){
        return a[x]<a[y];
    });
}

signed main(){
    // freopen(".in","r",stdin);
    // freopen(".out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    n=read(),m=read();
    rep(i,1,n)a[i]=read(),p[i]=i;a[n+1]=n+1;
    rep(i,1,m)q[i].l=read(),q[i].r=read(),q[i].id=i;
    dc(1,n,1,m);rep(i,1,m)cout<<ans[i]<<'\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1560ms
memory: 108464kb

input:

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

output:

918614
3322452
5149417
6621380
6533731
4094511
1379510
12924019
8157295
207612
6762916
1785007
4192984
2683252
8261655
6023203
9129238
1758632
6933498
3993947
4345067
11514930
5784142
3105482
8690594
6483330
5398139
146567
1726131
678045
7787022
2468410
8925300
2915763
2410040
1092694
568436
3475699...

result:

ok 1999998 numbers

Test #2:

score: 0
Accepted
time: 1583ms
memory: 110184kb

input:

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

output:

313920
6698345
2961193
3760894
1903023
549714
5500986
1427297
168364
7880151
7786970
4914863
8011527
5473090
10758103
3603965
1922957
7492511
6241425
8034137
3953170
4224180
10918550
8192854
10607899
564778
493566
2426821
12903222
1008307
1113749
5046569
2226134
403996
4119770
874567
2775979
3062657...

result:

ok 2000000 numbers

Test #3:

score: 0
Accepted
time: 427ms
memory: 56968kb

input:

1997 1993006
687 310 1393 747 1050 349 1814 1842 638 1694 321 1613 1034 1724 1579 1292 1761 1387 1213 463 1884 1882 835 316 1722 8 1743 473 978 1274 403 1617 1711 156 1227 40 1858 851 1625 1787 1779 1289 472 1103 1739 1563 507 1384 1804 1079 199 347 1462 1891 198 583 1112 1399 940 1958 1830 318 1312...

output:

1240830
1253194
1124612
1178272
1124252
1207336
1222464
1067970
1182422
1294456
1081384
1134164
1028222
1303360
1176576
1333256
1217496
1184112
826586
1321466
907642
1200660
1338438
1037922
1150322
1107906
1225848
947268
763606
1312232
1092234
1048136
1276728
1282240
1320450
826070
1150624
1089414
1...

result:

ok 1993006 numbers

Test #4:

score: 0
Accepted
time: 490ms
memory: 56960kb

input:

2000 1999000
7 8 1120 3 2 6 1 5 1118 4 15 1119 14 16 18 17 19 12 10 13 11 9 1125 1126 1124 1128 653 1127 1121 1123 1122 1137 1136 620 1130 1129 598 661 599 1160 654 656 655 608 1159 1157 1158 1153 657 1154 585 1155 584 658 588 586 587 1156 609 1133 1132 621 1131 611 597 660 596 1134 595 659 610 1135...

output:

21604
8101
19995
14882
8432
877
5904
13612
1227
3406
25246
19909
10398
3746
992
6920
7365
3098
9999
12602
13469
5809
9235
5502
18869
2545
20293
9800
9162
22085
629
17750
9312
6800
3584
5285
1665
18997
8614
910
8170
3884
2539
16147
4482
6615
4589
56
14542
5125
2243
8915
11027
17562
901
13603
15113
35...

result:

ok 1999000 numbers

Test #5:

score: 0
Accepted
time: 1910ms
memory: 108908kb

input:

1999998 1999996
1950413 1996247 1995151 1968122 1967590 1953664 1990263 1993028 1840495 1948577 1937627 1996103 1985373 1990286 1941726 1980603 1911368 1997777 1980827 1911708 1857169 1908738 1988483 1981228 1964408 1973964 1988108 1997405 1974863 1955238 1931220 1956903 1979158 1973709 1994885 1927...

output:

23360918224
17249116985
137365596
7383633917
344733205
13693676447
45751450613
41799074078
18749658821
8191544190
21313767745
10345260569
28616032820
24769453037
13508471201
11284312607
46246584
39927786203
32769454327
29036442020
4659551922
13004993101
14277007260
14959218808
41953497036
1454775489...

result:

ok 1999996 numbers

Test #6:

score: 0
Accepted
time: 1594ms
memory: 106204kb

input:

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

output:

562213112
94551420
262159799
602091491
72695579
360317862
329807355
140956386
151337533
659115214
400318102
186031488
207523349
57392693
222407249
352720579
242142491
407723070
20117481
192919246
19846194
71898155
269685332
484339831
112403213
205534120
330882290
437608076
490767271
181662191
455826...

result:

ok 2000000 numbers

Test #7:

score: 0
Accepted
time: 1217ms
memory: 108436kb

input:

2000000 2000000
1995252 1948611 1973950 1976206 1955844 1955912 1996169 1996972 1954774 1996640 1996968 1982313 1990998 1989028 1996551 1997076 1975971 1984519 1968527 1992793 1966604 1996348 1975495 1984452 1987961 1892158 1984326 1989766 1982452 1899841 1992812 1974021 1966972 1907205 1984423 1952...

output:

99198920232
110545019536
36176871359
79529492756
74201219262
73151236992
51014287398
60618273245
53981621748
66773422886
81712780914
74653436956
102721856454
58071021899
56510885872
60061184092
57578487582
83000507008
59640113133
61871930929
74642738564
54032804049
54894094476
31473453995
4718329256...

result:

ok 2000000 numbers

Test #8:

score: 0
Accepted
time: 1580ms
memory: 110240kb

input:

2000000 2000000
2 1 3 7 4 8 2003 764 765 1032 5 769 767 768 766 1037 1036 1034 1035 1033 6 1028 1030 1031 1029 1027 1026 1015 1021 1022 1023 1016 1024 1025 1017 1018 1019 1020 1013 1014 1011 1012 1010 1009 1008 1007 1005 1006 770 771 773 772 774 686 1038 2029 2030 1056 1057 2027 2028 1039 1040 1042 ...

output:

173888
1306856
16467650
5397437
22072736
23788517
1496488
25224093
27624594
32980700
3875395
26560832
6219638
46020178
34863664
9338841
45376438
1574336
22195868
26577385
61316913
9801600
4080597
53217149
8489025
23869293
16335283
30679698
40485034
464955
26084028
1860488
5215924
4767111
43673987
29...

result:

ok 2000000 numbers

Test #9:

score: 0
Accepted
time: 1914ms
memory: 108256kb

input:

1999999 1999999
1999229 1983788 1985038 1921593 1990549 1933372 1995866 1939374 1975680 1978926 1888723 1997207 1959374 1939635 1977884 1959376 1982397 1988493 1904974 1978350 1981682 1950026 1997268 1946338 1993309 1979525 1987557 1878825 1935649 1996862 1929559 1992596 1985390 1921751 1996826 1974...

output:

4925894357
4896310942
774067296
31352634339
83808980
9955732285
12782716001
3912253669
51737997080
17520780254
44627725396
7677895984
14127220676
46595914491
15172014314
12967705758
28691964079
11189785689
50575875372
28536846803
57726352901
35971953715
32451290008
14607111719
15490945147
3264818983...

result:

ok 1999999 numbers

Test #10:

score: 0
Accepted
time: 1604ms
memory: 108228kb

input:

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

output:

18486785
565994488
357992305
324290811
173749872
463288805
327577422
348086484
382579220
132150404
329503732
299633065
23601040
338483775
285797634
40510902
39916011
123112783
61069626
353545436
496348490
679019350
578471236
12746729
322699026
279954358
512489583
494866785
510234978
498991930
367631...

result:

ok 2000000 numbers

Test #11:

score: 0
Accepted
time: 1223ms
memory: 107700kb

input:

2000000 2000000
1976597 1982586 1949576 1932500 1991588 1979675 1903615 1964510 1977449 1993833 1985612 1952946 1980283 1931403 1968748 1977616 1965770 1968991 1955128 1975310 1985203 1953011 1929540 1984740 1992529 1979818 1976250 1974369 1930952 1988564 1889358 1929981 1980760 1975492 1973914 1996...

output:

62299960731
62457645503
108427988855
59730232235
61644581297
37439022602
97940896099
62590089573
95266076479
57244355411
63088847191
47897640566
51213318265
54839923338
62076710864
91643872593
98540780131
66382670525
62106352732
58239266348
57116604803
56281320295
42276085370
89686477933
12628163112...

result:

ok 2000000 numbers

Test #12:

score: 0
Accepted
time: 2003ms
memory: 109092kb

input:

2000000 2000000
1 2 1020 1021 1019 1024 1023 1022 993 994 995 5 4 1018 3 6 989 990 991 1017 992 1015 1014 1016 1013 1012 1009 1010 1008 1011 1007 1027 1026 1000 998 999 1006 1041 1003 1004 1001 997 1005 1025 996 1040 1002 1039 1037 1038 1035 1036 1034 1033 1032 1043 1042 1030 1028 1029 1031 1044 104...

output:

7362728
5901851
43317555
19913544
19158826
51177396
15817696
31399369
6217588
5971688
24194317
39987170
13654960
1278489
9526214
114723
4028623
29715971
31541650
26253442
19909987
13205284
37297254
7683306
34111013
19735919
6127455
6669085
6945638
8848212
9909547
681390
13369163
1256514
22914947
909...

result:

ok 2000000 numbers

Test #13:

score: 0
Accepted
time: 1921ms
memory: 108260kb

input:

1999995 1999999
1936672 1989379 1948029 1990284 1927017 1959739 1997853 1982539 1998959 1974358 1970662 1985295 1982329 1999460 1978155 1997271 1914440 1995945 1996049 1943549 1959542 1950745 1938170 1993031 1992055 1975631 1999390 1999725 1957124 1790325 1866296 1877061 1967550 1944444 1971649 1931...

output:

35950651968
15924280206
42560578945
56813065924
828293122
36481242286
4296141409
14816241029
7826560732
24863886042
39058274006
24216148824
32938674327
32974013597
17817317819
14449840802
44646413604
31266557373
412196878
1686996435
24635693163
609912483
640339072
9421101993
15005673887
14390637036
...

result:

ok 1999999 numbers

Test #14:

score: 0
Accepted
time: 1607ms
memory: 108472kb

input:

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

output:

48541636
77165175
43499632
454850613
350417271
581991501
435961193
36884013
475126189
137184640
28994774
427929603
122228682
704946051
332053374
25786932
14949071
278752602
200794985
519246142
475365027
61754800
241527349
17618230
460000052
84365204
439852872
764605381
47946907
140109146
449424729
1...

result:

ok 2000000 numbers

Test #15:

score: 0
Accepted
time: 1243ms
memory: 104576kb

input:

2000000 2000000
1992773 1946362 1941792 1961083 1999925 1982386 1971523 1999448 1993704 1993825 1998722 1975132 1963436 1970995 1978628 1997953 1904423 1943613 1983078 1886282 1944015 1951777 1896373 1944238 1938932 1985054 1965267 1998981 1988670 1988739 1989862 1996688 1953416 1987137 1919616 1983...

output:

53072255731
49555714940
54462867910
62450413447
99773650069
60992126123
118612435607
103197159693
100195164935
117125022863
105194352743
62213528893
98686523409
58289204447
57317895701
62097298558
81157286803
53514331312
57653585617
62121301750
49788136229
77216468855
49255612627
59036307590
4301137...

result:

ok 2000000 numbers

Test #16:

score: 0
Accepted
time: 1592ms
memory: 108164kb

input:

2000000 2000000
1 2 3 4 5 6 14 15 7 8 13 16 12 11 10 9 17 455 456 457 458 459 18 454 453 452 451 450 449 448 447 460 19 462 461 42 41 63 62 40 39 47 38 45 46 44 43 61 60 430 35 37 36 34 33 32 48 49 50 56 55 54 53 52 51 57 58 59 431 432 433 434 421 422 440 423 441 427 424 425 426 439 438 419 443 437 ...

output:

21182509
8833822
881337
60874875
6063493
23842280
24803452
16110354
2580479
26147515
15345949
7315215
30178796
5995525
32689522
29678416
4385188
14694989
22501076
18973805
615729
28088461
15179939
8617740
27478348
13078495
24492339
8642588
24394654
53788930
9720722
43489388
4297959
34103438
51252672...

result:

ok 2000000 numbers

Test #17:

score: 0
Accepted
time: 1910ms
memory: 110300kb

input:

1999995 1999998
1979846 1947124 1965946 1953594 1983574 1990659 1929905 1888928 1974444 1998069 1999240 1996891 1975733 1927117 1994851 1987958 1980478 1961408 1992372 1917909 1975636 1988412 1990717 1983126 1923129 1998325 1974081 1995268 1988405 1921058 1998966 1947350 1927897 1937097 1969117 1917...

output:

32287098380
53428024584
44067151576
21883427725
1954928849
40672639080
5475066235
51224942661
42680276748
24205761028
34346528066
4499909900
9596714849
41559896451
13728128953
23796822279
19574455174
12895675188
30129398206
39552064305
15583024420
24878020151
7894033920
32467239927
16163187489
61067...

result:

ok 1999998 numbers

Test #18:

score: 0
Accepted
time: 1605ms
memory: 109972kb

input:

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

output:

44107849
299212237
587192346
412342130
207705679
509268742
254545621
123143925
386528881
235374266
164255979
144652798
161183072
42962269
427075598
103570813
291138270
37291897
26637582
445593136
235610551
453925390
647807572
228904713
627301150
111158021
411196806
155510033
398568110
168715235
5002...

result:

ok 2000000 numbers

Test #19:

score: 0
Accepted
time: 1214ms
memory: 106068kb

input:

2000000 2000000
1994991 1960973 1994201 1931188 1916621 1954707 1978833 1989630 1998865 1997932 1967997 1915620 1918445 1993617 1910602 1999218 1993846 1978670 1982312 1944648 1986274 1976382 1999067 1972806 1968503 1978829 1809727 1998040 1974996 1964273 1992340 1946553 1972483 1948091 1992452 1947...

output:

54625243647
53118288546
62990506963
64344700323
57378382873
56618622821
96455402431
58659363273
60820545629
61247228503
50124408073
59686238034
54394327956
52033356917
61082704289
115119672403
49897003125
102405853351
80042940061
50471093310
89465526389
61150639953
89264894451
112617269869
599563218...

result:

ok 2000000 numbers

Test #20:

score: 0
Accepted
time: 1585ms
memory: 108064kb

input:

2000000 2000000
1 2 4 3 5 6 13 8718 8717 8716 8715 8714 8713 8711 8710 8712 8760 8761 8759 8758 8722 8724 8723 8725 8726 8727 8763 8709 8762 8708 8729 8728 8764 8765 8720 8719 12 8769 8770 8776 8775 8768 8767 8771 8766 14 11 8774 9 10 7 8 8773 8772 8721 8757 8756 8755 8754 8753 8747 8748 8738 8749 8...

output:

43700615
31074881
5698557
52225801
28295216
22696143
12848176
21938424
60549063
31499303
4827015
63422407
67177725
53224341
52178747
35213267
37232084
1937857
43284113
7525362
2921516
45860331
33960048
8081204
55930705
7057285
21957921
54954771
38300625
15607967
26954278
6930152
1085726
34388864
177...

result:

ok 2000000 numbers