Submission #1097459

# Submission time Handle Problem Language Result Execution time Memory
1097459 2024-10-07T12:35:04 Z alexander707070 Diversity (CEOI21_diversity) C++14
64 / 100
1000 ms 116216 KB
#include<bits/stdc++.h>
#define MAXN 300007
using namespace std;

const long long bucket_sz=500;

long long n,a[MAXN],q;

long long br[MAXN],maxs,l,r,p[MAXN],len,bucket[MAXN],pos[MAXN],nxt[MAXN][20],pr[MAXN][20],w[MAXN];
long long ans,mult,sum,s[MAXN],res[MAXN];

struct Fenwick{
    long long fenwick[MAXN];

    void update(long long x,long long val){
        while(x<=maxs){
            fenwick[x]+=val;
            x+=(x & (-x));
        }
    }

    long long getsum(long long x){
        long long res=0;
        
        while(x>=1){
            res+=fenwick[x];
            x^=(x & (-x));
        }

        return res;
    }
}pref,suff,sumpref,sumsuff;

struct query{
    long long l,r,id;

    inline friend bool operator < (query fr,query sc){
        if(bucket[fr.l]!=bucket[sc.l])return bucket[fr.l]<bucket[sc.l];
        return fr.r<sc.r;
    }

}qs[MAXN];

long long calc(long long x){
    return s[x] * ( pref.getsum(x-1)-sumpref.getsum(x-1)*(maxs-x+1) + suff.getsum(maxs-x)-sumsuff.getsum(maxs-x)*x ); 
}

void change(long long x,long long val){
    ans-=calc(x);
    ans-=s[x]*(s[x]-1)/2+s[x];

    s[x]+=val;

    sumpref.update(x,val);
    sumsuff.update(maxs-x+1,val);

    pref.update(x,(maxs-x+2)*val);
    suff.update(maxs-x+1,(x+1)*val);

    ans+=calc(x);
    ans+=s[x]*(s[x]-1)/2+s[x];
}

void calcdp(){
    for(long long j=1;j<20;j++){
        for(long long i=1;i<=n;i++){
            nxt[i][j]=nxt[nxt[i][j-1]][j-1];
            pr[i][j]=pr[pr[i][j-1]][j-1];
        }
    }
}

void add(long long x){
    br[x]++;
    change(pos[x],1);

    long long z=pos[x];
    for(long long s=19;s>=0;s--){
        if(br[w[nxt[z][s]]]<br[x])z=nxt[z][s];
    }

    long long y=w[z];

    if(x!=y){
        change(pos[x],br[y]-br[x]);
        change(pos[y],br[x]-br[y]);

        swap(w[pos[x]],w[pos[y]]);
        swap(pos[x],pos[y]);
    }
}

void rem(long long x){
    br[x]--;
    change(pos[x],-1);

    long long z=pos[x];
    for(long long s=19;s>=0;s--){
        if(pr[z][s]!=0 and br[w[pr[z][s]]]>br[x])z=pr[z][s];
    }

    long long y=w[z];

    if(x!=y){
        change(pos[x],br[y]-br[x]);
        change(pos[y],br[x]-br[y]);

        swap(w[pos[x]],w[pos[y]]);
        swap(pos[x],pos[y]);
    }
}

int main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin>>n>>q;
    for(long long i=1;i<=n;i++){
        cin>>a[i];
        maxs=max(maxs,a[i]);

        bucket[i]=i/bucket_sz;
    }

    for(long long i=1;i<=maxs;i++)pos[i]=w[i]=i;

    for(long long i=1;i<=maxs;i++){
        if(i<=maxs/2)nxt[i][0]=maxs-i+1;
        else nxt[i][0]=maxs-i+2;
    }
    if(maxs%2==1)nxt[maxs/2+1][0]=maxs/2+1;

    for(long long i=1;i<=maxs;i++)pr[nxt[i][0]][0]=i;

    calcdp();

    for(long long i=1;i<=q;i++){
        cin>>qs[i].l>>qs[i].r;
        qs[i].id=i;
    }

    sort(qs+1,qs+q+1);

    l=1; r=0;
    for(long long i=1;i<=q;i++){
        while(r<qs[i].r){
            r++; add(a[r]);
        }

        while(l>qs[i].l){
            l--; add(a[l]);
        }

        while(r>qs[i].r){
            rem(a[r]); r--;
        }

        while(l<qs[i].l){
            rem(a[l]); l++;
        }

        res[qs[i].id]=ans;
    }

    for(long long i=1;i<=q;i++){
        cout<<res[i]<<"\n";
    }
    
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 472 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 472 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 472 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 5 ms 3676 KB Output is correct
4 Correct 45 ms 33600 KB Output is correct
5 Correct 91 ms 66680 KB Output is correct
6 Correct 147 ms 99664 KB Output is correct
7 Correct 149 ms 99668 KB Output is correct
8 Correct 156 ms 99668 KB Output is correct
9 Correct 149 ms 99620 KB Output is correct
10 Correct 148 ms 99436 KB Output is correct
11 Correct 149 ms 99432 KB Output is correct
12 Correct 147 ms 99660 KB Output is correct
13 Correct 149 ms 99616 KB Output is correct
14 Correct 152 ms 99664 KB Output is correct
15 Correct 151 ms 99668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 5 ms 3676 KB Output is correct
4 Correct 45 ms 33600 KB Output is correct
5 Correct 91 ms 66680 KB Output is correct
6 Correct 147 ms 99664 KB Output is correct
7 Correct 149 ms 99668 KB Output is correct
8 Correct 156 ms 99668 KB Output is correct
9 Correct 149 ms 99620 KB Output is correct
10 Correct 148 ms 99436 KB Output is correct
11 Correct 149 ms 99432 KB Output is correct
12 Correct 147 ms 99660 KB Output is correct
13 Correct 149 ms 99616 KB Output is correct
14 Correct 152 ms 99664 KB Output is correct
15 Correct 151 ms 99668 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 856 KB Output is correct
18 Correct 5 ms 3676 KB Output is correct
19 Correct 48 ms 33696 KB Output is correct
20 Correct 101 ms 66776 KB Output is correct
21 Correct 149 ms 99928 KB Output is correct
22 Correct 166 ms 99716 KB Output is correct
23 Correct 151 ms 99920 KB Output is correct
24 Correct 150 ms 99924 KB Output is correct
25 Correct 146 ms 99924 KB Output is correct
26 Correct 147 ms 99616 KB Output is correct
27 Correct 162 ms 99924 KB Output is correct
28 Correct 144 ms 99664 KB Output is correct
29 Correct 150 ms 99668 KB Output is correct
30 Correct 147 ms 99748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 860 KB Output is correct
3 Correct 5 ms 3676 KB Output is correct
4 Correct 45 ms 33600 KB Output is correct
5 Correct 91 ms 66680 KB Output is correct
6 Correct 147 ms 99664 KB Output is correct
7 Correct 149 ms 99668 KB Output is correct
8 Correct 156 ms 99668 KB Output is correct
9 Correct 149 ms 99620 KB Output is correct
10 Correct 148 ms 99436 KB Output is correct
11 Correct 149 ms 99432 KB Output is correct
12 Correct 147 ms 99660 KB Output is correct
13 Correct 149 ms 99616 KB Output is correct
14 Correct 152 ms 99664 KB Output is correct
15 Correct 151 ms 99668 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 1 ms 856 KB Output is correct
18 Correct 5 ms 3676 KB Output is correct
19 Correct 48 ms 33696 KB Output is correct
20 Correct 101 ms 66776 KB Output is correct
21 Correct 149 ms 99928 KB Output is correct
22 Correct 166 ms 99716 KB Output is correct
23 Correct 151 ms 99920 KB Output is correct
24 Correct 150 ms 99924 KB Output is correct
25 Correct 146 ms 99924 KB Output is correct
26 Correct 147 ms 99616 KB Output is correct
27 Correct 162 ms 99924 KB Output is correct
28 Correct 144 ms 99664 KB Output is correct
29 Correct 150 ms 99668 KB Output is correct
30 Correct 147 ms 99748 KB Output is correct
31 Correct 1 ms 860 KB Output is correct
32 Correct 1 ms 724 KB Output is correct
33 Correct 1 ms 860 KB Output is correct
34 Correct 5 ms 3680 KB Output is correct
35 Correct 5 ms 3672 KB Output is correct
36 Correct 6 ms 3872 KB Output is correct
37 Correct 24 ms 16992 KB Output is correct
38 Correct 28 ms 17044 KB Output is correct
39 Correct 56 ms 33624 KB Output is correct
40 Correct 118 ms 66652 KB Output is correct
41 Correct 174 ms 99768 KB Output is correct
42 Correct 182 ms 99852 KB Output is correct
43 Correct 176 ms 99972 KB Output is correct
44 Correct 180 ms 99764 KB Output is correct
45 Correct 176 ms 99984 KB Output is correct
46 Correct 177 ms 99984 KB Output is correct
47 Correct 181 ms 99916 KB Output is correct
48 Correct 179 ms 99760 KB Output is correct
49 Correct 172 ms 99924 KB Output is correct
50 Correct 176 ms 99892 KB Output is correct
51 Correct 169 ms 100064 KB Output is correct
52 Correct 175 ms 99920 KB Output is correct
53 Correct 183 ms 99928 KB Output is correct
54 Correct 184 ms 99924 KB Output is correct
55 Correct 180 ms 99956 KB Output is correct
56 Correct 181 ms 99924 KB Output is correct
57 Correct 179 ms 99888 KB Output is correct
58 Correct 182 ms 99804 KB Output is correct
59 Correct 179 ms 99916 KB Output is correct
60 Correct 187 ms 99972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 472 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 472 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 472 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 5 ms 3676 KB Output is correct
14 Correct 45 ms 33600 KB Output is correct
15 Correct 91 ms 66680 KB Output is correct
16 Correct 147 ms 99664 KB Output is correct
17 Correct 149 ms 99668 KB Output is correct
18 Correct 156 ms 99668 KB Output is correct
19 Correct 149 ms 99620 KB Output is correct
20 Correct 148 ms 99436 KB Output is correct
21 Correct 149 ms 99432 KB Output is correct
22 Correct 147 ms 99660 KB Output is correct
23 Correct 149 ms 99616 KB Output is correct
24 Correct 152 ms 99664 KB Output is correct
25 Correct 151 ms 99668 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 1 ms 856 KB Output is correct
28 Correct 5 ms 3676 KB Output is correct
29 Correct 48 ms 33696 KB Output is correct
30 Correct 101 ms 66776 KB Output is correct
31 Correct 149 ms 99928 KB Output is correct
32 Correct 166 ms 99716 KB Output is correct
33 Correct 151 ms 99920 KB Output is correct
34 Correct 150 ms 99924 KB Output is correct
35 Correct 146 ms 99924 KB Output is correct
36 Correct 147 ms 99616 KB Output is correct
37 Correct 162 ms 99924 KB Output is correct
38 Correct 144 ms 99664 KB Output is correct
39 Correct 150 ms 99668 KB Output is correct
40 Correct 147 ms 99748 KB Output is correct
41 Correct 1 ms 860 KB Output is correct
42 Correct 1 ms 724 KB Output is correct
43 Correct 1 ms 860 KB Output is correct
44 Correct 5 ms 3680 KB Output is correct
45 Correct 5 ms 3672 KB Output is correct
46 Correct 6 ms 3872 KB Output is correct
47 Correct 24 ms 16992 KB Output is correct
48 Correct 28 ms 17044 KB Output is correct
49 Correct 56 ms 33624 KB Output is correct
50 Correct 118 ms 66652 KB Output is correct
51 Correct 174 ms 99768 KB Output is correct
52 Correct 182 ms 99852 KB Output is correct
53 Correct 176 ms 99972 KB Output is correct
54 Correct 180 ms 99764 KB Output is correct
55 Correct 176 ms 99984 KB Output is correct
56 Correct 177 ms 99984 KB Output is correct
57 Correct 181 ms 99916 KB Output is correct
58 Correct 179 ms 99760 KB Output is correct
59 Correct 172 ms 99924 KB Output is correct
60 Correct 176 ms 99892 KB Output is correct
61 Correct 169 ms 100064 KB Output is correct
62 Correct 175 ms 99920 KB Output is correct
63 Correct 183 ms 99928 KB Output is correct
64 Correct 184 ms 99924 KB Output is correct
65 Correct 180 ms 99956 KB Output is correct
66 Correct 181 ms 99924 KB Output is correct
67 Correct 179 ms 99888 KB Output is correct
68 Correct 182 ms 99804 KB Output is correct
69 Correct 179 ms 99916 KB Output is correct
70 Correct 187 ms 99972 KB Output is correct
71 Correct 31 ms 17300 KB Output is correct
72 Correct 34 ms 17240 KB Output is correct
73 Correct 31 ms 17240 KB Output is correct
74 Correct 31 ms 17240 KB Output is correct
75 Correct 31 ms 17244 KB Output is correct
76 Correct 77 ms 34140 KB Output is correct
77 Correct 74 ms 34140 KB Output is correct
78 Correct 75 ms 34132 KB Output is correct
79 Correct 74 ms 34132 KB Output is correct
80 Correct 74 ms 34004 KB Output is correct
81 Correct 166 ms 67492 KB Output is correct
82 Correct 165 ms 67664 KB Output is correct
83 Correct 165 ms 67660 KB Output is correct
84 Correct 178 ms 67664 KB Output is correct
85 Correct 183 ms 67664 KB Output is correct
86 Correct 199 ms 68180 KB Output is correct
87 Correct 212 ms 68180 KB Output is correct
88 Correct 213 ms 68180 KB Output is correct
89 Correct 205 ms 68180 KB Output is correct
90 Correct 205 ms 68252 KB Output is correct
91 Correct 457 ms 106316 KB Output is correct
92 Correct 435 ms 106428 KB Output is correct
93 Correct 414 ms 106320 KB Output is correct
94 Correct 401 ms 106432 KB Output is correct
95 Correct 411 ms 106324 KB Output is correct
96 Correct 510 ms 112724 KB Output is correct
97 Correct 532 ms 112740 KB Output is correct
98 Correct 581 ms 112724 KB Output is correct
99 Correct 560 ms 112732 KB Output is correct
100 Correct 553 ms 112724 KB Output is correct
101 Correct 575 ms 112924 KB Output is correct
102 Correct 537 ms 112864 KB Output is correct
103 Correct 575 ms 112980 KB Output is correct
104 Correct 551 ms 112932 KB Output is correct
105 Correct 547 ms 112932 KB Output is correct
106 Correct 587 ms 116044 KB Output is correct
107 Correct 610 ms 116064 KB Output is correct
108 Correct 605 ms 116064 KB Output is correct
109 Correct 610 ms 115936 KB Output is correct
110 Correct 576 ms 116216 KB Output is correct
111 Correct 590 ms 116068 KB Output is correct
112 Correct 605 ms 116036 KB Output is correct
113 Correct 610 ms 116052 KB Output is correct
114 Correct 591 ms 115816 KB Output is correct
115 Correct 607 ms 116028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 472 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 472 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 472 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 860 KB Output is correct
13 Correct 5 ms 3676 KB Output is correct
14 Correct 45 ms 33600 KB Output is correct
15 Correct 91 ms 66680 KB Output is correct
16 Correct 147 ms 99664 KB Output is correct
17 Correct 149 ms 99668 KB Output is correct
18 Correct 156 ms 99668 KB Output is correct
19 Correct 149 ms 99620 KB Output is correct
20 Correct 148 ms 99436 KB Output is correct
21 Correct 149 ms 99432 KB Output is correct
22 Correct 147 ms 99660 KB Output is correct
23 Correct 149 ms 99616 KB Output is correct
24 Correct 152 ms 99664 KB Output is correct
25 Correct 151 ms 99668 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 1 ms 856 KB Output is correct
28 Correct 5 ms 3676 KB Output is correct
29 Correct 48 ms 33696 KB Output is correct
30 Correct 101 ms 66776 KB Output is correct
31 Correct 149 ms 99928 KB Output is correct
32 Correct 166 ms 99716 KB Output is correct
33 Correct 151 ms 99920 KB Output is correct
34 Correct 150 ms 99924 KB Output is correct
35 Correct 146 ms 99924 KB Output is correct
36 Correct 147 ms 99616 KB Output is correct
37 Correct 162 ms 99924 KB Output is correct
38 Correct 144 ms 99664 KB Output is correct
39 Correct 150 ms 99668 KB Output is correct
40 Correct 147 ms 99748 KB Output is correct
41 Correct 1 ms 860 KB Output is correct
42 Correct 1 ms 724 KB Output is correct
43 Correct 1 ms 860 KB Output is correct
44 Correct 5 ms 3680 KB Output is correct
45 Correct 5 ms 3672 KB Output is correct
46 Correct 6 ms 3872 KB Output is correct
47 Correct 24 ms 16992 KB Output is correct
48 Correct 28 ms 17044 KB Output is correct
49 Correct 56 ms 33624 KB Output is correct
50 Correct 118 ms 66652 KB Output is correct
51 Correct 174 ms 99768 KB Output is correct
52 Correct 182 ms 99852 KB Output is correct
53 Correct 176 ms 99972 KB Output is correct
54 Correct 180 ms 99764 KB Output is correct
55 Correct 176 ms 99984 KB Output is correct
56 Correct 177 ms 99984 KB Output is correct
57 Correct 181 ms 99916 KB Output is correct
58 Correct 179 ms 99760 KB Output is correct
59 Correct 172 ms 99924 KB Output is correct
60 Correct 176 ms 99892 KB Output is correct
61 Correct 169 ms 100064 KB Output is correct
62 Correct 175 ms 99920 KB Output is correct
63 Correct 183 ms 99928 KB Output is correct
64 Correct 184 ms 99924 KB Output is correct
65 Correct 180 ms 99956 KB Output is correct
66 Correct 181 ms 99924 KB Output is correct
67 Correct 179 ms 99888 KB Output is correct
68 Correct 182 ms 99804 KB Output is correct
69 Correct 179 ms 99916 KB Output is correct
70 Correct 187 ms 99972 KB Output is correct
71 Correct 31 ms 17300 KB Output is correct
72 Correct 34 ms 17240 KB Output is correct
73 Correct 31 ms 17240 KB Output is correct
74 Correct 31 ms 17240 KB Output is correct
75 Correct 31 ms 17244 KB Output is correct
76 Correct 77 ms 34140 KB Output is correct
77 Correct 74 ms 34140 KB Output is correct
78 Correct 75 ms 34132 KB Output is correct
79 Correct 74 ms 34132 KB Output is correct
80 Correct 74 ms 34004 KB Output is correct
81 Correct 166 ms 67492 KB Output is correct
82 Correct 165 ms 67664 KB Output is correct
83 Correct 165 ms 67660 KB Output is correct
84 Correct 178 ms 67664 KB Output is correct
85 Correct 183 ms 67664 KB Output is correct
86 Correct 199 ms 68180 KB Output is correct
87 Correct 212 ms 68180 KB Output is correct
88 Correct 213 ms 68180 KB Output is correct
89 Correct 205 ms 68180 KB Output is correct
90 Correct 205 ms 68252 KB Output is correct
91 Correct 457 ms 106316 KB Output is correct
92 Correct 435 ms 106428 KB Output is correct
93 Correct 414 ms 106320 KB Output is correct
94 Correct 401 ms 106432 KB Output is correct
95 Correct 411 ms 106324 KB Output is correct
96 Correct 510 ms 112724 KB Output is correct
97 Correct 532 ms 112740 KB Output is correct
98 Correct 581 ms 112724 KB Output is correct
99 Correct 560 ms 112732 KB Output is correct
100 Correct 553 ms 112724 KB Output is correct
101 Correct 575 ms 112924 KB Output is correct
102 Correct 537 ms 112864 KB Output is correct
103 Correct 575 ms 112980 KB Output is correct
104 Correct 551 ms 112932 KB Output is correct
105 Correct 547 ms 112932 KB Output is correct
106 Correct 587 ms 116044 KB Output is correct
107 Correct 610 ms 116064 KB Output is correct
108 Correct 605 ms 116064 KB Output is correct
109 Correct 610 ms 115936 KB Output is correct
110 Correct 576 ms 116216 KB Output is correct
111 Correct 590 ms 116068 KB Output is correct
112 Correct 605 ms 116036 KB Output is correct
113 Correct 610 ms 116052 KB Output is correct
114 Correct 591 ms 115816 KB Output is correct
115 Correct 607 ms 116028 KB Output is correct
116 Incorrect 1000 ms 6224 KB Output isn't correct
117 Halted 0 ms 0 KB -