結果

問題 No.2568 列辞書順列列
ユーザー soto800soto800
提出日時 2023-12-02 19:33:54
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 5,539 bytes
コンパイル時間 3,574 ms
コンパイル使用メモリ 260,660 KB
実行使用メモリ 10,164 KB
最終ジャッジ日時 2023-12-02 19:34:28
合計ジャッジ時間 24,128 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,508 KB
testcase_01 AC 4 ms
7,508 KB
testcase_02 AC 176 ms
8,600 KB
testcase_03 AC 175 ms
8,600 KB
testcase_04 AC 393 ms
10,164 KB
testcase_05 AC 393 ms
10,164 KB
testcase_06 AC 392 ms
10,164 KB
testcase_07 AC 393 ms
10,164 KB
testcase_08 AC 392 ms
10,164 KB
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
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 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

// #include <atcoder/all>

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x) \
    if (DEBUG) cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y)                                                          \
    if (DEBUG)                                                              \
        cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y \
             << endl;
#define LOG3(x, y, z)                                                       \
    if (DEBUG)                                                              \
        cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y \
             << " " << TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z)                                                      \
    if (DEBUG)                                                                \
        cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x   \
             << " " << TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" \
             << z << endl;

template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}
const int mod = 998244353;
class mint {
    long long x;

   public:
    mint(long long x = 0) : x((x % mod + mod) % mod) {}
    mint operator-() const { return mint(-x); }
    mint& operator+=(const mint& a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint& a) {
        if ((x += mod - a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint& a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint& a) const {
        mint res(*this);
        return res += a;
    }
    mint operator-(const mint& a) const {
        mint res(*this);
        return res -= a;
    }
    mint operator*(const mint& a) const {
        mint res(*this);
        return res *= a;
    }
    mint pow(lli t) const {
        if (!t) return 1;
        mint a = pow(t >> 1);
        a *= a;
        if (t & 1) a *= *this;
        return a;
    }
    // for prime mod
    mint inv() const { return pow(mod - 2); }
    mint& operator/=(const mint& a) { return (*this) *= a.inv(); }
    mint operator/(const mint& a) const {
        mint res(*this);
        return res /= a;
    }

    friend ostream& operator<<(ostream& os, const mint& m) {
        os << m.x;
        return os;
    }
};
/* from https://nyaannyaan.github.io/library/misc/mo.hpp.html*/
struct Mo {
    int width;
    vector<int> left, right, order;

    Mo(int N, int Q) : order(Q) {
        width = max<int>(1, 1.0 * N / max<double>(1.0, sqrt(Q * 2.0 / 3.0)));
        iota(begin(order), end(order), 0);
    }

    void insert(int l, int r) { /* [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& delete_left,
             const DR& delete_right, const REM& rem) {
        assert(left.size() == order.size());
        sort(begin(order), end(order), [&](int a, int b) {
            int ablock = left[a] / width, bblock = left[b] / width;
            if (ablock != bblock) return ablock < bblock;
            if (ablock & 1) return right[a] < right[b];
            return right[a] > right[b];
        });
        int nl = 0, nr = 0;
        for (auto idx : order) {
            while (nl > left[idx]) add_left(--nl);
            while (nr < right[idx]) add_right(nr++);
            while (nl < left[idx]) delete_left(nl++);
            while (nr > right[idx]) delete_right(--nr);
            rem(idx);
        }
    }
};

/* ABC174-F https://atcoder.jp/contests/abc174/tasks/abc174_f*/
mint anss[500100];

// Generated by 2.12.0 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
    lli N, M, Q;
    cin >> N >> M >> Q;

    vector<lli> a(N);
    REP(i, 0, N) { cin >> a[i]; }

    Mo mo(N, Q);

    REP(i, 0, Q) {
        int l, r;
        cin >> l >> r;

        // 1-index -> 0-index
        mo.insert(l - 1, r);
    }

    mint ans = 0;
    lli length = 0;
    mint base = 1;
    auto al = [&](int nowIndex) {
        mint now = a[nowIndex];
        mint offset = (now - 1) * base;
        LOG2(nowIndex, offset);
        ans += offset;
        base *= M;
    };
    auto ar = [&](int nowIndex) {
        int now = a[nowIndex];
        base *= M;
        LOG3(ans, now, ans * M);
        ans *= M;

        ans += (now - 1);
    };
    auto el = [&](int nowIndex) {
        LOG("el");
        base /= M;
        mint now = a[nowIndex];
        mint offset = (now - 1) * base;
        ans -= offset;
    };
    auto er = [&](int nowIndex) {
        LOG("er");
        int now = a[nowIndex];
        ans -= (now - 1);
        ans /= M;
        base /= M;
    };
    auto q = [&](int i) {
        LOG2("q", i);
        anss[i] = ans;
    };

    mo.run(al, ar, el, er, q);

    REP(i, 0, Q) { cout << anss[i] + 1 << endl; }

    return 0;
}
0