Submission #469538

# Submission time Handle Problem Language Result Execution time Memory
469538 2021-09-01T09:20:44 Z Lam_lai_cuoc_doi Bubble Sort 2 (JOI18_bubblesort2) C++17
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

template <class T>
void read(T &x)
{
    x = 0;
    register int c;
    while ((c = getchar()) && (c > '9' || c < '0'))
        ;
    for (; c >= '0' && c <= '9'; c = getchar())
        x = x * 10 + c - '0';
}

constexpr bool typetest = 0;
constexpr int N = 5e5 + 5;
constexpr int Inf = 1e9 + 7;

struct FenwickTree
{
    int a[N * 2];
    int n;
    FenwickTree()
    {
        memset(a, 0, sizeof a);
    }
    void Update(int p, int v)
    {
        for (; p <= n; p += p & -p)
            a[p] += v;
    }
    int Get(int p)
    {
        int ans(0);
        for (; p; p -= p & -p)
            ans += a[p];
        return ans;
    }
} g;

struct SegmentTree
{
    int n;
    int lazy[N * 8], st[N * 8];

    SegmentTree()
    {
        memset(lazy, 0, sizeof lazy);
        fill_n(st, N * 8, -Inf);
    }
    void Push(int id)
    {
        if (lazy[id] != 0)
        {
            if (st[id << 1] != -Inf)
                st[id << 1] += lazy[id];
            lazy[id << 1] += lazy[id];
            if (st[id << 1 | 1] != -Inf)
                st[id << 1 | 1] += lazy[id];
            lazy[id << 1 | 1] += lazy[id];
            lazy[id] = 0;
        }
    }
    void Update(int id, int l, int r, const int &x, const int &v)
    {
        if (l > x || r < x)
            return;
        if (l == x && r == x)
        {
            st[id] = v;
            return;
        }
        Push(id);
        Update(id << 1, l, (l + r) / 2, x, v);
        Update(id << 1 | 1, (l + r) / 2 + 1, r, x, v);
        st[id] = max(st[id << 1], st[id << 1 | 1]);
    }
    void Update(int x, int v)
    {
        Update(1, 1, n, x, v);
    }
    void Update(int id, int l, int r, const int &a, const int &b, const int &v)
    {
        if (l > b || r < a)
            return;
        if (l >= a && r <= b)
        {
            st[id] += v;
            lazy[id] += v;
            return;
        }
        Push(id);
        Update(id << 1, l, (l + r) / 2, a, b, v);
        Update(id << 1 | 1, (l + r) / 2 + 1, r, a, b, v);
        st[id] = max(st[id << 1], st[id << 1 | 1]);
    }
    void Update(int l, int r, int v)
    {
        Update(1, 1, n, l, r, v);
    }
} f;

vector<int> s;
set<int> pos[N * 2];
int n, m;

#define Find(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin() + 1)

vector<int> countScans(vector<int> a, vector<int> x, vector<int> v)
{
    vector<int> ans(x.size(), 0);

    n = a.size();
    m = x.size();

    for (int i = 0; i < n; ++i)
        s.emplace_back(a[i]);
    for (int i = 0; i < m; ++i)
        s.emplace_back(v[i]);

    sort(s.begin(), s.end());
    s.resize(unique(s.begin(), s.end()) - s.begin());
    f.n = g.n = s.size();

    for (int i = 0; i < n; ++i)
    {
        a[i] = Find(s, a[i]);
        g.Update(a[i], 1);
        pos[a[i]].insert(i);
    }

    for (int i = 0; i < (int)s.size(); ++i)
        if (!pos[i + 1].empty())
            f.Update(i + 1, *pos[i + 1].rbegin() + 1 - g.Get(i + 1));

    for (int i = 0; i < m; ++i)
    {
        v[i] = Find(s, v[i]);

        pos[a[x[i]]].erase(x[i]);
        f.Update(a[x[i]], s.size(), 1);
        g.Update(a[x[i]], -1);

        if (pos[a[x[i]]].empty())
            f.Update(a[x[i]], -Inf);
        else
            f.Update(a[x[i]], *pos[a[x[i]]].rbegin() + 1 - g.Get(a[x[i]]));

        a[x[i]] = v[i];

        pos[a[x[i]]].insert(x[i]);
        f.Update(a[x[i]], s.size(), -1);
        g.Update(a[x[i]], 1);
        f.Update(v[i], *pos[v[i]].rbegin() + 1 - g.Get(v[i]));

        ans[i] = f.st[1];
    }

    return ans;
}

void Read()
{
    cin >> n >> m;
    vector<int> a(n), x(m), v(m);

    for (auto &i : a)
        cin >> i;
    for (int i = 0; i < m; ++i)
        cin >> x[i] >> v[i];

    vector<int> ans = countScans(a, x, v);

    for (int i = 0; i < m; ++i)
        cout << ans[i] << "\n";
}

void Solve()
{
}

int32_t main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t(1);
    if (typetest)
        cin >> t;
    for (int _ = 1; _ <= t; ++_)
    {
        //cout << "Case #" << _ << ": ";
        Read();
        Solve();
    }
    //cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}

Compilation message

bubblesort2.cpp: In function 'void read(T&)':
bubblesort2.cpp:12:18: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   12 |     register int c;
      |                  ^
/usr/bin/ld: /tmp/ccGPICpG.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccuFU0cF.o:bubblesort2.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status