Submission #549574

# Submission time Handle Problem Language Result Execution time Memory
549574 2022-04-16T04:29:35 Z minhcool Diversity (CEOI21_diversity) C++17
0 / 100
0 ms 340 KB
#include<bits/stdc++.h>
using namespace std;

#define int long long
#define fi first
#define se second
#define pb push_back

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;

const int N = 4e5 + 5;

const int oo = 1e18 + 7, mod = 1e9 + 7;

int n, q, a[N];

int cnt[N];

int cntt[N];

int S = 500;

bool cmp(iii a, iii b){
    if(a.fi.fi / S != b.fi.fi / S) return (a.fi.fi / S < b.fi.fi / S);
    else{
        if(!((a.fi.fi / S) & 1)) return (a.fi.se < b.fi.se);
        else return (a.fi.se > b.fi.se);
    }
}

vector<iii> queries;
set<int> se;

void ins(int pos){
    cntt[cnt[a[pos]]]--;
    if(!cntt[cnt[a[pos]]]) se.erase(cnt[a[pos]]);
    cnt[a[pos]]++;
    cout << a[pos] << " " << cnt[a[pos]] << "\n";
    cntt[cnt[a[pos]]]++;
    if(cntt[cnt[a[pos]]] == 1) se.insert(cnt[a[pos]]);
}

void er(int pos){
    cntt[cnt[a[pos]]]--;
    if(!cntt[cnt[a[pos]]]) se.erase(cnt[a[pos]]);
    cnt[a[pos]]--;
    cout << a[pos] << " " << cnt[a[pos]] << "\n";
    cntt[cnt[a[pos]]]++;
    if(cntt[cnt[a[pos]]] == 1) se.insert(cnt[a[pos]]);
}

int answer[N];

void process(){
    cin >> n >> q;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        //cnt[a[i]].fi++;
    }
    for(int i = 1; i <= q; i++){
        int l, r;
        cin >> l >> r;
        queries.pb({{l, r}, i});
    }   
    sort(queries.begin(), queries.end(), cmp);
    int lstl = 0, lstr = 0;
    for(int i = 0; i < q; i++){
        //sort(cnt + 1, cnt + 300000 + 1, greater<ii>());
        while(lstr < queries[i].fi.se){
            //ins(a[lstr]);
            lstr++;
            ins(lstr);
        }
        while(lstl > queries[i].fi.fi){
            lstl--;
            ins(lstl);
        }
        while(lstl < queries[i].fi.fi){
            if(lstl) er(lstl);
            lstl++;
        }
        while(lstr > queries[i].fi.se){
            er(lstr);
            lstr--;
        }
        //cout << lstl << " " << lstr << "\n";
        vector<ii> vc1, vc2;
        int total = 0;
        for(auto it : se){
            int temp1 = (cntt[it] + 1) / 2, temp2 = cntt[it] / 2;
            if(!(total & 1)){
                if(temp1) vc1.pb({it, temp1});
                if(temp2) vc2.pb({it, temp2});
            }
            else{
                if(temp2) vc1.pb({it, temp2});
                if(temp1) vc2.pb({it, temp1});
            }
            total += cntt[it];
        }
        //for(int j = 1; j <= 5; j++) cout << j << " " << cnt[j] << "\n";
        for(int j = vc2.size() - 1; j >= 0; j--) vc1.pb(vc2[j]);
        int tol = 0, tol2 = 0, toll = 1;
        for(auto it : vc1){
            //cout << lstl << " " << lstr << " " << it.fi << " " << it.se << "\n";
            int temp = ((it.se * (it.se + 1) * (2 * it.se + 1)) / 6 + (it.se * (it.se + 1)) / 2) / 2 - it.se;
            temp = temp * it.fi * it.fi + toll * it.fi * it.se;
            temp += it.se * (it.fi * (it.fi - 1) / 2);
            temp += tol * ((it.se * (it.se - 1)) / 2) * it.fi;
            toll += ((it.se * (it.se + 1)) / 2) * it.fi + tol * (it.se - 1);
            tol += it.fi * it.se;
            toll += tol;
            tol2 += temp;
        }
        answer[queries[i].se] = tol2;
    }
    for(int i = 1; i <= q; i++) cout << answer[i] << "\n";
}

signed main(){
    ios_base::sync_with_stdio(0);
    process();
}
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -