Submission #68179086


Source Code Expand

#include <bits/stdc++.h>
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define REP1(i, a, b) for (int i = (a); i <= int(b); i++)

#if __has_include("shik/dump.h")
#include "shik/dump.h"  // IWYU pragma: keep
#else
#define dump(...) 0
#endif

using namespace std;

using i64 = int64_t;

const int Q = 5e5 + 10;
const int L = 19;
const i64 INF = 1e18;

struct Jump {
  int to;
  i64 off;
};

i64 len[Q];
int lc[Q], rc[Q];
Jump jump[Q][L];

void build(int id, int l, int r) {
  len[id] = min(INF, len[l] + len[r]);
  lc[id] = l;
  rc[id] = r;

  jump[id][0] = (len[l] >= len[r]) ? Jump{l, 0} : Jump{r, len[l]};
  for (int i = 1; i < L; i++) {
    auto j1 = jump[id][i - 1];
    auto j2 = jump[j1.to][i - 1];
    jump[id][i] = {j2.to, min(INF, j1.off + j2.off)};
  }
}

int solve(int id, int l, int r, i64 x) {
  while (id >= 2) {
    for (int i = L - 1; i >= 0 && id >= 2; i--) {
      auto& j = jump[id][i];
      if (x > j.off && x <= j.off + len[j.to]) {
        x -= j.off;
        id = j.to;
      }
    }
    if (id < 2) return id;
    if (x > len[lc[id]]) {
      x -= len[lc[id]];
      id = rc[id];
    } else {
      id = lc[id];
    }
  }
  return id;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  len[0] = len[1] = 1;
  for (int i = 0; i < L; i++) {
    jump[0][i] = {0, 0};
    jump[1][i] = {1, 0};
  }

  int q;
  cin >> q;
  for (int i = 2; i < q + 2; i++) {
    int l, r;
    i64 x;
    cin >> l >> r >> x;
    build(i, l, r);
    cout << solve(i, l, r, x) << '\n';
  }
}

Submission Info

Submission Time
Task G - Binary Cat
User shik
Language C++ 23 (gcc 12.2)
Score 625
Code Size 1660 Byte
Status AC
Exec Time 1025 ms
Memory 159892 KiB

Compile Error

Main.cpp: In function ‘int solve(int, int, int, i64)’:
Main.cpp:43:23: warning: unused parameter ‘l’ [-Wunused-parameter]
   43 | int solve(int id, int l, int r, i64 x) {
      |                   ~~~~^
Main.cpp:43:30: warning: unused parameter ‘r’ [-Wunused-parameter]
   43 | int solve(int id, int l, int r, i64 x) {
      |                          ~~~~^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 625 / 625
Status
AC × 1
AC × 56
Set Name Test Cases
Sample example_00.txt
All example_00.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, random2_00.txt, random2_01.txt, random2_02.txt, random2_03.txt, random2_04.txt, random2_05.txt, random2_06.txt, random2_07.txt, random2_08.txt, random2_09.txt, random2_10.txt, random2_11.txt, random2_12.txt, random2_13.txt, random2_14.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 3520 KiB
hand_00.txt AC 589 ms 159704 KiB
hand_01.txt AC 889 ms 159740 KiB
hand_02.txt AC 588 ms 159772 KiB
hand_03.txt AC 893 ms 159772 KiB
hand_04.txt AC 1025 ms 159684 KiB
hand_05.txt AC 854 ms 159672 KiB
hand_06.txt AC 364 ms 159752 KiB
hand_07.txt AC 858 ms 159892 KiB
hand_08.txt AC 164 ms 159740 KiB
hand_09.txt AC 1 ms 3644 KiB
random2_00.txt AC 340 ms 148984 KiB
random2_01.txt AC 771 ms 147516 KiB
random2_02.txt AC 876 ms 158960 KiB
random2_03.txt AC 311 ms 152420 KiB
random2_04.txt AC 730 ms 152008 KiB
random2_05.txt AC 725 ms 150872 KiB
random2_06.txt AC 330 ms 150712 KiB
random2_07.txt AC 850 ms 158448 KiB
random2_08.txt AC 811 ms 149064 KiB
random2_09.txt AC 350 ms 149164 KiB
random2_10.txt AC 866 ms 153656 KiB
random2_11.txt AC 832 ms 150452 KiB
random2_12.txt AC 312 ms 151184 KiB
random2_13.txt AC 844 ms 150700 KiB
random2_14.txt AC 886 ms 153604 KiB
random_00.txt AC 348 ms 159788 KiB
random_01.txt AC 761 ms 159760 KiB
random_02.txt AC 560 ms 159736 KiB
random_03.txt AC 570 ms 159628 KiB
random_04.txt AC 804 ms 159744 KiB
random_05.txt AC 365 ms 159844 KiB
random_06.txt AC 546 ms 159656 KiB
random_07.txt AC 775 ms 159632 KiB
random_08.txt AC 573 ms 159744 KiB
random_09.txt AC 579 ms 159700 KiB
random_10.txt AC 416 ms 159656 KiB
random_11.txt AC 556 ms 159756 KiB
random_12.txt AC 572 ms 159736 KiB
random_13.txt AC 826 ms 159884 KiB
random_14.txt AC 578 ms 159596 KiB
random_15.txt AC 359 ms 159640 KiB
random_16.txt AC 794 ms 159620 KiB
random_17.txt AC 567 ms 159636 KiB
random_18.txt AC 587 ms 159728 KiB
random_19.txt AC 784 ms 159724 KiB
random_20.txt AC 366 ms 159708 KiB
random_21.txt AC 554 ms 159804 KiB
random_22.txt AC 773 ms 159732 KiB
random_23.txt AC 566 ms 159732 KiB
random_24.txt AC 570 ms 159668 KiB
random_25.txt AC 437 ms 159764 KiB
random_26.txt AC 556 ms 159796 KiB
random_27.txt AC 579 ms 159728 KiB
random_28.txt AC 786 ms 159736 KiB
random_29.txt AC 570 ms 159756 KiB