답안 #1049434

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1049434 2024-08-08T18:26:18 Z PoonYaPat Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 246028 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,sq=1350;

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

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;
deque<int> dq[mx+5];

ll ans=0;

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

int check=0;

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);
        dq[1].push_back(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=dq[cnt].front();
        sw(pos,max_pos);

        //increase from cnt to cnt+1
        dq[cnt].pop_front();
        dq[cnt+1].push_back(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 pos=posi[val], cnt=ccnt[pos];
    int min_pos=dq[cnt].back();
    sw(pos,min_pos);

    ans-=(query(1,mx,1,1,min_pos).sr+query(1,mx,1,min_pos,mx).sl-ccnt[min_pos]);
    dq[cnt].pop_back();

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

    --ccnt[min_pos];
    update(1,mx,1,min_pos);

    --have[val];
}

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

struct Query {
    int l,r,idx;
    bool operator<(Query other) const {
        return pii(l/sq,r)<pii(other.l/sq,other.r);
    }
};

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

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

    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 77 ms 219096 KB Output is correct
2 Correct 74 ms 221120 KB Output is correct
3 Correct 62 ms 221120 KB Output is correct
4 Correct 72 ms 221120 KB Output is correct
5 Correct 72 ms 221124 KB Output is correct
6 Correct 70 ms 221044 KB Output is correct
7 Correct 71 ms 221128 KB Output is correct
8 Correct 73 ms 221192 KB Output is correct
9 Correct 72 ms 221224 KB Output is correct
10 Correct 77 ms 221124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 221056 KB Output is correct
2 Correct 73 ms 221060 KB Output is correct
3 Correct 80 ms 221124 KB Output is correct
4 Correct 110 ms 221380 KB Output is correct
5 Correct 158 ms 221632 KB Output is correct
6 Correct 187 ms 221888 KB Output is correct
7 Correct 178 ms 221632 KB Output is correct
8 Correct 187 ms 221864 KB Output is correct
9 Correct 194 ms 221888 KB Output is correct
10 Correct 190 ms 221892 KB Output is correct
11 Correct 182 ms 221892 KB Output is correct
12 Correct 191 ms 221892 KB Output is correct
13 Correct 195 ms 221844 KB Output is correct
14 Correct 194 ms 221888 KB Output is correct
15 Correct 196 ms 221876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 221056 KB Output is correct
2 Correct 73 ms 221060 KB Output is correct
3 Correct 80 ms 221124 KB Output is correct
4 Correct 110 ms 221380 KB Output is correct
5 Correct 158 ms 221632 KB Output is correct
6 Correct 187 ms 221888 KB Output is correct
7 Correct 178 ms 221632 KB Output is correct
8 Correct 187 ms 221864 KB Output is correct
9 Correct 194 ms 221888 KB Output is correct
10 Correct 190 ms 221892 KB Output is correct
11 Correct 182 ms 221892 KB Output is correct
12 Correct 191 ms 221892 KB Output is correct
13 Correct 195 ms 221844 KB Output is correct
14 Correct 194 ms 221888 KB Output is correct
15 Correct 196 ms 221876 KB Output is correct
16 Correct 64 ms 221120 KB Output is correct
17 Correct 67 ms 221120 KB Output is correct
18 Correct 68 ms 221120 KB Output is correct
19 Correct 131 ms 221324 KB Output is correct
20 Correct 153 ms 221632 KB Output is correct
21 Correct 192 ms 221828 KB Output is correct
22 Correct 206 ms 221636 KB Output is correct
23 Correct 199 ms 221636 KB Output is correct
24 Correct 205 ms 221688 KB Output is correct
25 Correct 236 ms 221636 KB Output is correct
26 Correct 223 ms 221892 KB Output is correct
27 Correct 242 ms 221644 KB Output is correct
28 Correct 207 ms 221888 KB Output is correct
29 Correct 200 ms 221792 KB Output is correct
30 Correct 237 ms 221636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 74 ms 221056 KB Output is correct
2 Correct 73 ms 221060 KB Output is correct
3 Correct 80 ms 221124 KB Output is correct
4 Correct 110 ms 221380 KB Output is correct
5 Correct 158 ms 221632 KB Output is correct
6 Correct 187 ms 221888 KB Output is correct
7 Correct 178 ms 221632 KB Output is correct
8 Correct 187 ms 221864 KB Output is correct
9 Correct 194 ms 221888 KB Output is correct
10 Correct 190 ms 221892 KB Output is correct
11 Correct 182 ms 221892 KB Output is correct
12 Correct 191 ms 221892 KB Output is correct
13 Correct 195 ms 221844 KB Output is correct
14 Correct 194 ms 221888 KB Output is correct
15 Correct 196 ms 221876 KB Output is correct
16 Correct 64 ms 221120 KB Output is correct
17 Correct 67 ms 221120 KB Output is correct
18 Correct 68 ms 221120 KB Output is correct
19 Correct 131 ms 221324 KB Output is correct
20 Correct 153 ms 221632 KB Output is correct
21 Correct 192 ms 221828 KB Output is correct
22 Correct 206 ms 221636 KB Output is correct
23 Correct 199 ms 221636 KB Output is correct
24 Correct 205 ms 221688 KB Output is correct
25 Correct 236 ms 221636 KB Output is correct
26 Correct 223 ms 221892 KB Output is correct
27 Correct 242 ms 221644 KB Output is correct
28 Correct 207 ms 221888 KB Output is correct
29 Correct 200 ms 221792 KB Output is correct
30 Correct 237 ms 221636 KB Output is correct
31 Correct 101 ms 221120 KB Output is correct
32 Correct 89 ms 221244 KB Output is correct
33 Correct 72 ms 221076 KB Output is correct
34 Correct 119 ms 221124 KB Output is correct
35 Correct 76 ms 221120 KB Output is correct
36 Correct 79 ms 221120 KB Output is correct
37 Correct 109 ms 221376 KB Output is correct
38 Correct 101 ms 221380 KB Output is correct
39 Correct 126 ms 221632 KB Output is correct
40 Correct 173 ms 221892 KB Output is correct
41 Correct 225 ms 222148 KB Output is correct
42 Correct 219 ms 222132 KB Output is correct
43 Correct 226 ms 222144 KB Output is correct
44 Correct 234 ms 221924 KB Output is correct
45 Correct 248 ms 222036 KB Output is correct
46 Correct 219 ms 222020 KB Output is correct
47 Correct 227 ms 222148 KB Output is correct
48 Correct 234 ms 222148 KB Output is correct
49 Correct 229 ms 222144 KB Output is correct
50 Correct 220 ms 222100 KB Output is correct
51 Correct 233 ms 222148 KB Output is correct
52 Correct 221 ms 222148 KB Output is correct
53 Correct 222 ms 222148 KB Output is correct
54 Correct 229 ms 222148 KB Output is correct
55 Correct 220 ms 222144 KB Output is correct
56 Correct 228 ms 222140 KB Output is correct
57 Correct 249 ms 222144 KB Output is correct
58 Correct 228 ms 221968 KB Output is correct
59 Correct 221 ms 222144 KB Output is correct
60 Correct 246 ms 222036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 219096 KB Output is correct
2 Correct 74 ms 221120 KB Output is correct
3 Correct 62 ms 221120 KB Output is correct
4 Correct 72 ms 221120 KB Output is correct
5 Correct 72 ms 221124 KB Output is correct
6 Correct 70 ms 221044 KB Output is correct
7 Correct 71 ms 221128 KB Output is correct
8 Correct 73 ms 221192 KB Output is correct
9 Correct 72 ms 221224 KB Output is correct
10 Correct 77 ms 221124 KB Output is correct
11 Correct 74 ms 221056 KB Output is correct
12 Correct 73 ms 221060 KB Output is correct
13 Correct 80 ms 221124 KB Output is correct
14 Correct 110 ms 221380 KB Output is correct
15 Correct 158 ms 221632 KB Output is correct
16 Correct 187 ms 221888 KB Output is correct
17 Correct 178 ms 221632 KB Output is correct
18 Correct 187 ms 221864 KB Output is correct
19 Correct 194 ms 221888 KB Output is correct
20 Correct 190 ms 221892 KB Output is correct
21 Correct 182 ms 221892 KB Output is correct
22 Correct 191 ms 221892 KB Output is correct
23 Correct 195 ms 221844 KB Output is correct
24 Correct 194 ms 221888 KB Output is correct
25 Correct 196 ms 221876 KB Output is correct
26 Correct 64 ms 221120 KB Output is correct
27 Correct 67 ms 221120 KB Output is correct
28 Correct 68 ms 221120 KB Output is correct
29 Correct 131 ms 221324 KB Output is correct
30 Correct 153 ms 221632 KB Output is correct
31 Correct 192 ms 221828 KB Output is correct
32 Correct 206 ms 221636 KB Output is correct
33 Correct 199 ms 221636 KB Output is correct
34 Correct 205 ms 221688 KB Output is correct
35 Correct 236 ms 221636 KB Output is correct
36 Correct 223 ms 221892 KB Output is correct
37 Correct 242 ms 221644 KB Output is correct
38 Correct 207 ms 221888 KB Output is correct
39 Correct 200 ms 221792 KB Output is correct
40 Correct 237 ms 221636 KB Output is correct
41 Correct 101 ms 221120 KB Output is correct
42 Correct 89 ms 221244 KB Output is correct
43 Correct 72 ms 221076 KB Output is correct
44 Correct 119 ms 221124 KB Output is correct
45 Correct 76 ms 221120 KB Output is correct
46 Correct 79 ms 221120 KB Output is correct
47 Correct 109 ms 221376 KB Output is correct
48 Correct 101 ms 221380 KB Output is correct
49 Correct 126 ms 221632 KB Output is correct
50 Correct 173 ms 221892 KB Output is correct
51 Correct 225 ms 222148 KB Output is correct
52 Correct 219 ms 222132 KB Output is correct
53 Correct 226 ms 222144 KB Output is correct
54 Correct 234 ms 221924 KB Output is correct
55 Correct 248 ms 222036 KB Output is correct
56 Correct 219 ms 222020 KB Output is correct
57 Correct 227 ms 222148 KB Output is correct
58 Correct 234 ms 222148 KB Output is correct
59 Correct 229 ms 222144 KB Output is correct
60 Correct 220 ms 222100 KB Output is correct
61 Correct 233 ms 222148 KB Output is correct
62 Correct 221 ms 222148 KB Output is correct
63 Correct 222 ms 222148 KB Output is correct
64 Correct 229 ms 222148 KB Output is correct
65 Correct 220 ms 222144 KB Output is correct
66 Correct 228 ms 222140 KB Output is correct
67 Correct 249 ms 222144 KB Output is correct
68 Correct 228 ms 221968 KB Output is correct
69 Correct 221 ms 222144 KB Output is correct
70 Correct 246 ms 222036 KB Output is correct
71 Correct 106 ms 223424 KB Output is correct
72 Correct 100 ms 223444 KB Output is correct
73 Correct 109 ms 223428 KB Output is correct
74 Correct 108 ms 223432 KB Output is correct
75 Correct 110 ms 223424 KB Output is correct
76 Correct 130 ms 225728 KB Output is correct
77 Correct 130 ms 225732 KB Output is correct
78 Correct 125 ms 225728 KB Output is correct
79 Correct 148 ms 225732 KB Output is correct
80 Correct 137 ms 225728 KB Output is correct
81 Correct 186 ms 227912 KB Output is correct
82 Correct 179 ms 228028 KB Output is correct
83 Correct 180 ms 227988 KB Output is correct
84 Correct 177 ms 228080 KB Output is correct
85 Correct 181 ms 228032 KB Output is correct
86 Correct 171 ms 228028 KB Output is correct
87 Correct 180 ms 228032 KB Output is correct
88 Correct 172 ms 228064 KB Output is correct
89 Correct 176 ms 228028 KB Output is correct
90 Correct 187 ms 228032 KB Output is correct
91 Correct 273 ms 232896 KB Output is correct
92 Correct 223 ms 232888 KB Output is correct
93 Correct 226 ms 232864 KB Output is correct
94 Correct 228 ms 233088 KB Output is correct
95 Correct 220 ms 233076 KB Output is correct
96 Correct 243 ms 241596 KB Output is correct
97 Correct 225 ms 241600 KB Output is correct
98 Correct 225 ms 241600 KB Output is correct
99 Correct 259 ms 241652 KB Output is correct
100 Correct 225 ms 241592 KB Output is correct
101 Correct 225 ms 241596 KB Output is correct
102 Correct 221 ms 241600 KB Output is correct
103 Correct 233 ms 241476 KB Output is correct
104 Correct 233 ms 241600 KB Output is correct
105 Correct 228 ms 241596 KB Output is correct
106 Correct 230 ms 245948 KB Output is correct
107 Correct 222 ms 246008 KB Output is correct
108 Correct 219 ms 245872 KB Output is correct
109 Correct 236 ms 246028 KB Output is correct
110 Correct 230 ms 245824 KB Output is correct
111 Correct 251 ms 245948 KB Output is correct
112 Correct 229 ms 245928 KB Output is correct
113 Correct 224 ms 245948 KB Output is correct
114 Correct 227 ms 245952 KB Output is correct
115 Correct 224 ms 246004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 77 ms 219096 KB Output is correct
2 Correct 74 ms 221120 KB Output is correct
3 Correct 62 ms 221120 KB Output is correct
4 Correct 72 ms 221120 KB Output is correct
5 Correct 72 ms 221124 KB Output is correct
6 Correct 70 ms 221044 KB Output is correct
7 Correct 71 ms 221128 KB Output is correct
8 Correct 73 ms 221192 KB Output is correct
9 Correct 72 ms 221224 KB Output is correct
10 Correct 77 ms 221124 KB Output is correct
11 Correct 74 ms 221056 KB Output is correct
12 Correct 73 ms 221060 KB Output is correct
13 Correct 80 ms 221124 KB Output is correct
14 Correct 110 ms 221380 KB Output is correct
15 Correct 158 ms 221632 KB Output is correct
16 Correct 187 ms 221888 KB Output is correct
17 Correct 178 ms 221632 KB Output is correct
18 Correct 187 ms 221864 KB Output is correct
19 Correct 194 ms 221888 KB Output is correct
20 Correct 190 ms 221892 KB Output is correct
21 Correct 182 ms 221892 KB Output is correct
22 Correct 191 ms 221892 KB Output is correct
23 Correct 195 ms 221844 KB Output is correct
24 Correct 194 ms 221888 KB Output is correct
25 Correct 196 ms 221876 KB Output is correct
26 Correct 64 ms 221120 KB Output is correct
27 Correct 67 ms 221120 KB Output is correct
28 Correct 68 ms 221120 KB Output is correct
29 Correct 131 ms 221324 KB Output is correct
30 Correct 153 ms 221632 KB Output is correct
31 Correct 192 ms 221828 KB Output is correct
32 Correct 206 ms 221636 KB Output is correct
33 Correct 199 ms 221636 KB Output is correct
34 Correct 205 ms 221688 KB Output is correct
35 Correct 236 ms 221636 KB Output is correct
36 Correct 223 ms 221892 KB Output is correct
37 Correct 242 ms 221644 KB Output is correct
38 Correct 207 ms 221888 KB Output is correct
39 Correct 200 ms 221792 KB Output is correct
40 Correct 237 ms 221636 KB Output is correct
41 Correct 101 ms 221120 KB Output is correct
42 Correct 89 ms 221244 KB Output is correct
43 Correct 72 ms 221076 KB Output is correct
44 Correct 119 ms 221124 KB Output is correct
45 Correct 76 ms 221120 KB Output is correct
46 Correct 79 ms 221120 KB Output is correct
47 Correct 109 ms 221376 KB Output is correct
48 Correct 101 ms 221380 KB Output is correct
49 Correct 126 ms 221632 KB Output is correct
50 Correct 173 ms 221892 KB Output is correct
51 Correct 225 ms 222148 KB Output is correct
52 Correct 219 ms 222132 KB Output is correct
53 Correct 226 ms 222144 KB Output is correct
54 Correct 234 ms 221924 KB Output is correct
55 Correct 248 ms 222036 KB Output is correct
56 Correct 219 ms 222020 KB Output is correct
57 Correct 227 ms 222148 KB Output is correct
58 Correct 234 ms 222148 KB Output is correct
59 Correct 229 ms 222144 KB Output is correct
60 Correct 220 ms 222100 KB Output is correct
61 Correct 233 ms 222148 KB Output is correct
62 Correct 221 ms 222148 KB Output is correct
63 Correct 222 ms 222148 KB Output is correct
64 Correct 229 ms 222148 KB Output is correct
65 Correct 220 ms 222144 KB Output is correct
66 Correct 228 ms 222140 KB Output is correct
67 Correct 249 ms 222144 KB Output is correct
68 Correct 228 ms 221968 KB Output is correct
69 Correct 221 ms 222144 KB Output is correct
70 Correct 246 ms 222036 KB Output is correct
71 Correct 106 ms 223424 KB Output is correct
72 Correct 100 ms 223444 KB Output is correct
73 Correct 109 ms 223428 KB Output is correct
74 Correct 108 ms 223432 KB Output is correct
75 Correct 110 ms 223424 KB Output is correct
76 Correct 130 ms 225728 KB Output is correct
77 Correct 130 ms 225732 KB Output is correct
78 Correct 125 ms 225728 KB Output is correct
79 Correct 148 ms 225732 KB Output is correct
80 Correct 137 ms 225728 KB Output is correct
81 Correct 186 ms 227912 KB Output is correct
82 Correct 179 ms 228028 KB Output is correct
83 Correct 180 ms 227988 KB Output is correct
84 Correct 177 ms 228080 KB Output is correct
85 Correct 181 ms 228032 KB Output is correct
86 Correct 171 ms 228028 KB Output is correct
87 Correct 180 ms 228032 KB Output is correct
88 Correct 172 ms 228064 KB Output is correct
89 Correct 176 ms 228028 KB Output is correct
90 Correct 187 ms 228032 KB Output is correct
91 Correct 273 ms 232896 KB Output is correct
92 Correct 223 ms 232888 KB Output is correct
93 Correct 226 ms 232864 KB Output is correct
94 Correct 228 ms 233088 KB Output is correct
95 Correct 220 ms 233076 KB Output is correct
96 Correct 243 ms 241596 KB Output is correct
97 Correct 225 ms 241600 KB Output is correct
98 Correct 225 ms 241600 KB Output is correct
99 Correct 259 ms 241652 KB Output is correct
100 Correct 225 ms 241592 KB Output is correct
101 Correct 225 ms 241596 KB Output is correct
102 Correct 221 ms 241600 KB Output is correct
103 Correct 233 ms 241476 KB Output is correct
104 Correct 233 ms 241600 KB Output is correct
105 Correct 228 ms 241596 KB Output is correct
106 Correct 230 ms 245948 KB Output is correct
107 Correct 222 ms 246008 KB Output is correct
108 Correct 219 ms 245872 KB Output is correct
109 Correct 236 ms 246028 KB Output is correct
110 Correct 230 ms 245824 KB Output is correct
111 Correct 251 ms 245948 KB Output is correct
112 Correct 229 ms 245928 KB Output is correct
113 Correct 224 ms 245948 KB Output is correct
114 Correct 227 ms 245952 KB Output is correct
115 Correct 224 ms 246004 KB Output is correct
116 Execution timed out 7064 ms 223044 KB Time limit exceeded
117 Halted 0 ms 0 KB -