結果

問題 No.1558 Derby Live
ユーザー milanis48663220milanis48663220
提出日時 2021-06-25 21:59:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 323 ms / 2,000 ms
コード長 3,179 bytes
コンパイル時間 1,432 ms
コンパイル使用メモリ 125,372 KB
実行使用メモリ 6,716 KB
最終ジャッジ日時 2023-09-07 14:30:29
合計ジャッジ時間 11,929 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 323 ms
6,468 KB
testcase_01 AC 237 ms
6,508 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 184 ms
4,380 KB
testcase_04 AC 106 ms
4,376 KB
testcase_05 AC 130 ms
4,380 KB
testcase_06 AC 208 ms
4,384 KB
testcase_07 AC 5 ms
4,384 KB
testcase_08 AC 207 ms
4,876 KB
testcase_09 AC 216 ms
5,496 KB
testcase_10 AC 217 ms
5,336 KB
testcase_11 AC 219 ms
5,384 KB
testcase_12 AC 225 ms
5,392 KB
testcase_13 AC 233 ms
5,920 KB
testcase_14 AC 233 ms
5,972 KB
testcase_15 AC 237 ms
5,868 KB
testcase_16 AC 243 ms
6,124 KB
testcase_17 AC 248 ms
6,440 KB
testcase_18 AC 252 ms
6,404 KB
testcase_19 AC 268 ms
6,496 KB
testcase_20 AC 267 ms
6,560 KB
testcase_21 AC 251 ms
6,484 KB
testcase_22 AC 260 ms
6,472 KB
testcase_23 AC 272 ms
6,560 KB
testcase_24 AC 255 ms
6,716 KB
testcase_25 AC 261 ms
6,468 KB
testcase_26 AC 269 ms
6,552 KB
testcase_27 AC 251 ms
6,556 KB
testcase_28 AC 260 ms
6,500 KB
testcase_29 AC 265 ms
6,484 KB
testcase_30 AC 251 ms
6,412 KB
testcase_31 AC 259 ms
6,460 KB
testcase_32 AC 267 ms
6,552 KB
testcase_33 AC 1 ms
4,384 KB
testcase_34 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template <typename T>
struct segtree{
    int n;
    T UNIT;
    vector<T> dat;
    T (*calc)(T, T);

    segtree(int n_, T unit, T (*_calc)(T, T)){
        UNIT = unit;
        calc = _calc;
        n = 1;
        while(n < n_) n *= 2;
        dat = vector<T>(2*n);
        for(int i = 0; i < 2*n; i++) dat[i] = UNIT;
    }

    void insert(int k, T a){
        dat[k+n-1] = a;
    }
    void update_all(){
        for(int i = n-2; i >= 0; i--){
            dat[i] = calc(dat[i*2+1], dat[i*2+2]);
        }
    }
    //k番目の値(0-indexed)をaに変更
    void update(int k, T a){
        k += n-1;
        dat[k] = a;
        while(k > 0){
            k = (k-1)/2;
            dat[k] = calc(dat[k*2+1], dat[k*2+2]);
        }
    }

    //[a, b)
    //区間[a, b]へのクエリに対してはquery(a, b+1)と呼ぶ
    T query(int a, int b, int k=0, int l=0, int r=-1){
        if(r < 0) r = n;
        if(r <= a || b <= l) return UNIT;
        if(a <= l && r <= b) return dat[k];
        else{
            T vl = query(a, b, k*2+1, l, (l+r)/2);
            T vr = query(a, b, k*2+2, (l+r)/2, r);
            return calc(vl, vr);
        }
    }
};

int f(vector<int> v){
    int ans = 0;
    for(int i = 0; i < v.size(); i++) ans += abs(v[i]-i);
    return ans;
}

vector<int> calc(vector<int> p1, vector<int> p2){
    int n = p1.size();
    vector<int> v1(n), v2(n);
    for(int i = 0; i < n; i++){
        v1[p1[i]] = i;
    }
    for(int i = 0; i < n; i++){
        v2[p2[i]] = v1[i];
    }
    vector<int> ans(n);
    for(int i = 0; i < n; i++) ans[v2[i]] = i;
    return ans;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, m, q; cin >> n >> m >> q;
    vector<int> unit(n);
    for(int i = 0; i < n; i++) unit[i] = i;
    segtree<vector<int>> sgt_ope(m, unit, calc);
    while(q--){
        int t; cin >> t;
        if(t == 1){
            int d; cin >> d; d--;
            vector<int> p(n);
            for(int i = 0; i < n; i++){
                cin >> p[i];
                p[i]--;
            }
            sgt_ope.update(d, p);
        }else if(t == 2){
            int s; cin >> s; s--;
            auto p = sgt_ope.query(0, s+1);
            vector<int> ans(n);
            for(int i = 0; i < n; i++) ans[p[i]] = i;
            for(int x: ans) cout << x+1 << ' ';
            cout << endl;
        }else{
            int l, r; cin >> l >> r; l--; r--;
            auto p = sgt_ope.query(l, r+1);
            cout << f(p) << endl;
        }
    }
}
0