Submission #993849

# Submission time Handle Problem Language Result Execution time Memory
993849 2024-06-06T14:18:06 Z vladilius Diversity (CEOI21_diversity) C++17
4 / 100
15 ms 18012 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define ins insert
#define arr3 array<int, 3>
const int A = 3e5;
 
struct FT{
    vector<ll> bit;
    int n;
    FT(int ns){
        n = ns;
        bit.resize(n + 1);
    }
    void upd(int p, int k){ // a[p] += k
        while (p <= n){
            bit[p] += k;
            p |= (p + 1);
        }
    }
    ll get(int v){ // a[1] + ... + a[v]
        ll out = 0;
        while (v > 0){
            out += bit[v];
            v = (v & (v + 1)) - 1;
        }
        return out;
    }
};
 
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int n, q; cin>>n>>q;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++){
        cin>>a[i];
    }
    if (q > 1) return 0;
    vector<arr3> all;
    for (int i = 1; i <= q; i++){
        int l, r; cin>>l>>r;
        all.pb({l, r, i});
    }
    const int sz = sqrt(n);
    auto cmp = [&](arr3& x, arr3& y){
        if (x[0] / sz == x[1] / sz){
            return x[1] < y[1];
        }
        return x[0] < y[0];
    };
    
    sort(all.begin(), all.end(), cmp);
    
    int nn = 2 * n;
    FT T(A), Ts(nn), Tp(nn), T1(nn);
    set<int> st[A + 1];
    vector<int> cnt(A + 1);
    int l = n, r = n - 1;
    bool ind;
    
    ll sum = 0;
    auto add = [&](int i){
        if (!i) return;
        
        int k = cnt[a[i]];
        sum += (k + 1);
        
        int f = (int) T.get(1);
        if (f % 2 == 0){
            ind = 1;
        }
        else {
            f = (f / 2 + 1);
            // [l, l + f - 1]
            int cnt = (int) (T1.get(l + f - 1) - T1.get(l - 1));
            ind = (cnt != f);
        }
        
        int p;
        if (!k){
            p = ((ind) ? (l - 1) : (r + 1));
        }
        else {
            int m = (l + r) / 2;
            auto it = st[k].lower_bound(m);
            if (it == st[k].end()){
                p = *prev(it);
            }
            else if (it == st[k].begin()){
                p = *it;
            }
            else {
                auto it1 = prev(it);
                if (abs(*it1 - m) < abs(*it - m)){
                    p = *it1;
                }
                else {
                    p = *it;
                }
            }
        }
        
        sum += (1LL * (p + 1) * Ts.get(p - 1) - Tp.get(p - 1));
        sum += ((Tp.get(nn) - Tp.get(p)) - 1LL * (p - 1) * (Ts.get(nn) - Ts.get(p)));
        
        Ts.upd(p, 1);
        Tp.upd(p, p);
        
        if (!k){
            if (ind) l--;
            else r++;
        }
        else {
            T.upd(k, -1);
            st[k].erase(p);
            if (k == 1) T1.upd(p, -1);
        }
        cnt[a[i]]++; k++;
        T.upd(k, 1);
        st[k].ins(p);
        if (k == 1) T1.upd(p, 1);
        // scout<<"Yo "<<l<<" "<<r<<" "<<p<<"\n";
    };
    
    auto rem = [&](int i){
        
    };
    
    vector<ll> out(q + 1);
    int lc = 0, rc = 0;
    for (int i = 0; i < q; i++){
        auto &[l, r, ii] = all[i];
        
        while (lc > l) add(--lc);
        while (rc < r) add(++rc);
        while (lc < l) rem(lc++);
        while (rc > r) rem(rc--);
        
        out[ii] = sum;
    }
    
    for (int i = 1; i <= q; i++){
        cout<<out[i]<<"\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 15 ms 18008 KB Output is correct
2 Correct 11 ms 17868 KB Output is correct
3 Correct 12 ms 18004 KB Output is correct
4 Correct 10 ms 17868 KB Output is correct
5 Correct 10 ms 18008 KB Output is correct
6 Correct 11 ms 18012 KB Output is correct
7 Correct 10 ms 18008 KB Output is correct
8 Correct 10 ms 18012 KB Output is correct
9 Correct 10 ms 17932 KB Output is correct
10 Correct 9 ms 18008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 18012 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 18012 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 13 ms 18012 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 18008 KB Output is correct
2 Correct 11 ms 17868 KB Output is correct
3 Correct 12 ms 18004 KB Output is correct
4 Correct 10 ms 17868 KB Output is correct
5 Correct 10 ms 18008 KB Output is correct
6 Correct 11 ms 18012 KB Output is correct
7 Correct 10 ms 18008 KB Output is correct
8 Correct 10 ms 18012 KB Output is correct
9 Correct 10 ms 17932 KB Output is correct
10 Correct 9 ms 18008 KB Output is correct
11 Incorrect 13 ms 18012 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 15 ms 18008 KB Output is correct
2 Correct 11 ms 17868 KB Output is correct
3 Correct 12 ms 18004 KB Output is correct
4 Correct 10 ms 17868 KB Output is correct
5 Correct 10 ms 18008 KB Output is correct
6 Correct 11 ms 18012 KB Output is correct
7 Correct 10 ms 18008 KB Output is correct
8 Correct 10 ms 18012 KB Output is correct
9 Correct 10 ms 17932 KB Output is correct
10 Correct 9 ms 18008 KB Output is correct
11 Incorrect 13 ms 18012 KB Output isn't correct
12 Halted 0 ms 0 KB -