Submission #1049455

# Submission time Handle Problem Language Result Execution time Memory
1049455 2024-08-08T18:58:38 Z PoonYaPat Diversity (CEOI21_diversity) C++14
64 / 100
7000 ms 246208 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]);
    }
}

ll fast_query(int l, int r, int idx, int x) {
    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) return s[idx].sum;
    int mid=(l+r)/2;
    return fast_query(l,mid,2*idx,x)+fast_query(mid+1,r,2*idx+1,x);
}

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

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+=fast_query(1,mx,1,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+=fast_query(1,mx,1,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-=fast_query(1,mx,1,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";
}
# Verdict Execution time Memory Grader output
1 Correct 67 ms 219088 KB Output is correct
2 Correct 78 ms 221036 KB Output is correct
3 Correct 67 ms 221024 KB Output is correct
4 Correct 83 ms 221120 KB Output is correct
5 Correct 69 ms 221124 KB Output is correct
6 Correct 76 ms 221120 KB Output is correct
7 Correct 70 ms 221068 KB Output is correct
8 Correct 75 ms 221120 KB Output is correct
9 Correct 69 ms 221120 KB Output is correct
10 Correct 78 ms 221120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 221124 KB Output is correct
2 Correct 72 ms 221468 KB Output is correct
3 Correct 79 ms 221224 KB Output is correct
4 Correct 103 ms 221448 KB Output is correct
5 Correct 118 ms 221632 KB Output is correct
6 Correct 147 ms 221632 KB Output is correct
7 Correct 145 ms 221764 KB Output is correct
8 Correct 146 ms 221640 KB Output is correct
9 Correct 135 ms 221636 KB Output is correct
10 Correct 163 ms 221636 KB Output is correct
11 Correct 140 ms 221732 KB Output is correct
12 Correct 146 ms 221636 KB Output is correct
13 Correct 142 ms 221632 KB Output is correct
14 Correct 152 ms 221636 KB Output is correct
15 Correct 154 ms 221556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 221124 KB Output is correct
2 Correct 72 ms 221468 KB Output is correct
3 Correct 79 ms 221224 KB Output is correct
4 Correct 103 ms 221448 KB Output is correct
5 Correct 118 ms 221632 KB Output is correct
6 Correct 147 ms 221632 KB Output is correct
7 Correct 145 ms 221764 KB Output is correct
8 Correct 146 ms 221640 KB Output is correct
9 Correct 135 ms 221636 KB Output is correct
10 Correct 163 ms 221636 KB Output is correct
11 Correct 140 ms 221732 KB Output is correct
12 Correct 146 ms 221636 KB Output is correct
13 Correct 142 ms 221632 KB Output is correct
14 Correct 152 ms 221636 KB Output is correct
15 Correct 154 ms 221556 KB Output is correct
16 Correct 99 ms 221124 KB Output is correct
17 Correct 72 ms 221056 KB Output is correct
18 Correct 74 ms 221120 KB Output is correct
19 Correct 97 ms 221328 KB Output is correct
20 Correct 127 ms 221544 KB Output is correct
21 Correct 153 ms 222016 KB Output is correct
22 Correct 144 ms 221888 KB Output is correct
23 Correct 146 ms 221888 KB Output is correct
24 Correct 147 ms 221960 KB Output is correct
25 Correct 147 ms 221892 KB Output is correct
26 Correct 145 ms 221636 KB Output is correct
27 Correct 149 ms 221812 KB Output is correct
28 Correct 150 ms 221636 KB Output is correct
29 Correct 161 ms 221772 KB Output is correct
30 Correct 165 ms 221892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 221124 KB Output is correct
2 Correct 72 ms 221468 KB Output is correct
3 Correct 79 ms 221224 KB Output is correct
4 Correct 103 ms 221448 KB Output is correct
5 Correct 118 ms 221632 KB Output is correct
6 Correct 147 ms 221632 KB Output is correct
7 Correct 145 ms 221764 KB Output is correct
8 Correct 146 ms 221640 KB Output is correct
9 Correct 135 ms 221636 KB Output is correct
10 Correct 163 ms 221636 KB Output is correct
11 Correct 140 ms 221732 KB Output is correct
12 Correct 146 ms 221636 KB Output is correct
13 Correct 142 ms 221632 KB Output is correct
14 Correct 152 ms 221636 KB Output is correct
15 Correct 154 ms 221556 KB Output is correct
16 Correct 99 ms 221124 KB Output is correct
17 Correct 72 ms 221056 KB Output is correct
18 Correct 74 ms 221120 KB Output is correct
19 Correct 97 ms 221328 KB Output is correct
20 Correct 127 ms 221544 KB Output is correct
21 Correct 153 ms 222016 KB Output is correct
22 Correct 144 ms 221888 KB Output is correct
23 Correct 146 ms 221888 KB Output is correct
24 Correct 147 ms 221960 KB Output is correct
25 Correct 147 ms 221892 KB Output is correct
26 Correct 145 ms 221636 KB Output is correct
27 Correct 149 ms 221812 KB Output is correct
28 Correct 150 ms 221636 KB Output is correct
29 Correct 161 ms 221772 KB Output is correct
30 Correct 165 ms 221892 KB Output is correct
31 Correct 70 ms 221124 KB Output is correct
32 Correct 75 ms 221128 KB Output is correct
33 Correct 75 ms 221120 KB Output is correct
34 Correct 82 ms 221124 KB Output is correct
35 Correct 90 ms 221164 KB Output is correct
36 Correct 86 ms 221124 KB Output is correct
37 Correct 96 ms 221352 KB Output is correct
38 Correct 96 ms 221376 KB Output is correct
39 Correct 118 ms 221888 KB Output is correct
40 Correct 133 ms 221632 KB Output is correct
41 Correct 153 ms 222144 KB Output is correct
42 Correct 149 ms 222400 KB Output is correct
43 Correct 159 ms 222144 KB Output is correct
44 Correct 161 ms 222148 KB Output is correct
45 Correct 159 ms 222252 KB Output is correct
46 Correct 157 ms 222064 KB Output is correct
47 Correct 163 ms 222144 KB Output is correct
48 Correct 153 ms 222288 KB Output is correct
49 Correct 161 ms 222296 KB Output is correct
50 Correct 157 ms 222144 KB Output is correct
51 Correct 160 ms 222148 KB Output is correct
52 Correct 155 ms 222148 KB Output is correct
53 Correct 161 ms 222144 KB Output is correct
54 Correct 177 ms 222144 KB Output is correct
55 Correct 155 ms 222184 KB Output is correct
56 Correct 163 ms 222148 KB Output is correct
57 Correct 166 ms 222140 KB Output is correct
58 Correct 159 ms 222128 KB Output is correct
59 Correct 164 ms 222148 KB Output is correct
60 Correct 166 ms 222140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 219088 KB Output is correct
2 Correct 78 ms 221036 KB Output is correct
3 Correct 67 ms 221024 KB Output is correct
4 Correct 83 ms 221120 KB Output is correct
5 Correct 69 ms 221124 KB Output is correct
6 Correct 76 ms 221120 KB Output is correct
7 Correct 70 ms 221068 KB Output is correct
8 Correct 75 ms 221120 KB Output is correct
9 Correct 69 ms 221120 KB Output is correct
10 Correct 78 ms 221120 KB Output is correct
11 Correct 78 ms 221124 KB Output is correct
12 Correct 72 ms 221468 KB Output is correct
13 Correct 79 ms 221224 KB Output is correct
14 Correct 103 ms 221448 KB Output is correct
15 Correct 118 ms 221632 KB Output is correct
16 Correct 147 ms 221632 KB Output is correct
17 Correct 145 ms 221764 KB Output is correct
18 Correct 146 ms 221640 KB Output is correct
19 Correct 135 ms 221636 KB Output is correct
20 Correct 163 ms 221636 KB Output is correct
21 Correct 140 ms 221732 KB Output is correct
22 Correct 146 ms 221636 KB Output is correct
23 Correct 142 ms 221632 KB Output is correct
24 Correct 152 ms 221636 KB Output is correct
25 Correct 154 ms 221556 KB Output is correct
26 Correct 99 ms 221124 KB Output is correct
27 Correct 72 ms 221056 KB Output is correct
28 Correct 74 ms 221120 KB Output is correct
29 Correct 97 ms 221328 KB Output is correct
30 Correct 127 ms 221544 KB Output is correct
31 Correct 153 ms 222016 KB Output is correct
32 Correct 144 ms 221888 KB Output is correct
33 Correct 146 ms 221888 KB Output is correct
34 Correct 147 ms 221960 KB Output is correct
35 Correct 147 ms 221892 KB Output is correct
36 Correct 145 ms 221636 KB Output is correct
37 Correct 149 ms 221812 KB Output is correct
38 Correct 150 ms 221636 KB Output is correct
39 Correct 161 ms 221772 KB Output is correct
40 Correct 165 ms 221892 KB Output is correct
41 Correct 70 ms 221124 KB Output is correct
42 Correct 75 ms 221128 KB Output is correct
43 Correct 75 ms 221120 KB Output is correct
44 Correct 82 ms 221124 KB Output is correct
45 Correct 90 ms 221164 KB Output is correct
46 Correct 86 ms 221124 KB Output is correct
47 Correct 96 ms 221352 KB Output is correct
48 Correct 96 ms 221376 KB Output is correct
49 Correct 118 ms 221888 KB Output is correct
50 Correct 133 ms 221632 KB Output is correct
51 Correct 153 ms 222144 KB Output is correct
52 Correct 149 ms 222400 KB Output is correct
53 Correct 159 ms 222144 KB Output is correct
54 Correct 161 ms 222148 KB Output is correct
55 Correct 159 ms 222252 KB Output is correct
56 Correct 157 ms 222064 KB Output is correct
57 Correct 163 ms 222144 KB Output is correct
58 Correct 153 ms 222288 KB Output is correct
59 Correct 161 ms 222296 KB Output is correct
60 Correct 157 ms 222144 KB Output is correct
61 Correct 160 ms 222148 KB Output is correct
62 Correct 155 ms 222148 KB Output is correct
63 Correct 161 ms 222144 KB Output is correct
64 Correct 177 ms 222144 KB Output is correct
65 Correct 155 ms 222184 KB Output is correct
66 Correct 163 ms 222148 KB Output is correct
67 Correct 166 ms 222140 KB Output is correct
68 Correct 159 ms 222128 KB Output is correct
69 Correct 164 ms 222148 KB Output is correct
70 Correct 166 ms 222140 KB Output is correct
71 Correct 98 ms 223424 KB Output is correct
72 Correct 89 ms 223424 KB Output is correct
73 Correct 99 ms 223424 KB Output is correct
74 Correct 105 ms 223304 KB Output is correct
75 Correct 97 ms 223360 KB Output is correct
76 Correct 119 ms 225732 KB Output is correct
77 Correct 115 ms 225728 KB Output is correct
78 Correct 99 ms 225728 KB Output is correct
79 Correct 113 ms 225732 KB Output is correct
80 Correct 111 ms 225732 KB Output is correct
81 Correct 138 ms 228428 KB Output is correct
82 Correct 133 ms 228104 KB Output is correct
83 Correct 138 ms 227968 KB Output is correct
84 Correct 137 ms 228032 KB Output is correct
85 Correct 137 ms 227968 KB Output is correct
86 Correct 129 ms 228292 KB Output is correct
87 Correct 131 ms 228288 KB Output is correct
88 Correct 129 ms 228204 KB Output is correct
89 Correct 152 ms 228436 KB Output is correct
90 Correct 142 ms 228200 KB Output is correct
91 Correct 169 ms 232892 KB Output is correct
92 Correct 175 ms 233036 KB Output is correct
93 Correct 167 ms 232900 KB Output is correct
94 Correct 162 ms 232896 KB Output is correct
95 Correct 168 ms 232892 KB Output is correct
96 Correct 158 ms 241596 KB Output is correct
97 Correct 174 ms 241600 KB Output is correct
98 Correct 155 ms 241596 KB Output is correct
99 Correct 168 ms 241600 KB Output is correct
100 Correct 177 ms 241596 KB Output is correct
101 Correct 164 ms 241600 KB Output is correct
102 Correct 158 ms 241720 KB Output is correct
103 Correct 197 ms 241852 KB Output is correct
104 Correct 172 ms 241600 KB Output is correct
105 Correct 166 ms 241684 KB Output is correct
106 Correct 165 ms 245952 KB Output is correct
107 Correct 168 ms 245948 KB Output is correct
108 Correct 164 ms 245928 KB Output is correct
109 Correct 167 ms 246124 KB Output is correct
110 Correct 161 ms 245948 KB Output is correct
111 Correct 176 ms 246208 KB Output is correct
112 Correct 170 ms 245952 KB Output is correct
113 Correct 169 ms 245948 KB Output is correct
114 Correct 170 ms 245964 KB Output is correct
115 Correct 163 ms 246156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 219088 KB Output is correct
2 Correct 78 ms 221036 KB Output is correct
3 Correct 67 ms 221024 KB Output is correct
4 Correct 83 ms 221120 KB Output is correct
5 Correct 69 ms 221124 KB Output is correct
6 Correct 76 ms 221120 KB Output is correct
7 Correct 70 ms 221068 KB Output is correct
8 Correct 75 ms 221120 KB Output is correct
9 Correct 69 ms 221120 KB Output is correct
10 Correct 78 ms 221120 KB Output is correct
11 Correct 78 ms 221124 KB Output is correct
12 Correct 72 ms 221468 KB Output is correct
13 Correct 79 ms 221224 KB Output is correct
14 Correct 103 ms 221448 KB Output is correct
15 Correct 118 ms 221632 KB Output is correct
16 Correct 147 ms 221632 KB Output is correct
17 Correct 145 ms 221764 KB Output is correct
18 Correct 146 ms 221640 KB Output is correct
19 Correct 135 ms 221636 KB Output is correct
20 Correct 163 ms 221636 KB Output is correct
21 Correct 140 ms 221732 KB Output is correct
22 Correct 146 ms 221636 KB Output is correct
23 Correct 142 ms 221632 KB Output is correct
24 Correct 152 ms 221636 KB Output is correct
25 Correct 154 ms 221556 KB Output is correct
26 Correct 99 ms 221124 KB Output is correct
27 Correct 72 ms 221056 KB Output is correct
28 Correct 74 ms 221120 KB Output is correct
29 Correct 97 ms 221328 KB Output is correct
30 Correct 127 ms 221544 KB Output is correct
31 Correct 153 ms 222016 KB Output is correct
32 Correct 144 ms 221888 KB Output is correct
33 Correct 146 ms 221888 KB Output is correct
34 Correct 147 ms 221960 KB Output is correct
35 Correct 147 ms 221892 KB Output is correct
36 Correct 145 ms 221636 KB Output is correct
37 Correct 149 ms 221812 KB Output is correct
38 Correct 150 ms 221636 KB Output is correct
39 Correct 161 ms 221772 KB Output is correct
40 Correct 165 ms 221892 KB Output is correct
41 Correct 70 ms 221124 KB Output is correct
42 Correct 75 ms 221128 KB Output is correct
43 Correct 75 ms 221120 KB Output is correct
44 Correct 82 ms 221124 KB Output is correct
45 Correct 90 ms 221164 KB Output is correct
46 Correct 86 ms 221124 KB Output is correct
47 Correct 96 ms 221352 KB Output is correct
48 Correct 96 ms 221376 KB Output is correct
49 Correct 118 ms 221888 KB Output is correct
50 Correct 133 ms 221632 KB Output is correct
51 Correct 153 ms 222144 KB Output is correct
52 Correct 149 ms 222400 KB Output is correct
53 Correct 159 ms 222144 KB Output is correct
54 Correct 161 ms 222148 KB Output is correct
55 Correct 159 ms 222252 KB Output is correct
56 Correct 157 ms 222064 KB Output is correct
57 Correct 163 ms 222144 KB Output is correct
58 Correct 153 ms 222288 KB Output is correct
59 Correct 161 ms 222296 KB Output is correct
60 Correct 157 ms 222144 KB Output is correct
61 Correct 160 ms 222148 KB Output is correct
62 Correct 155 ms 222148 KB Output is correct
63 Correct 161 ms 222144 KB Output is correct
64 Correct 177 ms 222144 KB Output is correct
65 Correct 155 ms 222184 KB Output is correct
66 Correct 163 ms 222148 KB Output is correct
67 Correct 166 ms 222140 KB Output is correct
68 Correct 159 ms 222128 KB Output is correct
69 Correct 164 ms 222148 KB Output is correct
70 Correct 166 ms 222140 KB Output is correct
71 Correct 98 ms 223424 KB Output is correct
72 Correct 89 ms 223424 KB Output is correct
73 Correct 99 ms 223424 KB Output is correct
74 Correct 105 ms 223304 KB Output is correct
75 Correct 97 ms 223360 KB Output is correct
76 Correct 119 ms 225732 KB Output is correct
77 Correct 115 ms 225728 KB Output is correct
78 Correct 99 ms 225728 KB Output is correct
79 Correct 113 ms 225732 KB Output is correct
80 Correct 111 ms 225732 KB Output is correct
81 Correct 138 ms 228428 KB Output is correct
82 Correct 133 ms 228104 KB Output is correct
83 Correct 138 ms 227968 KB Output is correct
84 Correct 137 ms 228032 KB Output is correct
85 Correct 137 ms 227968 KB Output is correct
86 Correct 129 ms 228292 KB Output is correct
87 Correct 131 ms 228288 KB Output is correct
88 Correct 129 ms 228204 KB Output is correct
89 Correct 152 ms 228436 KB Output is correct
90 Correct 142 ms 228200 KB Output is correct
91 Correct 169 ms 232892 KB Output is correct
92 Correct 175 ms 233036 KB Output is correct
93 Correct 167 ms 232900 KB Output is correct
94 Correct 162 ms 232896 KB Output is correct
95 Correct 168 ms 232892 KB Output is correct
96 Correct 158 ms 241596 KB Output is correct
97 Correct 174 ms 241600 KB Output is correct
98 Correct 155 ms 241596 KB Output is correct
99 Correct 168 ms 241600 KB Output is correct
100 Correct 177 ms 241596 KB Output is correct
101 Correct 164 ms 241600 KB Output is correct
102 Correct 158 ms 241720 KB Output is correct
103 Correct 197 ms 241852 KB Output is correct
104 Correct 172 ms 241600 KB Output is correct
105 Correct 166 ms 241684 KB Output is correct
106 Correct 165 ms 245952 KB Output is correct
107 Correct 168 ms 245948 KB Output is correct
108 Correct 164 ms 245928 KB Output is correct
109 Correct 167 ms 246124 KB Output is correct
110 Correct 161 ms 245948 KB Output is correct
111 Correct 176 ms 246208 KB Output is correct
112 Correct 170 ms 245952 KB Output is correct
113 Correct 169 ms 245948 KB Output is correct
114 Correct 170 ms 245964 KB Output is correct
115 Correct 163 ms 246156 KB Output is correct
116 Correct 4094 ms 223444 KB Output is correct
117 Correct 4013 ms 223424 KB Output is correct
118 Correct 4582 ms 223708 KB Output is correct
119 Correct 5075 ms 223944 KB Output is correct
120 Correct 5310 ms 223936 KB Output is correct
121 Correct 6705 ms 224156 KB Output is correct
122 Correct 6959 ms 226260 KB Output is correct
123 Execution timed out 7048 ms 223936 KB Time limit exceeded
124 Halted 0 ms 0 KB -