結果

問題 No.2338 Range AtCoder Query
ユーザー rniyarniya
提出日時 2023-06-02 23:00:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 7,099 bytes
コンパイル時間 3,502 ms
コンパイル使用メモリ 221,848 KB
実行使用メモリ 13,888 KB
最終ジャッジ日時 2024-06-09 00:55:23
合計ジャッジ時間 9,902 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,888 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 6 ms
5,376 KB
testcase_07 AC 5 ms
5,376 KB
testcase_08 AC 6 ms
5,376 KB
testcase_09 AC 5 ms
5,376 KB
testcase_10 AC 6 ms
5,376 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif

struct Mo {
    Mo(int n) : n(n) {}

    void add(int l, int r) {
        assert(l <= r);
        left.emplace_back(l);
        right.emplace_back(r);
    }

    template <typename AL, typename AR, typename DL, typename DR, typename REM>
    void run(const AL& add_left, const AR& add_right, const DL& del_left, const DR del_right, const REM& rem) {
        int q = left.size(), width = n / std::min(std::max<int>(sqrt(q * 2 / 3), 1), n);
        std::vector<int> order(q);
        std::iota(order.begin(), order.end(), 0);
        std::sort(order.begin(), order.end(), [&](int a, int b) {
            int ablock = left[a] / width, bblock = left[b] / width;
            if (ablock != bblock) return ablock < bblock;
            return (ablock & 1) ? (right[a] > right[b]) : (right[a] < right[b]);
        });

        int l = 0, r = 0;
        for (auto idx : order) {
            while (l > left[idx]) add_left(--l);
            while (r < right[idx]) add_right(r++);
            while (l < left[idx]) del_left(l++);
            while (r > right[idx]) del_right(--r);
            rem(idx);
        }
    }

    template <typename A, typename D, typename REM> void run(const A& add, const D& del, const REM& rem) {
        run(add, add, del, del, rem);
    }

private:
    int n;
    std::vector<int> left, right;
};

template <typename T, int MAX_LOG> struct BinaryTrie {
    struct Node {
        std::array<int, 2> nxt;
        int count;
        Node() : nxt{-1, -1}, count(0) {}
    };

    std::vector<Node> nodes;

    inline int& next(int i, int j) { return nodes[i].nxt[j]; }

    BinaryTrie() { nodes.emplace_back(); }

    void insert(const T& x) {
        int cur = 0;
        for (int i = MAX_LOG - 1; i >= 0; i--) {
            int f = x >> i & 1;
            int nxt = next(cur, f);
            if (nxt == -1) {
                nxt = nodes.size();
                next(cur, f) = nxt;
                nodes.emplace_back();
            }
            nodes[cur].count++;
            cur = nxt;
        }
        nodes[cur].count++;
    }

    void erase(const T& x) {
        assert(count(x));
        int cur = 0;
        for (int i = MAX_LOG - 1; i >= 0; i--) {
            int f = x >> i & 1;
            nodes[cur].count--;
            cur = next(cur, f);
        }
        nodes[cur].count--;
    }

    int find(const T& x) {
        int cur = 0;
        for (int i = MAX_LOG - 1; i >= 0; i--) {
            int f = x >> i & 1;
            cur = next(cur, f);
            if (cur == -1) return -1;
        }
        return cur;
    }

    int count(const T& x) {
        int idx = find(x);
        return idx == -1 ? 0 : nodes[idx].count;
    }

    T min_element(const T& xor_val = 0) {
        int cur = 0;
        T res = 0;
        for (int i = MAX_LOG - 1; i >= 0; i--) {
            int f = xor_val >> i & 1;
            int l = next(cur, f), r = next(cur, f ^ 1);
            if (l == -1 or nodes[l].count == 0) {
                cur = r;
                res |= T(f ^ 1) << i;
            } else {
                cur = l;
                res |= T(f) << i;
            }
        }
        return res;
    }

    T max_element(const T& xor_val = 0) { return min_element(~xor_val); }

    T kth_element(int k, const T& xor_val = 0) const {
        int cur = 0;
        T res = 0;
        for (int i = MAX_LOG - 1; i >= 0; i--) {
            int f = xor_val >> i & 1;
            int l = next(cur, f), r = next(cur, f ^ 1);
            if (l == -1 or nodes[l].count <= k) {
                cur = r;
                k -= (l == -1 ? 0 : nodes[l].count);
                res |= T(f ^ 1) << i;
            } else {
                cur = l;
                res |= T(f) << i;
            }
        }
        return res;
    }

    int count_less(const T& x, const T& xor_val = 0) {
        int cur = 0;
        int res = 0;
        for (int i = MAX_LOG - 1; i >= 0; i--) {
            int f = xor_val >> i & 1, g = x >> i & 1;
            int l = next(cur, f), r = next(cur, f ^ 1);
            if (f != g and l != -1) res += nodes[l].count;
            cur = next(cur, g);
            if (cur == -1) break;
        }
        return res;
    }
};

using namespace std;

typedef long long ll;
#define all(x) begin(x), end(x)
constexpr int INF = (1 << 30) - 1;
constexpr long long IINF = (1LL << 60) - 1;
constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

template <class T> istream& operator>>(istream& is, vector<T>& v) {
    for (auto& x : v) is >> x;
    return is;
}

template <class T> ostream& operator<<(ostream& os, const vector<T>& v) {
    auto sep = "";
    for (const auto& x : v) os << exchange(sep, " ") << x;
    return os;
}

template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = forward<U>(y), true); }

template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = forward<U>(y), true); }

template <class T> void mkuni(vector<T>& v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

template <class T> int lwb(const vector<T>& v, const T& x) { return lower_bound(begin(v), end(v), x) - begin(v); }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, M, Q;
    cin >> N >> M >> Q;
    vector<int> P(N), S(N);
    for (int i = 0; i < N; i++) {
        string tmp;
        cin >> P[i] >> tmp;
        P[i]--;
        S[i] = (tmp == "AC");
    }

    Mo mo(N);
    for (int i = 0; i < Q; i++) {
        int L, R;
        cin >> L >> R;
        mo.add(--L, R);
    }
    vector<set<int>> ac(M), wa(M);
    vector<BinaryTrie<int, 20>> bt(M);
    int correct = 0, penalty = 0;
    auto calc = [&](int idx) {
        if (ac[idx].empty()) return 0;
        int x = *ac[idx].begin();
        if (int(wa[idx].size()) < 10) {
            int res = 0;
            for (auto val : wa[idx]) {
                if (val > x) break;
                res++;
            }
            return res;
        }
        return bt[idx].count_less(x);
    };
    auto add = [&](int idx) {
        int x = P[idx];
        {
            correct -= (not ac[x].empty());
            penalty -= calc(x);
        }
        if (S[idx] == 0) {
            wa[x].emplace(idx);
            bt[x].insert(idx);
        } else
            ac[x].emplace(idx);
        {
            correct += (not ac[x].empty());
            penalty += calc(x);
        }
    };
    auto del = [&](int idx) {
        int x = P[idx];
        {
            correct -= (not ac[x].empty());
            penalty -= calc(x);
        }
        if (S[idx] == 0) {
            wa[x].erase(idx);
            bt[x].erase(idx);
        } else
            ac[x].erase(idx);
        {
            correct += (not ac[x].empty());
            penalty += calc(x);
        }
    };
    vector<int> ans1(Q), ans2(Q);
    auto rem = [&](int idx) {
        ans1[idx] = correct;
        ans2[idx] = penalty;
    };

    mo.run(add, del, rem);
    for (int i = 0; i < Q; i++) cout << ans1[i] << ' ' << ans2[i] << '\n';
    return 0;
}
0