結果

問題 No.1558 Derby Live
ユーザー tnakao0123tnakao0123
提出日時 2021-06-28 13:28:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 2,865 bytes
コンパイル時間 2,101 ms
コンパイル使用メモリ 97,688 KB
実行使用メモリ 6,108 KB
最終ジャッジ日時 2023-09-07 18:23:33
合計ジャッジ時間 6,829 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
5,916 KB
testcase_01 AC 114 ms
5,872 KB
testcase_02 AC 4 ms
5,852 KB
testcase_03 AC 78 ms
5,932 KB
testcase_04 AC 34 ms
5,796 KB
testcase_05 AC 38 ms
5,868 KB
testcase_06 AC 66 ms
5,868 KB
testcase_07 AC 4 ms
5,872 KB
testcase_08 AC 62 ms
5,804 KB
testcase_09 AC 65 ms
5,852 KB
testcase_10 AC 68 ms
5,884 KB
testcase_11 AC 70 ms
5,868 KB
testcase_12 AC 74 ms
5,984 KB
testcase_13 AC 77 ms
5,932 KB
testcase_14 AC 80 ms
5,792 KB
testcase_15 AC 85 ms
5,980 KB
testcase_16 AC 88 ms
5,792 KB
testcase_17 AC 90 ms
5,920 KB
testcase_18 AC 94 ms
5,808 KB
testcase_19 AC 97 ms
5,804 KB
testcase_20 AC 102 ms
5,792 KB
testcase_21 AC 94 ms
5,988 KB
testcase_22 AC 99 ms
5,916 KB
testcase_23 AC 101 ms
5,872 KB
testcase_24 AC 95 ms
5,876 KB
testcase_25 AC 98 ms
5,876 KB
testcase_26 AC 101 ms
6,108 KB
testcase_27 AC 94 ms
5,796 KB
testcase_28 AC 96 ms
5,932 KB
testcase_29 AC 101 ms
5,852 KB
testcase_30 AC 94 ms
5,988 KB
testcase_31 AC 97 ms
5,928 KB
testcase_32 AC 101 ms
5,792 KB
testcase_33 AC 3 ms
5,884 KB
testcase_34 AC 4 ms
5,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1558.cc:  No.1558 Derby Live - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 18;
const int MAX_M = 10000;
const int MAX_E2 = 1 << 15; // = 32768

/* typedef */

typedef int vec[MAX_N];

struct Elm {
  vec ps;
  Elm(): ps() {}

  void init(int n) {
    iota(ps, ps + n, 0);
    fill(ps + n, ps + MAX_N, -1);
  }

  Elm operator+(const Elm &e) const {
    Elm r;
    for (int i = 0; i < MAX_N; i++)
      r.ps[i] = (ps[i] >= 0) ? e.ps[ps[i]] : -1;
    return r;
  }
};

Elm fop(Elm a, Elm b) { return a + b; }

template <typename T, T (*fop)(T,T), const int MAX_E2>
struct SegTreeOp {
  int e2;
  T nodes[MAX_E2], defv;
  SegTreeOp() {}

  void init(int n, T _defv) {
    defv = _defv;
    for (e2 = 1; e2 < n; e2 <<= 1);
    fill(nodes, nodes + MAX_E2, defv);
  }

  T &geti(int i) { return nodes[e2 - 1 + i]; }
  void seti(int i, T v) { geti(i) = v; }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = fop(nodes[j * 2 + 1], nodes[j * 2 + 2]);
  }

  void set(int i, T v) {
    int j = e2 - 1 + i;
    nodes[j] = v;
    while (j > 0) {
      j = (j - 1) / 2;
      nodes[j] = fop(nodes[j * 2 + 1], nodes[j * 2 + 2]);
    }
  }

  T op_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return defv;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    int im = (i0 + i1) / 2;
    T v0 = op_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = op_range(r0, r1, k * 2 + 2, im, i1);
    return fop(v0, v1);
  }
  T op_range(int r0, int r1) { return op_range(r0, r1, 0, 0, e2); }
};

/* global variables */

SegTreeOp<Elm,fop,MAX_E2> st;

/* subroutines */

void printvec(int n, vec v) {
  for (int i = 0; i < n; i++)
    printf("%d%c", v[i] + 1, (i + 1 < n) ? ' ' : '\n');
}

/* main */

int main() {
  int n, m, qn;
  scanf("%d%d%d", &n, &m, &qn);

  Elm e0; e0.init(n);
  st.init(m, e0);

  while (qn--) {
    int op;
    scanf("%d", &op);

    if (op == 1) {
      int d;
      scanf("%d", &d), d--;

      Elm e;
      for (int i = 0; i < n; i++) scanf("%d", e.ps + i), e.ps[i]--;
      fill(e.ps + n, e.ps + MAX_N, -1);

      st.set(d, e);
    }
    else if (op == 2) {
      int s;
      scanf("%d", &s);

      Elm e = st.op_range(0, s);
      vec v;
      for (int i = 0; i < n; i++) v[e.ps[i]] = i;
      printvec(n, v);
    }
    else {
      int l, r;
      scanf("%d%d", &l, &r), l--;

      Elm e = st.op_range(l, r);

      int sum = 0;
      for (int i = 0; i < n; i++) sum += abs(i - e.ps[i]);
      printf("%d\n", sum);
    }
  }
  return 0;
}
0