답안 #1049302

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1049302 2024-08-08T16:09:13 Z PoonYaPat Diversity (CEOI21_diversity) C++14
64 / 100
185 ms 54864 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

//sl : increase from l
//sr : increase from r

const int mx=300000,MID=150000;

struct NN {
    ll sum,sl,sr,cnt;
} s[1<<22];

int ccnt[mx+5],posi[mx+5],num[mx+5];

NN merge(NN a, NN b) {
    NN res;
    res.sum=a.sum+b.sum;
    res.cnt=a.cnt+b.cnt;
    res.sl=a.sl+a.cnt*b.sum+b.sl;
    res.sr=b.sr+b.cnt*a.sum+a.sr;
    return res;
}

void update(int l, int r, int idx, int x) {
    if (x>r || x<l) return;
    if (l==r) s[idx]={ccnt[l],ccnt[l],ccnt[l],1};
    else {
        int mid=(l+r)/2;
        update(l,mid,2*idx,x);
        update(mid+1,r,2*idx+1,x);
        s[idx]=merge(s[2*idx],s[2*idx+1]);
    }
}

NN query(int l, int r, int idx, int x, int y) {
    if (x>r || y<l) return {0,0,0,0};
    if (x<=l && r<=y) return s[idx];
    int mid=(l+r)/2;
    return merge(query(l,mid,2*idx,x,y),query(mid+1,r,2*idx+1,x,y));
}

int level[mx+5],have[mx+5],idx_seq=-1; //range of box that has been used
vector<int> seq;
priority_queue<pii, vector<pii>, greater<pii>> pq[mx+5]; //(level,pos)

ll ans=0;

void sw(int a, int b) {
    swap(num[a],num[b]);
    posi[num[a]]=a;
    posi[num[b]]=b;
}

void add(int val) {

    if (!have[val]) {
        
        ++idx_seq;
        int pos=seq[idx_seq]; //the position of the seg-tree to be added
        ccnt[pos]=1;
        posi[val]=pos;
        num[pos]=val;
        update(1,mx,1,pos);
        pq[1].push(pii(level[pos],pos));
        ans+=query(1,mx,1,1,pos).sr+query(1,mx,1,pos,mx).sl-ccnt[pos];

    } else {

        int pos=posi[val], cnt=ccnt[pos]; //increase from cnt to cnt+1
        int max_pos=pq[cnt].top().second;
        sw(pos,max_pos);

        //increase from cnt to cnt+1
        pq[cnt].pop();
        pq[cnt+1].push(pii(level[max_pos],max_pos));
        ++ccnt[max_pos];
        update(1,mx,1,max_pos);
        ans+=query(1,mx,1,1,max_pos).sr+query(1,mx,1,max_pos,mx).sl-ccnt[max_pos];
    }

    ++have[val];
}

void del(int val) {

}

int n,q,a[mx+5];

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int ll=MID,rr=MID;
    level[MID]=0;
    seq.push_back(MID);
    for (int i=1; i<mx; ++i) {
        if (i%2==0) level[--ll]=i, seq.push_back(ll);
        else level[++rr]=i, seq.push_back(rr);
    }

    cin>>n>>q;
    for (int i=0; i<n; ++i) cin>>a[i];
    cin>>q>>q;

    for (int i=0; i<n; ++i) {
        add(a[i]);
        //cout<<"add "<<i<<" success -> "<<ans<<"\n";
    }

    cout<<ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 26320 KB Output is correct
2 Correct 4 ms 28212 KB Output is correct
3 Correct 4 ms 28440 KB Output is correct
4 Correct 4 ms 28372 KB Output is correct
5 Correct 4 ms 28372 KB Output is correct
6 Correct 4 ms 28372 KB Output is correct
7 Correct 4 ms 28372 KB Output is correct
8 Correct 4 ms 28372 KB Output is correct
9 Correct 4 ms 28372 KB Output is correct
10 Correct 4 ms 28372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 28372 KB Output is correct
2 Correct 4 ms 28372 KB Output is correct
3 Correct 9 ms 28372 KB Output is correct
4 Correct 41 ms 29380 KB Output is correct
5 Correct 78 ms 31176 KB Output is correct
6 Correct 117 ms 30928 KB Output is correct
7 Correct 107 ms 34112 KB Output is correct
8 Correct 120 ms 30528 KB Output is correct
9 Correct 121 ms 30616 KB Output is correct
10 Correct 118 ms 32048 KB Output is correct
11 Correct 136 ms 31208 KB Output is correct
12 Correct 120 ms 30660 KB Output is correct
13 Correct 123 ms 30792 KB Output is correct
14 Correct 116 ms 31428 KB Output is correct
15 Correct 115 ms 31256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 28372 KB Output is correct
2 Correct 4 ms 28372 KB Output is correct
3 Correct 9 ms 28372 KB Output is correct
4 Correct 41 ms 29380 KB Output is correct
5 Correct 78 ms 31176 KB Output is correct
6 Correct 117 ms 30928 KB Output is correct
7 Correct 107 ms 34112 KB Output is correct
8 Correct 120 ms 30528 KB Output is correct
9 Correct 121 ms 30616 KB Output is correct
10 Correct 118 ms 32048 KB Output is correct
11 Correct 136 ms 31208 KB Output is correct
12 Correct 120 ms 30660 KB Output is correct
13 Correct 123 ms 30792 KB Output is correct
14 Correct 116 ms 31428 KB Output is correct
15 Correct 115 ms 31256 KB Output is correct
16 Correct 4 ms 28368 KB Output is correct
17 Correct 5 ms 28264 KB Output is correct
18 Correct 8 ms 28456 KB Output is correct
19 Correct 44 ms 29120 KB Output is correct
20 Correct 84 ms 29900 KB Output is correct
21 Correct 122 ms 31076 KB Output is correct
22 Correct 124 ms 30404 KB Output is correct
23 Correct 119 ms 30732 KB Output is correct
24 Correct 146 ms 30432 KB Output is correct
25 Correct 139 ms 30656 KB Output is correct
26 Correct 129 ms 30412 KB Output is correct
27 Correct 145 ms 30664 KB Output is correct
28 Correct 119 ms 31428 KB Output is correct
29 Correct 122 ms 30368 KB Output is correct
30 Correct 141 ms 30660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 28372 KB Output is correct
2 Correct 4 ms 28372 KB Output is correct
3 Correct 9 ms 28372 KB Output is correct
4 Correct 41 ms 29380 KB Output is correct
5 Correct 78 ms 31176 KB Output is correct
6 Correct 117 ms 30928 KB Output is correct
7 Correct 107 ms 34112 KB Output is correct
8 Correct 120 ms 30528 KB Output is correct
9 Correct 121 ms 30616 KB Output is correct
10 Correct 118 ms 32048 KB Output is correct
11 Correct 136 ms 31208 KB Output is correct
12 Correct 120 ms 30660 KB Output is correct
13 Correct 123 ms 30792 KB Output is correct
14 Correct 116 ms 31428 KB Output is correct
15 Correct 115 ms 31256 KB Output is correct
16 Correct 4 ms 28368 KB Output is correct
17 Correct 5 ms 28264 KB Output is correct
18 Correct 8 ms 28456 KB Output is correct
19 Correct 44 ms 29120 KB Output is correct
20 Correct 84 ms 29900 KB Output is correct
21 Correct 122 ms 31076 KB Output is correct
22 Correct 124 ms 30404 KB Output is correct
23 Correct 119 ms 30732 KB Output is correct
24 Correct 146 ms 30432 KB Output is correct
25 Correct 139 ms 30656 KB Output is correct
26 Correct 129 ms 30412 KB Output is correct
27 Correct 145 ms 30664 KB Output is correct
28 Correct 119 ms 31428 KB Output is correct
29 Correct 122 ms 30368 KB Output is correct
30 Correct 141 ms 30660 KB Output is correct
31 Correct 4 ms 28368 KB Output is correct
32 Correct 4 ms 28372 KB Output is correct
33 Correct 5 ms 28628 KB Output is correct
34 Correct 9 ms 28372 KB Output is correct
35 Correct 9 ms 28372 KB Output is correct
36 Correct 9 ms 28372 KB Output is correct
37 Correct 27 ms 28628 KB Output is correct
38 Correct 28 ms 28628 KB Output is correct
39 Correct 53 ms 28960 KB Output is correct
40 Correct 102 ms 29392 KB Output is correct
41 Correct 145 ms 29652 KB Output is correct
42 Correct 146 ms 29756 KB Output is correct
43 Correct 146 ms 29648 KB Output is correct
44 Correct 145 ms 29648 KB Output is correct
45 Correct 151 ms 29652 KB Output is correct
46 Correct 145 ms 29648 KB Output is correct
47 Correct 146 ms 29648 KB Output is correct
48 Correct 148 ms 29648 KB Output is correct
49 Correct 154 ms 29652 KB Output is correct
50 Correct 152 ms 29752 KB Output is correct
51 Correct 158 ms 29652 KB Output is correct
52 Correct 148 ms 29652 KB Output is correct
53 Correct 147 ms 29652 KB Output is correct
54 Correct 165 ms 29648 KB Output is correct
55 Correct 146 ms 29696 KB Output is correct
56 Correct 156 ms 29648 KB Output is correct
57 Correct 163 ms 29652 KB Output is correct
58 Correct 151 ms 29652 KB Output is correct
59 Correct 145 ms 29652 KB Output is correct
60 Correct 148 ms 30408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 26320 KB Output is correct
2 Correct 4 ms 28212 KB Output is correct
3 Correct 4 ms 28440 KB Output is correct
4 Correct 4 ms 28372 KB Output is correct
5 Correct 4 ms 28372 KB Output is correct
6 Correct 4 ms 28372 KB Output is correct
7 Correct 4 ms 28372 KB Output is correct
8 Correct 4 ms 28372 KB Output is correct
9 Correct 4 ms 28372 KB Output is correct
10 Correct 4 ms 28372 KB Output is correct
11 Correct 4 ms 28372 KB Output is correct
12 Correct 4 ms 28372 KB Output is correct
13 Correct 9 ms 28372 KB Output is correct
14 Correct 41 ms 29380 KB Output is correct
15 Correct 78 ms 31176 KB Output is correct
16 Correct 117 ms 30928 KB Output is correct
17 Correct 107 ms 34112 KB Output is correct
18 Correct 120 ms 30528 KB Output is correct
19 Correct 121 ms 30616 KB Output is correct
20 Correct 118 ms 32048 KB Output is correct
21 Correct 136 ms 31208 KB Output is correct
22 Correct 120 ms 30660 KB Output is correct
23 Correct 123 ms 30792 KB Output is correct
24 Correct 116 ms 31428 KB Output is correct
25 Correct 115 ms 31256 KB Output is correct
26 Correct 4 ms 28368 KB Output is correct
27 Correct 5 ms 28264 KB Output is correct
28 Correct 8 ms 28456 KB Output is correct
29 Correct 44 ms 29120 KB Output is correct
30 Correct 84 ms 29900 KB Output is correct
31 Correct 122 ms 31076 KB Output is correct
32 Correct 124 ms 30404 KB Output is correct
33 Correct 119 ms 30732 KB Output is correct
34 Correct 146 ms 30432 KB Output is correct
35 Correct 139 ms 30656 KB Output is correct
36 Correct 129 ms 30412 KB Output is correct
37 Correct 145 ms 30664 KB Output is correct
38 Correct 119 ms 31428 KB Output is correct
39 Correct 122 ms 30368 KB Output is correct
40 Correct 141 ms 30660 KB Output is correct
41 Correct 4 ms 28368 KB Output is correct
42 Correct 4 ms 28372 KB Output is correct
43 Correct 5 ms 28628 KB Output is correct
44 Correct 9 ms 28372 KB Output is correct
45 Correct 9 ms 28372 KB Output is correct
46 Correct 9 ms 28372 KB Output is correct
47 Correct 27 ms 28628 KB Output is correct
48 Correct 28 ms 28628 KB Output is correct
49 Correct 53 ms 28960 KB Output is correct
50 Correct 102 ms 29392 KB Output is correct
51 Correct 145 ms 29652 KB Output is correct
52 Correct 146 ms 29756 KB Output is correct
53 Correct 146 ms 29648 KB Output is correct
54 Correct 145 ms 29648 KB Output is correct
55 Correct 151 ms 29652 KB Output is correct
56 Correct 145 ms 29648 KB Output is correct
57 Correct 146 ms 29648 KB Output is correct
58 Correct 148 ms 29648 KB Output is correct
59 Correct 154 ms 29652 KB Output is correct
60 Correct 152 ms 29752 KB Output is correct
61 Correct 158 ms 29652 KB Output is correct
62 Correct 148 ms 29652 KB Output is correct
63 Correct 147 ms 29652 KB Output is correct
64 Correct 165 ms 29648 KB Output is correct
65 Correct 146 ms 29696 KB Output is correct
66 Correct 156 ms 29648 KB Output is correct
67 Correct 163 ms 29652 KB Output is correct
68 Correct 151 ms 29652 KB Output is correct
69 Correct 145 ms 29652 KB Output is correct
70 Correct 148 ms 30408 KB Output is correct
71 Correct 30 ms 28628 KB Output is correct
72 Correct 29 ms 28628 KB Output is correct
73 Correct 29 ms 28880 KB Output is correct
74 Correct 29 ms 28624 KB Output is correct
75 Correct 29 ms 28628 KB Output is correct
76 Correct 61 ms 28972 KB Output is correct
77 Correct 56 ms 28884 KB Output is correct
78 Correct 56 ms 28884 KB Output is correct
79 Correct 55 ms 28880 KB Output is correct
80 Correct 66 ms 28884 KB Output is correct
81 Correct 121 ms 31660 KB Output is correct
82 Correct 111 ms 31640 KB Output is correct
83 Correct 109 ms 31684 KB Output is correct
84 Correct 114 ms 31440 KB Output is correct
85 Correct 114 ms 31624 KB Output is correct
86 Correct 111 ms 31944 KB Output is correct
87 Correct 111 ms 31912 KB Output is correct
88 Correct 111 ms 31940 KB Output is correct
89 Correct 116 ms 32200 KB Output is correct
90 Correct 112 ms 31784 KB Output is correct
91 Correct 163 ms 39116 KB Output is correct
92 Correct 168 ms 39012 KB Output is correct
93 Correct 166 ms 39032 KB Output is correct
94 Correct 185 ms 39108 KB Output is correct
95 Correct 163 ms 39108 KB Output is correct
96 Correct 163 ms 48612 KB Output is correct
97 Correct 160 ms 48324 KB Output is correct
98 Correct 158 ms 48324 KB Output is correct
99 Correct 160 ms 48584 KB Output is correct
100 Correct 161 ms 48612 KB Output is correct
101 Correct 162 ms 48328 KB Output is correct
102 Correct 156 ms 48584 KB Output is correct
103 Correct 161 ms 48324 KB Output is correct
104 Correct 158 ms 48328 KB Output is correct
105 Correct 176 ms 48328 KB Output is correct
106 Correct 156 ms 54864 KB Output is correct
107 Correct 151 ms 54616 KB Output is correct
108 Correct 150 ms 54724 KB Output is correct
109 Correct 153 ms 54700 KB Output is correct
110 Correct 158 ms 54652 KB Output is correct
111 Correct 153 ms 54724 KB Output is correct
112 Correct 151 ms 54632 KB Output is correct
113 Correct 171 ms 54728 KB Output is correct
114 Correct 159 ms 54728 KB Output is correct
115 Correct 153 ms 54772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 26320 KB Output is correct
2 Correct 4 ms 28212 KB Output is correct
3 Correct 4 ms 28440 KB Output is correct
4 Correct 4 ms 28372 KB Output is correct
5 Correct 4 ms 28372 KB Output is correct
6 Correct 4 ms 28372 KB Output is correct
7 Correct 4 ms 28372 KB Output is correct
8 Correct 4 ms 28372 KB Output is correct
9 Correct 4 ms 28372 KB Output is correct
10 Correct 4 ms 28372 KB Output is correct
11 Correct 4 ms 28372 KB Output is correct
12 Correct 4 ms 28372 KB Output is correct
13 Correct 9 ms 28372 KB Output is correct
14 Correct 41 ms 29380 KB Output is correct
15 Correct 78 ms 31176 KB Output is correct
16 Correct 117 ms 30928 KB Output is correct
17 Correct 107 ms 34112 KB Output is correct
18 Correct 120 ms 30528 KB Output is correct
19 Correct 121 ms 30616 KB Output is correct
20 Correct 118 ms 32048 KB Output is correct
21 Correct 136 ms 31208 KB Output is correct
22 Correct 120 ms 30660 KB Output is correct
23 Correct 123 ms 30792 KB Output is correct
24 Correct 116 ms 31428 KB Output is correct
25 Correct 115 ms 31256 KB Output is correct
26 Correct 4 ms 28368 KB Output is correct
27 Correct 5 ms 28264 KB Output is correct
28 Correct 8 ms 28456 KB Output is correct
29 Correct 44 ms 29120 KB Output is correct
30 Correct 84 ms 29900 KB Output is correct
31 Correct 122 ms 31076 KB Output is correct
32 Correct 124 ms 30404 KB Output is correct
33 Correct 119 ms 30732 KB Output is correct
34 Correct 146 ms 30432 KB Output is correct
35 Correct 139 ms 30656 KB Output is correct
36 Correct 129 ms 30412 KB Output is correct
37 Correct 145 ms 30664 KB Output is correct
38 Correct 119 ms 31428 KB Output is correct
39 Correct 122 ms 30368 KB Output is correct
40 Correct 141 ms 30660 KB Output is correct
41 Correct 4 ms 28368 KB Output is correct
42 Correct 4 ms 28372 KB Output is correct
43 Correct 5 ms 28628 KB Output is correct
44 Correct 9 ms 28372 KB Output is correct
45 Correct 9 ms 28372 KB Output is correct
46 Correct 9 ms 28372 KB Output is correct
47 Correct 27 ms 28628 KB Output is correct
48 Correct 28 ms 28628 KB Output is correct
49 Correct 53 ms 28960 KB Output is correct
50 Correct 102 ms 29392 KB Output is correct
51 Correct 145 ms 29652 KB Output is correct
52 Correct 146 ms 29756 KB Output is correct
53 Correct 146 ms 29648 KB Output is correct
54 Correct 145 ms 29648 KB Output is correct
55 Correct 151 ms 29652 KB Output is correct
56 Correct 145 ms 29648 KB Output is correct
57 Correct 146 ms 29648 KB Output is correct
58 Correct 148 ms 29648 KB Output is correct
59 Correct 154 ms 29652 KB Output is correct
60 Correct 152 ms 29752 KB Output is correct
61 Correct 158 ms 29652 KB Output is correct
62 Correct 148 ms 29652 KB Output is correct
63 Correct 147 ms 29652 KB Output is correct
64 Correct 165 ms 29648 KB Output is correct
65 Correct 146 ms 29696 KB Output is correct
66 Correct 156 ms 29648 KB Output is correct
67 Correct 163 ms 29652 KB Output is correct
68 Correct 151 ms 29652 KB Output is correct
69 Correct 145 ms 29652 KB Output is correct
70 Correct 148 ms 30408 KB Output is correct
71 Correct 30 ms 28628 KB Output is correct
72 Correct 29 ms 28628 KB Output is correct
73 Correct 29 ms 28880 KB Output is correct
74 Correct 29 ms 28624 KB Output is correct
75 Correct 29 ms 28628 KB Output is correct
76 Correct 61 ms 28972 KB Output is correct
77 Correct 56 ms 28884 KB Output is correct
78 Correct 56 ms 28884 KB Output is correct
79 Correct 55 ms 28880 KB Output is correct
80 Correct 66 ms 28884 KB Output is correct
81 Correct 121 ms 31660 KB Output is correct
82 Correct 111 ms 31640 KB Output is correct
83 Correct 109 ms 31684 KB Output is correct
84 Correct 114 ms 31440 KB Output is correct
85 Correct 114 ms 31624 KB Output is correct
86 Correct 111 ms 31944 KB Output is correct
87 Correct 111 ms 31912 KB Output is correct
88 Correct 111 ms 31940 KB Output is correct
89 Correct 116 ms 32200 KB Output is correct
90 Correct 112 ms 31784 KB Output is correct
91 Correct 163 ms 39116 KB Output is correct
92 Correct 168 ms 39012 KB Output is correct
93 Correct 166 ms 39032 KB Output is correct
94 Correct 185 ms 39108 KB Output is correct
95 Correct 163 ms 39108 KB Output is correct
96 Correct 163 ms 48612 KB Output is correct
97 Correct 160 ms 48324 KB Output is correct
98 Correct 158 ms 48324 KB Output is correct
99 Correct 160 ms 48584 KB Output is correct
100 Correct 161 ms 48612 KB Output is correct
101 Correct 162 ms 48328 KB Output is correct
102 Correct 156 ms 48584 KB Output is correct
103 Correct 161 ms 48324 KB Output is correct
104 Correct 158 ms 48328 KB Output is correct
105 Correct 176 ms 48328 KB Output is correct
106 Correct 156 ms 54864 KB Output is correct
107 Correct 151 ms 54616 KB Output is correct
108 Correct 150 ms 54724 KB Output is correct
109 Correct 153 ms 54700 KB Output is correct
110 Correct 158 ms 54652 KB Output is correct
111 Correct 153 ms 54724 KB Output is correct
112 Correct 151 ms 54632 KB Output is correct
113 Correct 171 ms 54728 KB Output is correct
114 Correct 159 ms 54728 KB Output is correct
115 Correct 153 ms 54772 KB Output is correct
116 Incorrect 9 ms 28368 KB Output isn't correct
117 Halted 0 ms 0 KB -