結果

問題 No.1558 Derby Live
ユーザー magstamagsta
提出日時 2021-05-20 20:24:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 296 ms / 2,000 ms
コード長 2,378 bytes
コンパイル時間 1,744 ms
コンパイル使用メモリ 171,272 KB
実行使用メモリ 10,316 KB
最終ジャッジ日時 2023-09-07 13:56:27
合計ジャッジ時間 11,266 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 296 ms
10,240 KB
testcase_01 AC 236 ms
10,184 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 181 ms
4,384 KB
testcase_04 AC 76 ms
5,120 KB
testcase_05 AC 90 ms
5,060 KB
testcase_06 AC 158 ms
5,124 KB
testcase_07 AC 6 ms
5,168 KB
testcase_08 AC 148 ms
6,608 KB
testcase_09 AC 154 ms
6,616 KB
testcase_10 AC 159 ms
6,980 KB
testcase_11 AC 166 ms
6,968 KB
testcase_12 AC 176 ms
8,112 KB
testcase_13 AC 182 ms
7,756 KB
testcase_14 AC 189 ms
8,344 KB
testcase_15 AC 197 ms
8,448 KB
testcase_16 AC 203 ms
8,816 KB
testcase_17 AC 211 ms
8,864 KB
testcase_18 AC 240 ms
9,772 KB
testcase_19 AC 245 ms
9,772 KB
testcase_20 AC 255 ms
10,316 KB
testcase_21 AC 243 ms
9,612 KB
testcase_22 AC 248 ms
9,604 KB
testcase_23 AC 255 ms
10,312 KB
testcase_24 AC 241 ms
9,704 KB
testcase_25 AC 247 ms
9,720 KB
testcase_26 AC 259 ms
10,144 KB
testcase_27 AC 239 ms
9,600 KB
testcase_28 AC 246 ms
9,656 KB
testcase_29 AC 255 ms
10,248 KB
testcase_30 AC 243 ms
9,712 KB
testcase_31 AC 244 ms
9,600 KB
testcase_32 AC 255 ms
10,192 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <typename T>
struct SegTree {
    const T INF = numeric_limits<T>::max();
    int n, m;
    vector<vector<T>> dat;
    SegTree(int n_, int m_) : dat(n_ * 4, vector<T>(m_, 0)) {
        int x = 1;
        while (n_ > x) {
            x *= 2;
        }
        n = x;
        m = m_;
    }
    void init() {
        for (int i = 0; i < 2 * n - 1; i++) {
            for (int j = 0; j < m; j++) {
                dat[i][j] = j;
            }
        }
    }
    void update(int i, vector<T> x) {
        i += n - 1;
        for (int j = 0; j < m; j++) dat[i][j] = x[j];
        while (i > 0) {
            i = (i - 1) / 2;
            for (int j = 0; j < m; j++) dat[i][j] = dat[i * 2 + 2][dat[i * 2 + 1][j]];
        }
    }
    vector<T> query(int a, int b) {
        return query_sub(a, b, 0, 0, n);
    }
    vector<T> query_sub(int a, int b, int k, int l, int r) {
        vector<T> x(m);
        if (r <= a || b <= l) {
            for (int i = 0; i < m; i++) x[i] = i;
        }
        else if (a <= l && r <= b) {
            for (int i = 0; i < m; i++) x[i] = dat[k][i];
        }
        else {
            vector<T> vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
            vector<T> vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
            for (int i = 0; i < m; i++) x[i] = vr[vl[i]];
        }
        return x;
    }
};

int main() {
	long long N, M, Q; cin >> N >> M >> Q;
    SegTree<long long> Seg(M, N);
    Seg.init();
    for (int i = 0; i < Q; i++) {
        int com; cin >> com;
        if (com == 1) {
            long long B; cin >> B;
            vector<long long> P(N);
            for (int j = 0; j < N; j++) cin >> P[j];
            for (int j = 0; j < N; j++) P[j]--;
            Seg.update(B - 1, P);
        }
        if (com == 2) {
            long long S; cin >> S;
            auto x = Seg.query(0, S);
            vector<long long> y(N);
            for (int j = 0; j < N; j++) y[x[j]] = j;
            cout << y[0] + 1;
            for (int j = 1; j < N; j++) cout << " " << y[j] + 1;
            cout << endl;
        }
        if (com == 3) {
            long long a, b; cin >> a >> b;
            auto x = Seg.query(a - 1, b);
            long long count = 0;
            for (int j = 0; j < N; j++) count += abs(x[j] - j);
            cout << count << endl;
        }
    }
}
0