답안 #1097463

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1097463 2024-10-07T12:40:35 Z alexander707070 Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 64340 KB
#include<bits/stdc++.h>
#define MAXN 300007
using namespace std;

const int bucket_sz=300;

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

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

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

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

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];
        return fr.r<sc.r;
    }

}qs[MAXN];

long long calc(int 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(int 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(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]++;
    change(pos[x],1);

    int z=pos[x];
    for(int s=19;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]);
        change(pos[y],br[x]-br[y]);

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

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

    int z=pos[x];
    for(int s=19;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]);
        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(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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 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 472 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 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 4 ms 2140 KB Output is correct
4 Correct 33 ms 17164 KB Output is correct
5 Correct 69 ms 33744 KB Output is correct
6 Correct 122 ms 50456 KB Output is correct
7 Correct 103 ms 50260 KB Output is correct
8 Correct 101 ms 50248 KB Output is correct
9 Correct 98 ms 50260 KB Output is correct
10 Correct 99 ms 50240 KB Output is correct
11 Correct 99 ms 50460 KB Output is correct
12 Correct 100 ms 50488 KB Output is correct
13 Correct 107 ms 50284 KB Output is correct
14 Correct 97 ms 50240 KB Output is correct
15 Correct 102 ms 50420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 4 ms 2140 KB Output is correct
4 Correct 33 ms 17164 KB Output is correct
5 Correct 69 ms 33744 KB Output is correct
6 Correct 122 ms 50456 KB Output is correct
7 Correct 103 ms 50260 KB Output is correct
8 Correct 101 ms 50248 KB Output is correct
9 Correct 98 ms 50260 KB Output is correct
10 Correct 99 ms 50240 KB Output is correct
11 Correct 99 ms 50460 KB Output is correct
12 Correct 100 ms 50488 KB Output is correct
13 Correct 107 ms 50284 KB Output is correct
14 Correct 97 ms 50240 KB Output is correct
15 Correct 102 ms 50420 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 4 ms 2136 KB Output is correct
19 Correct 33 ms 17224 KB Output is correct
20 Correct 69 ms 33884 KB Output is correct
21 Correct 108 ms 50512 KB Output is correct
22 Correct 110 ms 50516 KB Output is correct
23 Correct 103 ms 50512 KB Output is correct
24 Correct 106 ms 50516 KB Output is correct
25 Correct 104 ms 50376 KB Output is correct
26 Correct 102 ms 50512 KB Output is correct
27 Correct 101 ms 50520 KB Output is correct
28 Correct 105 ms 50516 KB Output is correct
29 Correct 105 ms 50564 KB Output is correct
30 Correct 106 ms 50520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 4 ms 2140 KB Output is correct
4 Correct 33 ms 17164 KB Output is correct
5 Correct 69 ms 33744 KB Output is correct
6 Correct 122 ms 50456 KB Output is correct
7 Correct 103 ms 50260 KB Output is correct
8 Correct 101 ms 50248 KB Output is correct
9 Correct 98 ms 50260 KB Output is correct
10 Correct 99 ms 50240 KB Output is correct
11 Correct 99 ms 50460 KB Output is correct
12 Correct 100 ms 50488 KB Output is correct
13 Correct 107 ms 50284 KB Output is correct
14 Correct 97 ms 50240 KB Output is correct
15 Correct 102 ms 50420 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 4 ms 2136 KB Output is correct
19 Correct 33 ms 17224 KB Output is correct
20 Correct 69 ms 33884 KB Output is correct
21 Correct 108 ms 50512 KB Output is correct
22 Correct 110 ms 50516 KB Output is correct
23 Correct 103 ms 50512 KB Output is correct
24 Correct 106 ms 50516 KB Output is correct
25 Correct 104 ms 50376 KB Output is correct
26 Correct 102 ms 50512 KB Output is correct
27 Correct 101 ms 50520 KB Output is correct
28 Correct 105 ms 50516 KB Output is correct
29 Correct 105 ms 50564 KB Output is correct
30 Correct 106 ms 50520 KB Output is correct
31 Correct 1 ms 600 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 1 ms 604 KB Output is correct
34 Correct 4 ms 2140 KB Output is correct
35 Correct 5 ms 2140 KB Output is correct
36 Correct 6 ms 2140 KB Output is correct
37 Correct 20 ms 8796 KB Output is correct
38 Correct 24 ms 9136 KB Output is correct
39 Correct 44 ms 17244 KB Output is correct
40 Correct 91 ms 34044 KB Output is correct
41 Correct 129 ms 50944 KB Output is correct
42 Correct 136 ms 51028 KB Output is correct
43 Correct 135 ms 51176 KB Output is correct
44 Correct 131 ms 51024 KB Output is correct
45 Correct 127 ms 51024 KB Output is correct
46 Correct 134 ms 51028 KB Output is correct
47 Correct 130 ms 51024 KB Output is correct
48 Correct 130 ms 51016 KB Output is correct
49 Correct 133 ms 50924 KB Output is correct
50 Correct 132 ms 50932 KB Output is correct
51 Correct 138 ms 50988 KB Output is correct
52 Correct 132 ms 51024 KB Output is correct
53 Correct 134 ms 51024 KB Output is correct
54 Correct 139 ms 51028 KB Output is correct
55 Correct 143 ms 51020 KB Output is correct
56 Correct 126 ms 51024 KB Output is correct
57 Correct 130 ms 50920 KB Output is correct
58 Correct 130 ms 50984 KB Output is correct
59 Correct 124 ms 50860 KB Output is correct
60 Correct 129 ms 50920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 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 472 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 1 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 4 ms 2140 KB Output is correct
14 Correct 33 ms 17164 KB Output is correct
15 Correct 69 ms 33744 KB Output is correct
16 Correct 122 ms 50456 KB Output is correct
17 Correct 103 ms 50260 KB Output is correct
18 Correct 101 ms 50248 KB Output is correct
19 Correct 98 ms 50260 KB Output is correct
20 Correct 99 ms 50240 KB Output is correct
21 Correct 99 ms 50460 KB Output is correct
22 Correct 100 ms 50488 KB Output is correct
23 Correct 107 ms 50284 KB Output is correct
24 Correct 97 ms 50240 KB Output is correct
25 Correct 102 ms 50420 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 4 ms 2136 KB Output is correct
29 Correct 33 ms 17224 KB Output is correct
30 Correct 69 ms 33884 KB Output is correct
31 Correct 108 ms 50512 KB Output is correct
32 Correct 110 ms 50516 KB Output is correct
33 Correct 103 ms 50512 KB Output is correct
34 Correct 106 ms 50516 KB Output is correct
35 Correct 104 ms 50376 KB Output is correct
36 Correct 102 ms 50512 KB Output is correct
37 Correct 101 ms 50520 KB Output is correct
38 Correct 105 ms 50516 KB Output is correct
39 Correct 105 ms 50564 KB Output is correct
40 Correct 106 ms 50520 KB Output is correct
41 Correct 1 ms 600 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 4 ms 2140 KB Output is correct
45 Correct 5 ms 2140 KB Output is correct
46 Correct 6 ms 2140 KB Output is correct
47 Correct 20 ms 8796 KB Output is correct
48 Correct 24 ms 9136 KB Output is correct
49 Correct 44 ms 17244 KB Output is correct
50 Correct 91 ms 34044 KB Output is correct
51 Correct 129 ms 50944 KB Output is correct
52 Correct 136 ms 51028 KB Output is correct
53 Correct 135 ms 51176 KB Output is correct
54 Correct 131 ms 51024 KB Output is correct
55 Correct 127 ms 51024 KB Output is correct
56 Correct 134 ms 51028 KB Output is correct
57 Correct 130 ms 51024 KB Output is correct
58 Correct 130 ms 51016 KB Output is correct
59 Correct 133 ms 50924 KB Output is correct
60 Correct 132 ms 50932 KB Output is correct
61 Correct 138 ms 50988 KB Output is correct
62 Correct 132 ms 51024 KB Output is correct
63 Correct 134 ms 51024 KB Output is correct
64 Correct 139 ms 51028 KB Output is correct
65 Correct 143 ms 51020 KB Output is correct
66 Correct 126 ms 51024 KB Output is correct
67 Correct 130 ms 50920 KB Output is correct
68 Correct 130 ms 50984 KB Output is correct
69 Correct 124 ms 50860 KB Output is correct
70 Correct 129 ms 50920 KB Output is correct
71 Correct 25 ms 9048 KB Output is correct
72 Correct 25 ms 9048 KB Output is correct
73 Correct 27 ms 9052 KB Output is correct
74 Correct 25 ms 9052 KB Output is correct
75 Correct 30 ms 9088 KB Output is correct
76 Correct 55 ms 17496 KB Output is correct
77 Correct 54 ms 17676 KB Output is correct
78 Correct 63 ms 17500 KB Output is correct
79 Correct 59 ms 17752 KB Output is correct
80 Correct 64 ms 17496 KB Output is correct
81 Correct 123 ms 34644 KB Output is correct
82 Correct 127 ms 34668 KB Output is correct
83 Correct 122 ms 34640 KB Output is correct
84 Correct 128 ms 34672 KB Output is correct
85 Correct 125 ms 34860 KB Output is correct
86 Correct 147 ms 35484 KB Output is correct
87 Correct 148 ms 35480 KB Output is correct
88 Correct 152 ms 35416 KB Output is correct
89 Correct 151 ms 35408 KB Output is correct
90 Correct 160 ms 35408 KB Output is correct
91 Correct 351 ms 56400 KB Output is correct
92 Correct 348 ms 56612 KB Output is correct
93 Correct 345 ms 56464 KB Output is correct
94 Correct 344 ms 56388 KB Output is correct
95 Correct 352 ms 56656 KB Output is correct
96 Correct 432 ms 61776 KB Output is correct
97 Correct 436 ms 62032 KB Output is correct
98 Correct 434 ms 61780 KB Output is correct
99 Correct 424 ms 61520 KB Output is correct
100 Correct 426 ms 61524 KB Output is correct
101 Correct 417 ms 61520 KB Output is correct
102 Correct 404 ms 61580 KB Output is correct
103 Correct 415 ms 61520 KB Output is correct
104 Correct 410 ms 61520 KB Output is correct
105 Correct 420 ms 61524 KB Output is correct
106 Correct 458 ms 64084 KB Output is correct
107 Correct 460 ms 64084 KB Output is correct
108 Correct 452 ms 64080 KB Output is correct
109 Correct 427 ms 64256 KB Output is correct
110 Correct 460 ms 64312 KB Output is correct
111 Correct 453 ms 64080 KB Output is correct
112 Correct 447 ms 64080 KB Output is correct
113 Correct 457 ms 64340 KB Output is correct
114 Correct 481 ms 64120 KB Output is correct
115 Correct 445 ms 64084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 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 472 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 1 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 4 ms 2140 KB Output is correct
14 Correct 33 ms 17164 KB Output is correct
15 Correct 69 ms 33744 KB Output is correct
16 Correct 122 ms 50456 KB Output is correct
17 Correct 103 ms 50260 KB Output is correct
18 Correct 101 ms 50248 KB Output is correct
19 Correct 98 ms 50260 KB Output is correct
20 Correct 99 ms 50240 KB Output is correct
21 Correct 99 ms 50460 KB Output is correct
22 Correct 100 ms 50488 KB Output is correct
23 Correct 107 ms 50284 KB Output is correct
24 Correct 97 ms 50240 KB Output is correct
25 Correct 102 ms 50420 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 4 ms 2136 KB Output is correct
29 Correct 33 ms 17224 KB Output is correct
30 Correct 69 ms 33884 KB Output is correct
31 Correct 108 ms 50512 KB Output is correct
32 Correct 110 ms 50516 KB Output is correct
33 Correct 103 ms 50512 KB Output is correct
34 Correct 106 ms 50516 KB Output is correct
35 Correct 104 ms 50376 KB Output is correct
36 Correct 102 ms 50512 KB Output is correct
37 Correct 101 ms 50520 KB Output is correct
38 Correct 105 ms 50516 KB Output is correct
39 Correct 105 ms 50564 KB Output is correct
40 Correct 106 ms 50520 KB Output is correct
41 Correct 1 ms 600 KB Output is correct
42 Correct 1 ms 604 KB Output is correct
43 Correct 1 ms 604 KB Output is correct
44 Correct 4 ms 2140 KB Output is correct
45 Correct 5 ms 2140 KB Output is correct
46 Correct 6 ms 2140 KB Output is correct
47 Correct 20 ms 8796 KB Output is correct
48 Correct 24 ms 9136 KB Output is correct
49 Correct 44 ms 17244 KB Output is correct
50 Correct 91 ms 34044 KB Output is correct
51 Correct 129 ms 50944 KB Output is correct
52 Correct 136 ms 51028 KB Output is correct
53 Correct 135 ms 51176 KB Output is correct
54 Correct 131 ms 51024 KB Output is correct
55 Correct 127 ms 51024 KB Output is correct
56 Correct 134 ms 51028 KB Output is correct
57 Correct 130 ms 51024 KB Output is correct
58 Correct 130 ms 51016 KB Output is correct
59 Correct 133 ms 50924 KB Output is correct
60 Correct 132 ms 50932 KB Output is correct
61 Correct 138 ms 50988 KB Output is correct
62 Correct 132 ms 51024 KB Output is correct
63 Correct 134 ms 51024 KB Output is correct
64 Correct 139 ms 51028 KB Output is correct
65 Correct 143 ms 51020 KB Output is correct
66 Correct 126 ms 51024 KB Output is correct
67 Correct 130 ms 50920 KB Output is correct
68 Correct 130 ms 50984 KB Output is correct
69 Correct 124 ms 50860 KB Output is correct
70 Correct 129 ms 50920 KB Output is correct
71 Correct 25 ms 9048 KB Output is correct
72 Correct 25 ms 9048 KB Output is correct
73 Correct 27 ms 9052 KB Output is correct
74 Correct 25 ms 9052 KB Output is correct
75 Correct 30 ms 9088 KB Output is correct
76 Correct 55 ms 17496 KB Output is correct
77 Correct 54 ms 17676 KB Output is correct
78 Correct 63 ms 17500 KB Output is correct
79 Correct 59 ms 17752 KB Output is correct
80 Correct 64 ms 17496 KB Output is correct
81 Correct 123 ms 34644 KB Output is correct
82 Correct 127 ms 34668 KB Output is correct
83 Correct 122 ms 34640 KB Output is correct
84 Correct 128 ms 34672 KB Output is correct
85 Correct 125 ms 34860 KB Output is correct
86 Correct 147 ms 35484 KB Output is correct
87 Correct 148 ms 35480 KB Output is correct
88 Correct 152 ms 35416 KB Output is correct
89 Correct 151 ms 35408 KB Output is correct
90 Correct 160 ms 35408 KB Output is correct
91 Correct 351 ms 56400 KB Output is correct
92 Correct 348 ms 56612 KB Output is correct
93 Correct 345 ms 56464 KB Output is correct
94 Correct 344 ms 56388 KB Output is correct
95 Correct 352 ms 56656 KB Output is correct
96 Correct 432 ms 61776 KB Output is correct
97 Correct 436 ms 62032 KB Output is correct
98 Correct 434 ms 61780 KB Output is correct
99 Correct 424 ms 61520 KB Output is correct
100 Correct 426 ms 61524 KB Output is correct
101 Correct 417 ms 61520 KB Output is correct
102 Correct 404 ms 61580 KB Output is correct
103 Correct 415 ms 61520 KB Output is correct
104 Correct 410 ms 61520 KB Output is correct
105 Correct 420 ms 61524 KB Output is correct
106 Correct 458 ms 64084 KB Output is correct
107 Correct 460 ms 64084 KB Output is correct
108 Correct 452 ms 64080 KB Output is correct
109 Correct 427 ms 64256 KB Output is correct
110 Correct 460 ms 64312 KB Output is correct
111 Correct 453 ms 64080 KB Output is correct
112 Correct 447 ms 64080 KB Output is correct
113 Correct 457 ms 64340 KB Output is correct
114 Correct 481 ms 64120 KB Output is correct
115 Correct 445 ms 64084 KB Output is correct
116 Correct 654 ms 3924 KB Output is correct
117 Correct 787 ms 3924 KB Output is correct
118 Correct 1745 ms 10884 KB Output is correct
119 Correct 2661 ms 11020 KB Output is correct
120 Correct 3062 ms 11084 KB Output is correct
121 Execution timed out 7071 ms 18892 KB Time limit exceeded
122 Halted 0 ms 0 KB -