結果

問題 No.1558 Derby Live
ユーザー magstamagsta
提出日時 2021-05-21 16:02:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 3,239 bytes
コンパイル時間 6,124 ms
コンパイル使用メモリ 199,760 KB
実行使用メモリ 10,404 KB
最終ジャッジ日時 2023-09-07 13:57:31
合計ジャッジ時間 14,280 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 271 ms
10,336 KB
testcase_01 AC 202 ms
10,340 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 162 ms
4,380 KB
testcase_04 AC 67 ms
5,396 KB
testcase_05 AC 77 ms
5,060 KB
testcase_06 AC 138 ms
5,340 KB
testcase_07 AC 5 ms
5,276 KB
testcase_08 AC 131 ms
6,724 KB
testcase_09 AC 136 ms
6,716 KB
testcase_10 AC 141 ms
7,184 KB
testcase_11 AC 146 ms
7,128 KB
testcase_12 AC 153 ms
7,924 KB
testcase_13 AC 163 ms
7,920 KB
testcase_14 AC 162 ms
8,508 KB
testcase_15 AC 172 ms
8,580 KB
testcase_16 AC 175 ms
9,476 KB
testcase_17 AC 186 ms
9,360 KB
testcase_18 AC 206 ms
9,824 KB
testcase_19 AC 213 ms
9,844 KB
testcase_20 AC 222 ms
10,376 KB
testcase_21 AC 211 ms
10,036 KB
testcase_22 AC 217 ms
10,008 KB
testcase_23 AC 225 ms
10,352 KB
testcase_24 AC 204 ms
9,824 KB
testcase_25 AC 214 ms
9,760 KB
testcase_26 AC 225 ms
10,308 KB
testcase_27 AC 209 ms
9,816 KB
testcase_28 AC 219 ms
9,776 KB
testcase_29 AC 221 ms
10,356 KB
testcase_30 AC 207 ms
9,888 KB
testcase_31 AC 214 ms
9,816 KB
testcase_32 AC 225 ms
10,404 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include "testlib.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() {
    registerValidation();
    long long N = inf.readLong((long long)2, (long long)18, "N");
    inf.readSpace();
    long long M = inf.readLong((long long)2, (long long)10000, "M");
    inf.readSpace();
    long long Q = inf.readLong((long long)2, (long long)50000, "Q");
    inf.readEoln();
    SegTree<long long> Seg(M, N);
    Seg.init();
    for (int i = 0; i < Q; i++) {
        int com = inf.readInt(1, 3);
        if (com == 1) {
            inf.readSpace();
            long long B = inf.readLong((long long)1, M, "B");
            vector<long long> P(N);
            for (int j = 0; j < N; j++) {
                inf.readSpace();
                P[j] = inf.readLong((long long)1, N, "P");
            }
            for (int j = 0; j < N; j++) P[j]--;
            vector<bool> flag(N, false);
            for (int j = 0; j < N; j++) {
                if (flag[P[j]]) cout << -1 << endl;
                else flag[P[j]] = true;
            }
            Seg.update(B - 1, P);
        }
        if (com == 2) {
            inf.readSpace();
            long long S = inf.readLong((long long)1, M, "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) {
            inf.readSpace();
            long long a = inf.readLong((long long)1, M, "L");
            inf.readSpace();
            long long b = inf.readLong(a, M, "R");
            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;
        }
        inf.readEoln();
    }
    inf.readEof();
}
0