답안 #1050085

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

const int MX=300000,sq=1350;
int MID,mx;

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

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

ll fast2_query(int l, int r, int idx, int x, int val) {
    if (x>r) return s[idx].sr+s[idx].sum*(ll)(x-r);
    if (x<l) return s[idx].sl+s[idx].sum*(ll)(l-x);
    if (l==r) {
        s[idx]={val,val,val,1};
        return val;
    }
    int mid=(l+r)/2;
    ll res1=fast2_query(l,mid,2*idx,x,val);
    ll res2=fast2_query(mid+1,r,2*idx+1,x,val);
    s[idx]=merge(s[2*idx],s[2*idx+1]);
    return res1+res2;
}

int have[MX+5],idx_seq=-1;
vector<int> seq;
deque<int> dq[MX+5];
ll ans=0;

void add(int val) {

    if (!have[val]) {
        int pos=seq[++idx_seq];
        dq[1].push_back(pos);
        ans+=fast2_query(1,mx,1,pos,1);

    } else {
        int max_pos=dq[have[val]].front();
        dq[have[val]].pop_front();
        dq[have[val]+1].push_back(max_pos);
        ans+=fast2_query(1,mx,1,max_pos,have[val]+1);
    }

    ++have[val];
}

void del(int val) {

    int min_pos=dq[have[val]].back();
    dq[have[val]].pop_back();

    if (have[val]==1) --idx_seq;
    else dq[have[val]-1].push_front(min_pos);

    --have[val];
    ans-=(fast2_query(1,mx,1,min_pos,have[val])+1);
}

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

struct Query {
    int l,r,idx;
    bool operator<(Query other) const {
        return pii(l/sq,((l/sq)&1) ? -r : +r)<pii(other.l/sq,((other.l/sq)&1) ? -other.r : +other.r);
    }
};

vector<Query> ques;
ll Ans[50005];

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    set<int> ss;
    cin>>n>>q;
    for (int i=0; i<n; ++i) cin>>a[i], ss.insert(a[i]);
    mx=ss.size();
    mx+=(mx%2);

    MID=mx/2;
    int ll=MID,rr=MID;
    seq.push_back(MID);
    for (int i=1; i<mx; ++i) {
        if (i%2==0) seq.push_back(--ll);
        else seq.push_back(++rr);
    }

    for (int i=0; i<q; ++i) {
        int l,r; cin>>l>>r;
        ques.push_back({--l,--r,i});
    }

    sort(ques.begin(),ques.end());

    int cur_l=0;
    int cur_r=-1;

    for (auto q : ques) {
        while (cur_l>q.l) add(a[--cur_l]);
        while (cur_r<q.r) add(a[++cur_r]);
        while (cur_l<q.l) del(a[cur_l++]);
        while (cur_r>q.r) del(a[cur_r--]);
        Ans[q.idx]=ans;
    }

    for (int i=0; i<q; ++i) cout<<Ans[i]<<"\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 205552 KB Output is correct
2 Correct 67 ms 205416 KB Output is correct
3 Correct 64 ms 205408 KB Output is correct
4 Correct 74 ms 205404 KB Output is correct
5 Correct 68 ms 205396 KB Output is correct
6 Correct 66 ms 205396 KB Output is correct
7 Correct 100 ms 205488 KB Output is correct
8 Correct 70 ms 205556 KB Output is correct
9 Correct 77 ms 205472 KB Output is correct
10 Correct 66 ms 205360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 205396 KB Output is correct
2 Correct 65 ms 205360 KB Output is correct
3 Correct 67 ms 205512 KB Output is correct
4 Correct 85 ms 205652 KB Output is correct
5 Correct 87 ms 205988 KB Output is correct
6 Correct 96 ms 206164 KB Output is correct
7 Correct 116 ms 206164 KB Output is correct
8 Correct 104 ms 206144 KB Output is correct
9 Correct 110 ms 206088 KB Output is correct
10 Correct 115 ms 206164 KB Output is correct
11 Correct 114 ms 205996 KB Output is correct
12 Correct 119 ms 206160 KB Output is correct
13 Correct 120 ms 206160 KB Output is correct
14 Correct 106 ms 206164 KB Output is correct
15 Correct 106 ms 206164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 205396 KB Output is correct
2 Correct 65 ms 205360 KB Output is correct
3 Correct 67 ms 205512 KB Output is correct
4 Correct 85 ms 205652 KB Output is correct
5 Correct 87 ms 205988 KB Output is correct
6 Correct 96 ms 206164 KB Output is correct
7 Correct 116 ms 206164 KB Output is correct
8 Correct 104 ms 206144 KB Output is correct
9 Correct 110 ms 206088 KB Output is correct
10 Correct 115 ms 206164 KB Output is correct
11 Correct 114 ms 205996 KB Output is correct
12 Correct 119 ms 206160 KB Output is correct
13 Correct 120 ms 206160 KB Output is correct
14 Correct 106 ms 206164 KB Output is correct
15 Correct 106 ms 206164 KB Output is correct
16 Correct 81 ms 205396 KB Output is correct
17 Correct 76 ms 205592 KB Output is correct
18 Correct 78 ms 205396 KB Output is correct
19 Correct 84 ms 205648 KB Output is correct
20 Correct 97 ms 206020 KB Output is correct
21 Correct 115 ms 206164 KB Output is correct
22 Correct 118 ms 206164 KB Output is correct
23 Correct 116 ms 206164 KB Output is correct
24 Correct 110 ms 206264 KB Output is correct
25 Correct 111 ms 206160 KB Output is correct
26 Correct 106 ms 206164 KB Output is correct
27 Correct 112 ms 206292 KB Output is correct
28 Correct 109 ms 206120 KB Output is correct
29 Correct 110 ms 206340 KB Output is correct
30 Correct 113 ms 206164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 75 ms 205396 KB Output is correct
2 Correct 65 ms 205360 KB Output is correct
3 Correct 67 ms 205512 KB Output is correct
4 Correct 85 ms 205652 KB Output is correct
5 Correct 87 ms 205988 KB Output is correct
6 Correct 96 ms 206164 KB Output is correct
7 Correct 116 ms 206164 KB Output is correct
8 Correct 104 ms 206144 KB Output is correct
9 Correct 110 ms 206088 KB Output is correct
10 Correct 115 ms 206164 KB Output is correct
11 Correct 114 ms 205996 KB Output is correct
12 Correct 119 ms 206160 KB Output is correct
13 Correct 120 ms 206160 KB Output is correct
14 Correct 106 ms 206164 KB Output is correct
15 Correct 106 ms 206164 KB Output is correct
16 Correct 81 ms 205396 KB Output is correct
17 Correct 76 ms 205592 KB Output is correct
18 Correct 78 ms 205396 KB Output is correct
19 Correct 84 ms 205648 KB Output is correct
20 Correct 97 ms 206020 KB Output is correct
21 Correct 115 ms 206164 KB Output is correct
22 Correct 118 ms 206164 KB Output is correct
23 Correct 116 ms 206164 KB Output is correct
24 Correct 110 ms 206264 KB Output is correct
25 Correct 111 ms 206160 KB Output is correct
26 Correct 106 ms 206164 KB Output is correct
27 Correct 112 ms 206292 KB Output is correct
28 Correct 109 ms 206120 KB Output is correct
29 Correct 110 ms 206340 KB Output is correct
30 Correct 113 ms 206164 KB Output is correct
31 Correct 77 ms 205388 KB Output is correct
32 Correct 76 ms 205392 KB Output is correct
33 Correct 78 ms 205512 KB Output is correct
34 Correct 96 ms 205392 KB Output is correct
35 Correct 78 ms 205392 KB Output is correct
36 Correct 86 ms 205652 KB Output is correct
37 Correct 94 ms 205652 KB Output is correct
38 Correct 96 ms 205648 KB Output is correct
39 Correct 110 ms 206024 KB Output is correct
40 Correct 118 ms 206392 KB Output is correct
41 Correct 139 ms 206780 KB Output is correct
42 Correct 133 ms 206760 KB Output is correct
43 Correct 139 ms 206932 KB Output is correct
44 Correct 131 ms 206776 KB Output is correct
45 Correct 127 ms 206676 KB Output is correct
46 Correct 140 ms 206780 KB Output is correct
47 Correct 138 ms 206676 KB Output is correct
48 Correct 139 ms 206672 KB Output is correct
49 Correct 137 ms 206672 KB Output is correct
50 Correct 138 ms 206764 KB Output is correct
51 Correct 139 ms 206788 KB Output is correct
52 Correct 135 ms 206672 KB Output is correct
53 Correct 134 ms 206608 KB Output is correct
54 Correct 138 ms 206608 KB Output is correct
55 Correct 144 ms 206736 KB Output is correct
56 Correct 147 ms 206796 KB Output is correct
57 Correct 143 ms 206652 KB Output is correct
58 Correct 137 ms 206624 KB Output is correct
59 Correct 135 ms 206676 KB Output is correct
60 Correct 159 ms 206624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 205552 KB Output is correct
2 Correct 67 ms 205416 KB Output is correct
3 Correct 64 ms 205408 KB Output is correct
4 Correct 74 ms 205404 KB Output is correct
5 Correct 68 ms 205396 KB Output is correct
6 Correct 66 ms 205396 KB Output is correct
7 Correct 100 ms 205488 KB Output is correct
8 Correct 70 ms 205556 KB Output is correct
9 Correct 77 ms 205472 KB Output is correct
10 Correct 66 ms 205360 KB Output is correct
11 Correct 75 ms 205396 KB Output is correct
12 Correct 65 ms 205360 KB Output is correct
13 Correct 67 ms 205512 KB Output is correct
14 Correct 85 ms 205652 KB Output is correct
15 Correct 87 ms 205988 KB Output is correct
16 Correct 96 ms 206164 KB Output is correct
17 Correct 116 ms 206164 KB Output is correct
18 Correct 104 ms 206144 KB Output is correct
19 Correct 110 ms 206088 KB Output is correct
20 Correct 115 ms 206164 KB Output is correct
21 Correct 114 ms 205996 KB Output is correct
22 Correct 119 ms 206160 KB Output is correct
23 Correct 120 ms 206160 KB Output is correct
24 Correct 106 ms 206164 KB Output is correct
25 Correct 106 ms 206164 KB Output is correct
26 Correct 81 ms 205396 KB Output is correct
27 Correct 76 ms 205592 KB Output is correct
28 Correct 78 ms 205396 KB Output is correct
29 Correct 84 ms 205648 KB Output is correct
30 Correct 97 ms 206020 KB Output is correct
31 Correct 115 ms 206164 KB Output is correct
32 Correct 118 ms 206164 KB Output is correct
33 Correct 116 ms 206164 KB Output is correct
34 Correct 110 ms 206264 KB Output is correct
35 Correct 111 ms 206160 KB Output is correct
36 Correct 106 ms 206164 KB Output is correct
37 Correct 112 ms 206292 KB Output is correct
38 Correct 109 ms 206120 KB Output is correct
39 Correct 110 ms 206340 KB Output is correct
40 Correct 113 ms 206164 KB Output is correct
41 Correct 77 ms 205388 KB Output is correct
42 Correct 76 ms 205392 KB Output is correct
43 Correct 78 ms 205512 KB Output is correct
44 Correct 96 ms 205392 KB Output is correct
45 Correct 78 ms 205392 KB Output is correct
46 Correct 86 ms 205652 KB Output is correct
47 Correct 94 ms 205652 KB Output is correct
48 Correct 96 ms 205648 KB Output is correct
49 Correct 110 ms 206024 KB Output is correct
50 Correct 118 ms 206392 KB Output is correct
51 Correct 139 ms 206780 KB Output is correct
52 Correct 133 ms 206760 KB Output is correct
53 Correct 139 ms 206932 KB Output is correct
54 Correct 131 ms 206776 KB Output is correct
55 Correct 127 ms 206676 KB Output is correct
56 Correct 140 ms 206780 KB Output is correct
57 Correct 138 ms 206676 KB Output is correct
58 Correct 139 ms 206672 KB Output is correct
59 Correct 137 ms 206672 KB Output is correct
60 Correct 138 ms 206764 KB Output is correct
61 Correct 139 ms 206788 KB Output is correct
62 Correct 135 ms 206672 KB Output is correct
63 Correct 134 ms 206608 KB Output is correct
64 Correct 138 ms 206608 KB Output is correct
65 Correct 144 ms 206736 KB Output is correct
66 Correct 147 ms 206796 KB Output is correct
67 Correct 143 ms 206652 KB Output is correct
68 Correct 137 ms 206624 KB Output is correct
69 Correct 135 ms 206676 KB Output is correct
70 Correct 159 ms 206624 KB Output is correct
71 Correct 83 ms 205908 KB Output is correct
72 Correct 84 ms 205728 KB Output is correct
73 Correct 79 ms 205872 KB Output is correct
74 Correct 84 ms 205908 KB Output is correct
75 Correct 88 ms 205904 KB Output is correct
76 Correct 109 ms 206224 KB Output is correct
77 Correct 109 ms 206144 KB Output is correct
78 Correct 106 ms 206212 KB Output is correct
79 Correct 103 ms 206144 KB Output is correct
80 Correct 104 ms 206356 KB Output is correct
81 Correct 132 ms 208976 KB Output is correct
82 Correct 137 ms 208980 KB Output is correct
83 Correct 139 ms 209232 KB Output is correct
84 Correct 140 ms 209028 KB Output is correct
85 Correct 142 ms 208976 KB Output is correct
86 Correct 134 ms 209236 KB Output is correct
87 Correct 134 ms 209236 KB Output is correct
88 Correct 129 ms 209184 KB Output is correct
89 Correct 141 ms 209236 KB Output is correct
90 Correct 132 ms 209052 KB Output is correct
91 Correct 206 ms 220884 KB Output is correct
92 Correct 202 ms 220872 KB Output is correct
93 Correct 194 ms 220876 KB Output is correct
94 Correct 196 ms 220876 KB Output is correct
95 Correct 202 ms 220880 KB Output is correct
96 Correct 256 ms 234688 KB Output is correct
97 Correct 218 ms 234688 KB Output is correct
98 Correct 226 ms 234840 KB Output is correct
99 Correct 215 ms 234696 KB Output is correct
100 Correct 235 ms 234700 KB Output is correct
101 Correct 244 ms 234696 KB Output is correct
102 Correct 226 ms 234888 KB Output is correct
103 Correct 220 ms 234840 KB Output is correct
104 Correct 238 ms 234684 KB Output is correct
105 Correct 225 ms 234688 KB Output is correct
106 Correct 238 ms 237504 KB Output is correct
107 Correct 234 ms 237872 KB Output is correct
108 Correct 230 ms 237588 KB Output is correct
109 Correct 238 ms 237504 KB Output is correct
110 Correct 232 ms 237508 KB Output is correct
111 Correct 235 ms 237476 KB Output is correct
112 Correct 224 ms 237512 KB Output is correct
113 Correct 237 ms 237456 KB Output is correct
114 Correct 236 ms 237500 KB Output is correct
115 Correct 227 ms 237504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 205552 KB Output is correct
2 Correct 67 ms 205416 KB Output is correct
3 Correct 64 ms 205408 KB Output is correct
4 Correct 74 ms 205404 KB Output is correct
5 Correct 68 ms 205396 KB Output is correct
6 Correct 66 ms 205396 KB Output is correct
7 Correct 100 ms 205488 KB Output is correct
8 Correct 70 ms 205556 KB Output is correct
9 Correct 77 ms 205472 KB Output is correct
10 Correct 66 ms 205360 KB Output is correct
11 Correct 75 ms 205396 KB Output is correct
12 Correct 65 ms 205360 KB Output is correct
13 Correct 67 ms 205512 KB Output is correct
14 Correct 85 ms 205652 KB Output is correct
15 Correct 87 ms 205988 KB Output is correct
16 Correct 96 ms 206164 KB Output is correct
17 Correct 116 ms 206164 KB Output is correct
18 Correct 104 ms 206144 KB Output is correct
19 Correct 110 ms 206088 KB Output is correct
20 Correct 115 ms 206164 KB Output is correct
21 Correct 114 ms 205996 KB Output is correct
22 Correct 119 ms 206160 KB Output is correct
23 Correct 120 ms 206160 KB Output is correct
24 Correct 106 ms 206164 KB Output is correct
25 Correct 106 ms 206164 KB Output is correct
26 Correct 81 ms 205396 KB Output is correct
27 Correct 76 ms 205592 KB Output is correct
28 Correct 78 ms 205396 KB Output is correct
29 Correct 84 ms 205648 KB Output is correct
30 Correct 97 ms 206020 KB Output is correct
31 Correct 115 ms 206164 KB Output is correct
32 Correct 118 ms 206164 KB Output is correct
33 Correct 116 ms 206164 KB Output is correct
34 Correct 110 ms 206264 KB Output is correct
35 Correct 111 ms 206160 KB Output is correct
36 Correct 106 ms 206164 KB Output is correct
37 Correct 112 ms 206292 KB Output is correct
38 Correct 109 ms 206120 KB Output is correct
39 Correct 110 ms 206340 KB Output is correct
40 Correct 113 ms 206164 KB Output is correct
41 Correct 77 ms 205388 KB Output is correct
42 Correct 76 ms 205392 KB Output is correct
43 Correct 78 ms 205512 KB Output is correct
44 Correct 96 ms 205392 KB Output is correct
45 Correct 78 ms 205392 KB Output is correct
46 Correct 86 ms 205652 KB Output is correct
47 Correct 94 ms 205652 KB Output is correct
48 Correct 96 ms 205648 KB Output is correct
49 Correct 110 ms 206024 KB Output is correct
50 Correct 118 ms 206392 KB Output is correct
51 Correct 139 ms 206780 KB Output is correct
52 Correct 133 ms 206760 KB Output is correct
53 Correct 139 ms 206932 KB Output is correct
54 Correct 131 ms 206776 KB Output is correct
55 Correct 127 ms 206676 KB Output is correct
56 Correct 140 ms 206780 KB Output is correct
57 Correct 138 ms 206676 KB Output is correct
58 Correct 139 ms 206672 KB Output is correct
59 Correct 137 ms 206672 KB Output is correct
60 Correct 138 ms 206764 KB Output is correct
61 Correct 139 ms 206788 KB Output is correct
62 Correct 135 ms 206672 KB Output is correct
63 Correct 134 ms 206608 KB Output is correct
64 Correct 138 ms 206608 KB Output is correct
65 Correct 144 ms 206736 KB Output is correct
66 Correct 147 ms 206796 KB Output is correct
67 Correct 143 ms 206652 KB Output is correct
68 Correct 137 ms 206624 KB Output is correct
69 Correct 135 ms 206676 KB Output is correct
70 Correct 159 ms 206624 KB Output is correct
71 Correct 83 ms 205908 KB Output is correct
72 Correct 84 ms 205728 KB Output is correct
73 Correct 79 ms 205872 KB Output is correct
74 Correct 84 ms 205908 KB Output is correct
75 Correct 88 ms 205904 KB Output is correct
76 Correct 109 ms 206224 KB Output is correct
77 Correct 109 ms 206144 KB Output is correct
78 Correct 106 ms 206212 KB Output is correct
79 Correct 103 ms 206144 KB Output is correct
80 Correct 104 ms 206356 KB Output is correct
81 Correct 132 ms 208976 KB Output is correct
82 Correct 137 ms 208980 KB Output is correct
83 Correct 139 ms 209232 KB Output is correct
84 Correct 140 ms 209028 KB Output is correct
85 Correct 142 ms 208976 KB Output is correct
86 Correct 134 ms 209236 KB Output is correct
87 Correct 134 ms 209236 KB Output is correct
88 Correct 129 ms 209184 KB Output is correct
89 Correct 141 ms 209236 KB Output is correct
90 Correct 132 ms 209052 KB Output is correct
91 Correct 206 ms 220884 KB Output is correct
92 Correct 202 ms 220872 KB Output is correct
93 Correct 194 ms 220876 KB Output is correct
94 Correct 196 ms 220876 KB Output is correct
95 Correct 202 ms 220880 KB Output is correct
96 Correct 256 ms 234688 KB Output is correct
97 Correct 218 ms 234688 KB Output is correct
98 Correct 226 ms 234840 KB Output is correct
99 Correct 215 ms 234696 KB Output is correct
100 Correct 235 ms 234700 KB Output is correct
101 Correct 244 ms 234696 KB Output is correct
102 Correct 226 ms 234888 KB Output is correct
103 Correct 220 ms 234840 KB Output is correct
104 Correct 238 ms 234684 KB Output is correct
105 Correct 225 ms 234688 KB Output is correct
106 Correct 238 ms 237504 KB Output is correct
107 Correct 234 ms 237872 KB Output is correct
108 Correct 230 ms 237588 KB Output is correct
109 Correct 238 ms 237504 KB Output is correct
110 Correct 232 ms 237508 KB Output is correct
111 Correct 235 ms 237476 KB Output is correct
112 Correct 224 ms 237512 KB Output is correct
113 Correct 237 ms 237456 KB Output is correct
114 Correct 236 ms 237500 KB Output is correct
115 Correct 227 ms 237504 KB Output is correct
116 Correct 1060 ms 207308 KB Output is correct
117 Correct 1248 ms 207304 KB Output is correct
118 Correct 1446 ms 207568 KB Output is correct
119 Correct 1991 ms 207740 KB Output is correct
120 Correct 2168 ms 207816 KB Output is correct
121 Correct 2451 ms 208080 KB Output is correct
122 Correct 2684 ms 208116 KB Output is correct
123 Correct 3591 ms 208448 KB Output is correct
124 Correct 4229 ms 208864 KB Output is correct
125 Correct 4499 ms 211216 KB Output is correct
126 Correct 4386 ms 208840 KB Output is correct
127 Correct 4632 ms 208748 KB Output is correct
128 Correct 4551 ms 208740 KB Output is correct
129 Correct 4574 ms 208748 KB Output is correct
130 Correct 4440 ms 208744 KB Output is correct
131 Correct 5121 ms 208792 KB Output is correct
132 Correct 4940 ms 208796 KB Output is correct
133 Correct 4895 ms 208844 KB Output is correct
134 Correct 4754 ms 208788 KB Output is correct
135 Correct 4858 ms 208800 KB Output is correct
136 Correct 4893 ms 208788 KB Output is correct
137 Correct 5022 ms 208808 KB Output is correct
138 Correct 4886 ms 208844 KB Output is correct
139 Correct 4827 ms 208788 KB Output is correct
140 Correct 4738 ms 208788 KB Output is correct
141 Correct 5357 ms 208856 KB Output is correct
142 Correct 5293 ms 208848 KB Output is correct
143 Correct 5373 ms 208844 KB Output is correct
144 Correct 5288 ms 208864 KB Output is correct
145 Correct 5400 ms 208844 KB Output is correct
146 Correct 6327 ms 209376 KB Output is correct
147 Correct 6436 ms 209372 KB Output is correct
148 Correct 6470 ms 209372 KB Output is correct
149 Correct 6595 ms 209352 KB Output is correct
150 Correct 6675 ms 209376 KB Output is correct
151 Correct 6690 ms 211728 KB Output is correct
152 Correct 6685 ms 211720 KB Output is correct
153 Correct 6808 ms 211712 KB Output is correct
154 Correct 6790 ms 211708 KB Output is correct
155 Correct 6935 ms 211808 KB Output is correct
156 Execution timed out 7066 ms 215484 KB Time limit exceeded
157 Halted 0 ms 0 KB -