Submission #1013729

#TimeUsernameProblemLanguageResultExecution timeMemory
1013729vjudge1Bubble Sort 2 (JOI18_bubblesort2)C++17
17 / 100
33 ms3480 KiB
#include "bubblesort2.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector <ll>;
using vi = vector <int>;
// using ii = pair <ll, ll>;
// using vii = vector <ii>;

const ll MAXN = 1E4+16, INF = ll(1E18)+16;
priority_queue <ll> pq[MAXN];

struct Fenwick {
    vll tree;
    ll n;

    Fenwick (ll n): tree(n, 0), n(n) {}

    void update (ll id, ll val) {
        for (; id < n; id |= id+1) tree[id] += val;
    }

    ll query (ll id) {
        ll ans = 0;
        for (; id >= 0; id &= id+1, id--) ans += tree[id];
        return ans;
    }
};

struct SegTree {
    vll tree;
    ll n;

    SegTree (ll n): tree(2*n, 0), n(n) {}

    void update (ll id, ll val) {
        tree[id] = val;
    }

    void update (ll ql, ll qr, ll val) {
        for (ll i = ql; i <= qr; i++) tree[i] += val;
    }

    ll query (ll ql, ll qr) {
        ll ans = -INF;
        for (ll i = ql; i <= qr; i++) ans = max(ans, tree[i]);
        return ans;
    }
};

vi countScans (vi a, vi wh, vi val) {
    vll ve(a.begin(), a.end());
    ll n = a.size();
    ll Q = wh.size();
    {vll th = ve;
    for (int i : val) th.push_back(i);
    sort(th.begin(), th.end());
    th.resize(unique(th.begin(), th.end()) - th.begin());
    for (ll &i : ve) i = lower_bound(th.begin(), th.end(), i) - th.begin();
    for (int &i : val) i = lower_bound(th.begin(), th.end(), i) - th.begin();}
    Fenwick ftFreq(MAXN);
    SegTree stAns(MAXN);
    vi ans;
    for (ll i = 0; i < n; i++) {
        pq[ve[i]].push(i);
        ftFreq.update(ve[i], +1);
    }
    auto redo = [&](ll i) {
        while (pq[i].size() && ve[pq[i].top()] != i) pq[i].pop();
        stAns.update(i, -INF);
        if (!pq[i].size()) return;
        stAns.update(i, pq[i].top()+1 - ftFreq.query(i));
    };
    for (ll i = 0; i < n+wh.size(); i++) redo(i);
    // for (ll i = 0; i < n+wh.size(); i++) cerr << stAns.query(0, i) << ' ';
    // cerr << '\n';
    for (ll q = 0; q < Q; q++) {
        ll val1 = ve[wh[q]], val2 = val[q];
        ftFreq.update(ve[wh[q]], -1);
        ve[wh[q]] = val[q];
        ftFreq.update(ve[wh[q]], +1);
        pq[val[q]].push(wh[q]);
        stAns.update(val1, MAXN-4, +1);
        stAns.update(val2, MAXN-4, -1);
        redo(val1);
        redo(val2);
        // cerr << val1 << ' ' << val2 << '\n';
        // for (ll i : ve) cerr << i << ' '; cerr << '\n';
        // for (ll i = 0; i < n+wh.size(); i++) cerr << stAns.query(i, i) << ' ';
        // cerr << '\n';
        ans.push_back(stAns.query(0, MAXN-8));
    }
    return ans;
}

Compilation message (stderr)

bubblesort2.cpp: In function 'vi countScans(vi, vi, vi)':
bubblesort2.cpp:74:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'long long unsigned int' [-Wsign-compare]
   74 |     for (ll i = 0; i < n+wh.size(); i++) redo(i);
      |                    ~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...