#! /usr/bin/env python3 # OBI 2023 - Fase 2 # Fortunas - Solução com DFS, Binary Lifting e BIT # Mateus Bezrutchka import sys sys.setrecursionlimit(10**6) MAXV = int(1e5 + 1) LOG = 20 def read_int(): return int(input()) def read_int_list(): return [int(x) for x in input().split()] [n, m] = read_int_list() fortuna = [0] pai = [0] grafo = [[] for i in range(n + 1)] for i in range(1, n + 1): [f, p] = read_int_list() fortuna.append(f) pai.append(p) if i > 1: grafo[p].append(i) reuniao = [] reunioes_original = [[] for i in range(n + 1)] # lista de ids pra cada anfitriao reunioes_novo = [[] for i in range(n + 1)] # lista de ids pra cada anfitriao atualizado dp = [[0 for i in range(n + 1)] for k in range(LOG)] resp = [0 for i in range(n + 1)] for i in range(m): [o, l, r] = read_int_list() reuniao.append([o, l, r]) reunioes_original[o].append(i) # binary indexed tree bit = [0 for i in range(MAXV)] def bit_update(i, val): while i < MAXV: bit[i] += val i += (i & -i) def bit_query(i): ret = 0 while i > 0: ret += bit[i] i -= (i & -i) return ret def dfs_1(v): # build binary lifting dp[0][v] = pai[v] for k in range(1, LOG): dp[k][v] = dp[k - 1][ dp[k - 1][v] ] for r_id in reunioes_original[v]: l, r = reuniao[r_id][1], reuniao[r_id][2] # vamos mover a festa do anfitriao original pro ancestral mais velho cur_anf = v for k in range(LOG - 1, -1, -1): # checa se foi convocado nxt_anf = dp[k][cur_anf] if l <= fortuna[nxt_anf] and fortuna[nxt_anf] <= r: cur_anf = nxt_anf reunioes_novo[cur_anf].append(r_id) # dfs usual for w in grafo[v]: dfs_1(w) def dfs_2(v): # atualiza festas ativas for r_id in reunioes_novo[v]: l, r = reuniao[r_id][1], reuniao[r_id][2] bit_update(l, 1) bit_update(r + 1, -1) # resp p/ v eh o numero de festas ativas para a fortuna dele resp[v] = bit_query(fortuna[v]) for w in grafo[v]: dfs_2(w) for r_id in reunioes_novo[v]: l, r = reuniao[r_id][1], reuniao[r_id][2] bit_update(l, -1) bit_update(r + 1, 1) dfs_1(1); dfs_2(1); resp = [str(x) for x in resp[1:]] print(' '.join(resp))