結果

問題 No.778 クリスマスツリー
ユーザー tnakao0123tnakao0123
提出日時 2018-12-28 09:24:52
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 1,571 bytes
コンパイル時間 676 ms
コンパイル使用メモリ 88,048 KB
実行使用メモリ 17,664 KB
最終ジャッジ日時 2024-04-22 03:04:15
合計ジャッジ時間 1,830 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,984 KB
testcase_01 AC 4 ms
9,856 KB
testcase_02 AC 4 ms
9,856 KB
testcase_03 AC 4 ms
9,856 KB
testcase_04 AC 4 ms
9,728 KB
testcase_05 AC 5 ms
9,856 KB
testcase_06 AC 44 ms
17,604 KB
testcase_07 AC 29 ms
12,020 KB
testcase_08 AC 87 ms
14,464 KB
testcase_09 AC 85 ms
14,464 KB
testcase_10 AC 75 ms
14,720 KB
testcase_11 AC 74 ms
14,592 KB
testcase_12 AC 69 ms
14,564 KB
testcase_13 AC 44 ms
14,464 KB
testcase_14 AC 45 ms
17,664 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:76:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   76 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
main.cpp:80:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   80 |     scanf("%d", prts + i);
      |     ~~~~~^~~~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 778.cc:  No.778 クリスマスツリー - 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 = 200000;

/* typedef */

typedef long long ll;
typedef vector<int> vi;

template <typename T, const int MAX_N>
struct BIT {
  int n;
  T bits[MAX_N + 1];
  BIT() {}

  void init(int _n) {
    n = _n;
    memset(bits, 0, sizeof(bits));
  }

  T sum(int x) {
    T s = 0;
    while (x > 0) {
      s += bits[x];
      x -= (x & -x);
    }
    return s;
  }

  void add(int x, T v) {
    while (x <= n) {
      bits[x] += v;
      x += (x & -x);
    }
  }
};

/* global variables */

int prts[MAX_N], cis[MAX_N], avs[MAX_N];
vi clds[MAX_N];
BIT<int,MAX_N> bit;

/* subroutines */

/* main */

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

  prts[0] = -1;
  for (int i = 1; i < n; i++) {
    scanf("%d", prts + i);
    clds[prts[i]].push_back(i);
  }

  memset(cis, -1, sizeof(cis));
  bit.init(n);
  ll sum = 0;

  for (int u = 0, rn = 0; u >= 0;) {
    if (cis[u] < 0)
      avs[u] = rn - bit.sum(u);

    if (++cis[u] >= clds[u].size()) {
      sum += (rn - bit.sum(u)) - avs[u];
      bit.add(u + 1, 1);
      rn++;
      u = prts[u];
    }
    else
      u = clds[u][cis[u]];
  }

  printf("%lld\n", sum);
  return 0;
}
0