답안 #1106435

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1106435 2024-10-30T10:40:08 Z atom Izbori (COCI22_izbori) C++17
0 / 110
245 ms 3012 KB
#include "bits/stdc++.h"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;

#ifdef JASPER
#include "debug.h"
#else
#define debug(...) 166
#endif

const int N = 2e5 + 5;
const int B = 450;
int a[N];
int n;

bool sub12() {
    return (n <= 2000);
}

bool sub3() {
    for (int i = 1; i <= n; ++i)
        if (a[i] > 2) return false;
    return true;
}

template <typename T>
struct Fenwick {
    vector <int> f;
    int n;
    Fenwick(int _n) {
        f.resize(_n + 5);
        n = _n;
    }

    void upd(int x, int val) {
        for (; x <= n; x += x & -x)
            f[x] += val;
    }

    int get(int x) {
        int ans = 0;
        for (; x > 0; x -= x & -x)
            ans += f[x];
        return ans;
    }
};

signed main() {
    cin.tie(0) -> sync_with_stdio(0);
    
    #ifdef JASPER
        freopen("in1", "r", stdin);
    #endif

    cin >> n;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];

    vector <int> vals;
    for (int i = 1; i <= n; ++i)
        vals.push_back(a[i]);

    sort(vals.begin(), vals.end());
    vals.resize(unique(vals.begin(), vals.end()) - vals.begin());

    vector <int> cnt(n, 0);
    for (int i = 1; i <= n; ++i) {
        a[i] = (int) (lower_bound(vals.begin(), vals.end(), a[i]) - vals.begin() + 1);
        cnt[a[i]]++;
    }

    vector <bool> heavy(n, false);
    for (int i = 1; i <= n; ++i) {
        if (cnt[i] >= B)
            heavy[i] = true;
    }

    ll ans = 0;
    // processing light queries: all elements appear less than sqrt(n)
    cnt.clear();
    for (int i = 1; i <= n; ++i) {
        int mx = 0;
        for (int j = i; j <= n && (j - i + 1) / 2 < B; ++j) {
            if (!heavy[a[j]]) {
                cnt[a[j]]++;
                if (cnt[a[j]] > mx) mx = cnt[a[j]];
                if (mx > (j - i + 1) / 2) ++ans;
            }
        }

        for (int j = i; j <= n && (j - i + 1) / 2 < B; ++j)
            if (!heavy[a[j]]) cnt[a[j]]--;
    }

    // processing heavy queries: all elements appear at least sqrt(n)
    for (int x = 1; x <= n; ++x) {
        if (heavy[x]) {
            auto get = [&] (int _x) { debug(_x); return _x + n; };

            vector <int> f(2 * n + 5, 0);
            int sum = 0;
            int prf = 0;
            f[get(prf)]++;
            for (int i = 1; i <= n; ++i) {
                if (get(prf) > 0)
                    sum += f[get(prf - 1)];
                else    
                    sum -= f[get(prf)];
                ans += sum;
                f[get(prf)]++;
            }
        }
    }

    cout << ans << "\n";

    
    return 0;
}

Compilation message

Main.cpp: In lambda function:
Main.cpp:9:20: warning: statement has no effect [-Wunused-value]
    9 | #define debug(...) 166
      |                    ^~~
Main.cpp:99:39: note: in expansion of macro 'debug'
   99 |             auto get = [&] (int _x) { debug(_x); return _x + n; };
      |                                       ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 592 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 592 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 245 ms 3012 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 592 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -