Submission #1097581

# Submission time Handle Problem Language Result Execution time Memory
1097581 2024-10-07T15:14:55 Z alexander707070 Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 61844 KB
#include<iostream>
#include<algorithm>

#define MAXN 300007
using namespace std;

const int bucket_sz=500;

int n,a[MAXN],q;

int 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];

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

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

        return res;
    }
}pref,suff,sumpref;

struct query{
    int 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];

        if(bucket[fr.l]%2==0)return fr.r<sc.r;
        else return fr.r>sc.r;
    }

}qs[MAXN];

long long total;

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

void change(int x,long long val){
    if(val==0)return;

    long long mul=calc(x);

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

    s[x]+=val;

    sumpref.update(x,val);
    total+=val;

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

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

void calcdp(){
    for(int j=1;j<20;j++){
        for(int 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(int x){
    br[x]++;

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

    int y=w[z];

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

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

void rem(int x){
    br[x]--;

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

    int y=w[z];

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

        swap(w[pos[x]],w[pos[y]]);
        swap(pos[x],pos[y]);
    }else{
        change(pos[x],-1);
    }
}

int main(){

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

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

        bucket[i]=i/bucket_sz;
    }

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

    for(int 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(int i=1;i<=maxs;i++)pr[nxt[i][0]][0]=i;

    if(maxs%2==1)pr[maxs/2+1][0]=maxs/2+2;
    else pr[maxs/2+1][0]=maxs/2;
    pr[1][0]=0;

    calcdp();

    for(int 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(int 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(int 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 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 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 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 2 ms 2136 KB Output is correct
4 Correct 20 ms 17088 KB Output is correct
5 Correct 45 ms 33620 KB Output is correct
6 Correct 67 ms 50300 KB Output is correct
7 Correct 64 ms 50116 KB Output is correct
8 Correct 66 ms 50328 KB Output is correct
9 Correct 66 ms 50256 KB Output is correct
10 Correct 66 ms 50328 KB Output is correct
11 Correct 64 ms 50316 KB Output is correct
12 Correct 67 ms 50256 KB Output is correct
13 Correct 67 ms 50208 KB Output is correct
14 Correct 69 ms 50256 KB Output is correct
15 Correct 68 ms 50260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 2 ms 2136 KB Output is correct
4 Correct 20 ms 17088 KB Output is correct
5 Correct 45 ms 33620 KB Output is correct
6 Correct 67 ms 50300 KB Output is correct
7 Correct 64 ms 50116 KB Output is correct
8 Correct 66 ms 50328 KB Output is correct
9 Correct 66 ms 50256 KB Output is correct
10 Correct 66 ms 50328 KB Output is correct
11 Correct 64 ms 50316 KB Output is correct
12 Correct 67 ms 50256 KB Output is correct
13 Correct 67 ms 50208 KB Output is correct
14 Correct 69 ms 50256 KB Output is correct
15 Correct 68 ms 50260 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 2 ms 2140 KB Output is correct
19 Correct 21 ms 17080 KB Output is correct
20 Correct 45 ms 33876 KB Output is correct
21 Correct 67 ms 50556 KB Output is correct
22 Correct 69 ms 50776 KB Output is correct
23 Correct 68 ms 50552 KB Output is correct
24 Correct 70 ms 50548 KB Output is correct
25 Correct 75 ms 50512 KB Output is correct
26 Correct 72 ms 50260 KB Output is correct
27 Correct 79 ms 50548 KB Output is correct
28 Correct 67 ms 50260 KB Output is correct
29 Correct 63 ms 50264 KB Output is correct
30 Correct 68 ms 50556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 2 ms 2136 KB Output is correct
4 Correct 20 ms 17088 KB Output is correct
5 Correct 45 ms 33620 KB Output is correct
6 Correct 67 ms 50300 KB Output is correct
7 Correct 64 ms 50116 KB Output is correct
8 Correct 66 ms 50328 KB Output is correct
9 Correct 66 ms 50256 KB Output is correct
10 Correct 66 ms 50328 KB Output is correct
11 Correct 64 ms 50316 KB Output is correct
12 Correct 67 ms 50256 KB Output is correct
13 Correct 67 ms 50208 KB Output is correct
14 Correct 69 ms 50256 KB Output is correct
15 Correct 68 ms 50260 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 2 ms 2140 KB Output is correct
19 Correct 21 ms 17080 KB Output is correct
20 Correct 45 ms 33876 KB Output is correct
21 Correct 67 ms 50556 KB Output is correct
22 Correct 69 ms 50776 KB Output is correct
23 Correct 68 ms 50552 KB Output is correct
24 Correct 70 ms 50548 KB Output is correct
25 Correct 75 ms 50512 KB Output is correct
26 Correct 72 ms 50260 KB Output is correct
27 Correct 79 ms 50548 KB Output is correct
28 Correct 67 ms 50260 KB Output is correct
29 Correct 63 ms 50264 KB Output is correct
30 Correct 68 ms 50556 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 3 ms 2140 KB Output is correct
35 Correct 4 ms 2140 KB Output is correct
36 Correct 5 ms 2256 KB Output is correct
37 Correct 15 ms 8792 KB Output is correct
38 Correct 18 ms 8796 KB Output is correct
39 Correct 34 ms 17240 KB Output is correct
40 Correct 69 ms 33876 KB Output is correct
41 Correct 98 ms 50604 KB Output is correct
42 Correct 95 ms 50564 KB Output is correct
43 Correct 92 ms 50512 KB Output is correct
44 Correct 93 ms 50516 KB Output is correct
45 Correct 95 ms 50516 KB Output is correct
46 Correct 97 ms 50512 KB Output is correct
47 Correct 99 ms 50516 KB Output is correct
48 Correct 100 ms 50576 KB Output is correct
49 Correct 97 ms 50512 KB Output is correct
50 Correct 93 ms 50512 KB Output is correct
51 Correct 89 ms 50556 KB Output is correct
52 Correct 89 ms 50512 KB Output is correct
53 Correct 92 ms 50516 KB Output is correct
54 Correct 93 ms 50516 KB Output is correct
55 Correct 95 ms 50512 KB Output is correct
56 Correct 96 ms 50512 KB Output is correct
57 Correct 94 ms 50516 KB Output is correct
58 Correct 91 ms 50560 KB Output is correct
59 Correct 90 ms 50516 KB Output is correct
60 Correct 89 ms 50512 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 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 2 ms 2136 KB Output is correct
14 Correct 20 ms 17088 KB Output is correct
15 Correct 45 ms 33620 KB Output is correct
16 Correct 67 ms 50300 KB Output is correct
17 Correct 64 ms 50116 KB Output is correct
18 Correct 66 ms 50328 KB Output is correct
19 Correct 66 ms 50256 KB Output is correct
20 Correct 66 ms 50328 KB Output is correct
21 Correct 64 ms 50316 KB Output is correct
22 Correct 67 ms 50256 KB Output is correct
23 Correct 67 ms 50208 KB Output is correct
24 Correct 69 ms 50256 KB Output is correct
25 Correct 68 ms 50260 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 2 ms 2140 KB Output is correct
29 Correct 21 ms 17080 KB Output is correct
30 Correct 45 ms 33876 KB Output is correct
31 Correct 67 ms 50556 KB Output is correct
32 Correct 69 ms 50776 KB Output is correct
33 Correct 68 ms 50552 KB Output is correct
34 Correct 70 ms 50548 KB Output is correct
35 Correct 75 ms 50512 KB Output is correct
36 Correct 72 ms 50260 KB Output is correct
37 Correct 79 ms 50548 KB Output is correct
38 Correct 67 ms 50260 KB Output is correct
39 Correct 63 ms 50264 KB Output is correct
40 Correct 68 ms 50556 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 3 ms 2140 KB Output is correct
45 Correct 4 ms 2140 KB Output is correct
46 Correct 5 ms 2256 KB Output is correct
47 Correct 15 ms 8792 KB Output is correct
48 Correct 18 ms 8796 KB Output is correct
49 Correct 34 ms 17240 KB Output is correct
50 Correct 69 ms 33876 KB Output is correct
51 Correct 98 ms 50604 KB Output is correct
52 Correct 95 ms 50564 KB Output is correct
53 Correct 92 ms 50512 KB Output is correct
54 Correct 93 ms 50516 KB Output is correct
55 Correct 95 ms 50516 KB Output is correct
56 Correct 97 ms 50512 KB Output is correct
57 Correct 99 ms 50516 KB Output is correct
58 Correct 100 ms 50576 KB Output is correct
59 Correct 97 ms 50512 KB Output is correct
60 Correct 93 ms 50512 KB Output is correct
61 Correct 89 ms 50556 KB Output is correct
62 Correct 89 ms 50512 KB Output is correct
63 Correct 92 ms 50516 KB Output is correct
64 Correct 93 ms 50516 KB Output is correct
65 Correct 95 ms 50512 KB Output is correct
66 Correct 96 ms 50512 KB Output is correct
67 Correct 94 ms 50516 KB Output is correct
68 Correct 91 ms 50560 KB Output is correct
69 Correct 90 ms 50516 KB Output is correct
70 Correct 89 ms 50512 KB Output is correct
71 Correct 20 ms 9052 KB Output is correct
72 Correct 19 ms 9052 KB Output is correct
73 Correct 19 ms 9000 KB Output is correct
74 Correct 18 ms 9052 KB Output is correct
75 Correct 20 ms 9048 KB Output is correct
76 Correct 44 ms 17500 KB Output is correct
77 Correct 43 ms 17496 KB Output is correct
78 Correct 42 ms 17492 KB Output is correct
79 Correct 45 ms 17500 KB Output is correct
80 Correct 40 ms 17496 KB Output is correct
81 Correct 93 ms 34436 KB Output is correct
82 Correct 101 ms 34384 KB Output is correct
83 Correct 99 ms 34540 KB Output is correct
84 Correct 104 ms 34460 KB Output is correct
85 Correct 93 ms 34568 KB Output is correct
86 Correct 101 ms 35000 KB Output is correct
87 Correct 104 ms 35156 KB Output is correct
88 Correct 101 ms 34904 KB Output is correct
89 Correct 99 ms 34900 KB Output is correct
90 Correct 101 ms 34900 KB Output is correct
91 Correct 228 ms 55200 KB Output is correct
92 Correct 224 ms 55124 KB Output is correct
93 Correct 220 ms 54908 KB Output is correct
94 Correct 254 ms 54920 KB Output is correct
95 Correct 232 ms 55124 KB Output is correct
96 Correct 284 ms 59536 KB Output is correct
97 Correct 272 ms 59472 KB Output is correct
98 Correct 291 ms 59620 KB Output is correct
99 Correct 293 ms 59572 KB Output is correct
100 Correct 288 ms 59532 KB Output is correct
101 Correct 279 ms 59540 KB Output is correct
102 Correct 264 ms 59440 KB Output is correct
103 Correct 272 ms 59476 KB Output is correct
104 Correct 305 ms 59732 KB Output is correct
105 Correct 289 ms 59448 KB Output is correct
106 Correct 307 ms 61736 KB Output is correct
107 Correct 321 ms 61808 KB Output is correct
108 Correct 307 ms 61840 KB Output is correct
109 Correct 300 ms 61844 KB Output is correct
110 Correct 291 ms 61788 KB Output is correct
111 Correct 331 ms 61776 KB Output is correct
112 Correct 316 ms 61672 KB Output is correct
113 Correct 295 ms 61780 KB Output is correct
114 Correct 301 ms 61756 KB Output is correct
115 Correct 299 ms 61676 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 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 2 ms 2136 KB Output is correct
14 Correct 20 ms 17088 KB Output is correct
15 Correct 45 ms 33620 KB Output is correct
16 Correct 67 ms 50300 KB Output is correct
17 Correct 64 ms 50116 KB Output is correct
18 Correct 66 ms 50328 KB Output is correct
19 Correct 66 ms 50256 KB Output is correct
20 Correct 66 ms 50328 KB Output is correct
21 Correct 64 ms 50316 KB Output is correct
22 Correct 67 ms 50256 KB Output is correct
23 Correct 67 ms 50208 KB Output is correct
24 Correct 69 ms 50256 KB Output is correct
25 Correct 68 ms 50260 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 2 ms 2140 KB Output is correct
29 Correct 21 ms 17080 KB Output is correct
30 Correct 45 ms 33876 KB Output is correct
31 Correct 67 ms 50556 KB Output is correct
32 Correct 69 ms 50776 KB Output is correct
33 Correct 68 ms 50552 KB Output is correct
34 Correct 70 ms 50548 KB Output is correct
35 Correct 75 ms 50512 KB Output is correct
36 Correct 72 ms 50260 KB Output is correct
37 Correct 79 ms 50548 KB Output is correct
38 Correct 67 ms 50260 KB Output is correct
39 Correct 63 ms 50264 KB Output is correct
40 Correct 68 ms 50556 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 3 ms 2140 KB Output is correct
45 Correct 4 ms 2140 KB Output is correct
46 Correct 5 ms 2256 KB Output is correct
47 Correct 15 ms 8792 KB Output is correct
48 Correct 18 ms 8796 KB Output is correct
49 Correct 34 ms 17240 KB Output is correct
50 Correct 69 ms 33876 KB Output is correct
51 Correct 98 ms 50604 KB Output is correct
52 Correct 95 ms 50564 KB Output is correct
53 Correct 92 ms 50512 KB Output is correct
54 Correct 93 ms 50516 KB Output is correct
55 Correct 95 ms 50516 KB Output is correct
56 Correct 97 ms 50512 KB Output is correct
57 Correct 99 ms 50516 KB Output is correct
58 Correct 100 ms 50576 KB Output is correct
59 Correct 97 ms 50512 KB Output is correct
60 Correct 93 ms 50512 KB Output is correct
61 Correct 89 ms 50556 KB Output is correct
62 Correct 89 ms 50512 KB Output is correct
63 Correct 92 ms 50516 KB Output is correct
64 Correct 93 ms 50516 KB Output is correct
65 Correct 95 ms 50512 KB Output is correct
66 Correct 96 ms 50512 KB Output is correct
67 Correct 94 ms 50516 KB Output is correct
68 Correct 91 ms 50560 KB Output is correct
69 Correct 90 ms 50516 KB Output is correct
70 Correct 89 ms 50512 KB Output is correct
71 Correct 20 ms 9052 KB Output is correct
72 Correct 19 ms 9052 KB Output is correct
73 Correct 19 ms 9000 KB Output is correct
74 Correct 18 ms 9052 KB Output is correct
75 Correct 20 ms 9048 KB Output is correct
76 Correct 44 ms 17500 KB Output is correct
77 Correct 43 ms 17496 KB Output is correct
78 Correct 42 ms 17492 KB Output is correct
79 Correct 45 ms 17500 KB Output is correct
80 Correct 40 ms 17496 KB Output is correct
81 Correct 93 ms 34436 KB Output is correct
82 Correct 101 ms 34384 KB Output is correct
83 Correct 99 ms 34540 KB Output is correct
84 Correct 104 ms 34460 KB Output is correct
85 Correct 93 ms 34568 KB Output is correct
86 Correct 101 ms 35000 KB Output is correct
87 Correct 104 ms 35156 KB Output is correct
88 Correct 101 ms 34904 KB Output is correct
89 Correct 99 ms 34900 KB Output is correct
90 Correct 101 ms 34900 KB Output is correct
91 Correct 228 ms 55200 KB Output is correct
92 Correct 224 ms 55124 KB Output is correct
93 Correct 220 ms 54908 KB Output is correct
94 Correct 254 ms 54920 KB Output is correct
95 Correct 232 ms 55124 KB Output is correct
96 Correct 284 ms 59536 KB Output is correct
97 Correct 272 ms 59472 KB Output is correct
98 Correct 291 ms 59620 KB Output is correct
99 Correct 293 ms 59572 KB Output is correct
100 Correct 288 ms 59532 KB Output is correct
101 Correct 279 ms 59540 KB Output is correct
102 Correct 264 ms 59440 KB Output is correct
103 Correct 272 ms 59476 KB Output is correct
104 Correct 305 ms 59732 KB Output is correct
105 Correct 289 ms 59448 KB Output is correct
106 Correct 307 ms 61736 KB Output is correct
107 Correct 321 ms 61808 KB Output is correct
108 Correct 307 ms 61840 KB Output is correct
109 Correct 300 ms 61844 KB Output is correct
110 Correct 291 ms 61788 KB Output is correct
111 Correct 331 ms 61776 KB Output is correct
112 Correct 316 ms 61672 KB Output is correct
113 Correct 295 ms 61780 KB Output is correct
114 Correct 301 ms 61756 KB Output is correct
115 Correct 299 ms 61676 KB Output is correct
116 Correct 300 ms 3920 KB Output is correct
117 Correct 478 ms 3928 KB Output is correct
118 Correct 488 ms 10580 KB Output is correct
119 Correct 1058 ms 10764 KB Output is correct
120 Correct 1270 ms 10836 KB Output is correct
121 Correct 2228 ms 19264 KB Output is correct
122 Correct 2771 ms 18460 KB Output is correct
123 Correct 5403 ms 34896 KB Output is correct
124 Execution timed out 7049 ms 34388 KB Time limit exceeded
125 Halted 0 ms 0 KB -