/**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣤⡤⠤⣤⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡴⠛⠉⠀⠀⠀⠀⠈⠙⢷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⠶⣄⡀⠀⠀⠀⢠⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⡏⠀⠀⠉⠛⠶⣤⣸⡇⠀⠀⠀⠀⣀⣤⣶⣶⠒⠒⠒⠶⣬⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣤⠴⠶⣿⠀⠀⠀⠀⠀⠀⠈⠉⠉⠛⠒⠶⢿⣭⣀⡀⢻⡀⠀⠀⢠⡿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣤⠶⠛⠋⠁⠀⠀⢸⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠛⠷⣴⣞⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⡴⠞⠉⠀⠀⠀⠀⠀⠀⢰⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⢦⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠞⠋⠀⠀⠀⠀⢀⡤⠠⡄⠀⢰⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠿⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡞⠁⠀⠀⠀⠀⣠⠖⠋⠀⣸⠇⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠀⢀⡀⠀⠀⠀⠀⠀⠀⢦⡀⠀⠀⠀⠸⣷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡼⠋⠀⠀⠀⠀⢀⣴⠋⢀⢀⡴⠋⠀⢀⣼⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣽⠀⣼⢿⡄⠀⠀⠀⠀⣆⠀⠉⢦⡀⠀⠀⠀⠘⢧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡟⠁⠀⠀⠀⠀⣠⠟⠇⠀⠈⠉⠁⠀⣀⣾⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡟⢰⠏⠀⠻⣄⠀⠀⠀⠹⣄⣰⠟⠁⠀⠀⠀⠀⠀⢻⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⡟⠀⢀⣀⡖⠀⣰⠏⠀⠀⠀⠀⠀⢀⣼⣿⠋⠸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣧⡟⠀⠀⠀⠹⣦⠀⠀⠀⠀⠁⠀⣶⠀⠀⣴⠛⢧⠀⢻⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣠⠞⣩⡟⠀⠀⠈⠉⠀⢀⡟⠀⠀⢀⣀⣠⣤⣾⡿⠗⠒⠚⣿⠠⣤⠀⠀⠀⠀⠀⠀⠀⢸⣿⠓⠒⠲⠶⠶⠾⢷⣤⣀⣀⠀⠀⠙⣧⠀⠹⣆⣼⠃⠀⢷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⢀⡴⠟⢁⡼⣿⠁⠀⠀⠀⠀⠀⢸⠃⠀⠀⠈⠉⢠⡾⠋⠀⠀⠀⠀⠸⣆⠙⣧⡀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠻⣆⠉⠁⠀⠀⢹⡄⠀⠈⠁⠀⠀⠘⣷⢦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⣠⡶⠋⢀⡴⠋⢰⡏⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⣠⠟⠀⠀⠀⠀⠀⠀⠀⢻⡄⢸⣷⣄⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠈⢳⣄⠀⠀⠸⣧⠀⠀⠀⠀⠀⠀⢿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⢰⣾⡥⠴⠞⠋⠀⠀⣼⠀⠀⠀⠀⠀⠀⠀⣸⠀⠀⠀⣰⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⡄⣷⠙⠷⣄⡀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢷⣄⠀⣿⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣿⡆⠀⠀⠀⠀⠀⠀⢸⡄⠀⣼⢏⣀⣤⣶⣦⣤⣶⣶⣶⣶⣶⣿⣿⣾⡆⠀⠈⠻⢦⣼⡇⢰⣶⣶⣶⣶⣶⣶⣶⣤⣤⣤⣦⠙⢧⣿⠀⠀⠀⠀⠀⠀⢸⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣿⡇⠀⠀⠀⠀⠀⠀⠘⣇⣼⠏⠘⣿⡿⢿⣿⣿⣿⣿⣿⡏⠉⠉⠉⠙⠃⠀⠀⠀⠀⠉⠁⠘⠛⢻⣿⣿⣿⣿⣿⣟⠛⢛⠷⠀⠀⣿⠀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⡟⣷⠀⠀⠀⠀⠀⠀⠀⢻⡏⠀⠀⠀⠀⣸⣿⣿⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣾⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⢰⡏⠀⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣷⢹⣆⠀⠀⠀⠀⠀⠀⠈⣷⠀⠀⠀⠀⢹⣿⣿⣿⣿⣿⣿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⣾⠀⠀⠀⠀⠀⠀⢰⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⢿⠘⣿⣆⠀⠀⠀⠀⠀⠐⠘⣧⠀⠀⠀⠘⢿⣿⣿⣿⣿⠏⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿⣿⡿⠃⠀⠀⠀⣼⠃⠀⠀⠀⠀⠀⣰⠟⠐⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠸⣇⣿⠙⣧⣄⠀⠀⠀⠀⠀⠘⢧⡀⠀⠀⠈⢹⠿⢟⡋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢻⡿⠀⡀⠀⠀⣼⠃⠀⠀⠀⠀⣀⡾⠋⠀⣴⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⣿⠀⣿⠝⠳⣤⣀⡀⠀⠀⠈⢷⣤⠇⢠⡞⠠⠾⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠟⠁⡾⠃⣠⡾⠃⠀⠀⣀⣤⠾⠋⠀⠀⠀⡿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣸⣿⠀⠀⠀⠉⠙⠓⡶⠦⠤⣿⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠓⢰⡿⠤⠴⠶⣿⠉⠀⠀⠀⠀⠀⢠⡇⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⢻⠀⠀⠀⠀⠀⠃⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢲⣄⠀⠀⠀⠀⠀⠀⠀⢀⣴⠆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡟⠀⠀⠀⠀⠀⠀⢸⡇⣸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⡿⢸⡇⠀⠀⠀⠀⠀⢿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠛⠲⠤⣤⠤⠴⠞⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⡏⠀⠀⠀⠀⠀⠀⢸⠇⠈⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⡇⢸⡇⠀⠀⠀⠀⠀⢸⣿⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢿⡇⠀⠀⠀⠀⠀⠀⢺⢀⠀⢿⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⢷⠛⣇⠀⠀⠀⠀⠀⠈⣿⠉⠻⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⣅⣸⠀⠀⠀⠀⠀⠀⢀⣿⢸⡀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡿⠘⣀⣿⠀⠀⠀⠀⠀⢷⢸⡄⠀⠈⠙⠶⣤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⡴⠟⠁⠀⠈⣿⠀⠀⠀⠀⠀⠀⠈⡟⣼⡇⠀⣧⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⡇⠀⢿⢿⠀⠀⠀⠀⠀⠀⠈⣧⠀⠀⠀⠀⠀⠉⠛⠶⢤⣤⣀⣀⠀⣀⡀⠀⠀⠀⠀⠀⢀⣠⡴⠞⠋⠁⠀⠀⠀⠀⢀⡏⠀⠀⠀⠀⠀⠀⢠⡇⢹⣤⠀⢹⡄⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⠀⠀⡿⢸⡆⠀⠀⠀⠀⠀⠀⢻⡆⠀⠀⠀⠀⠀⠀⠀⠀⣿⡿⠟⠋⠉⠁⠀⠀⠀⠀⠀⢸⠁⠀⠀⠀⠀⠀⠀⠀⠀⢸⠇⠀⠀⠀⠀⠀⠀⢸⡇⠈⣟⠀⠈⣷⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⢰⡟⠀⢰⡇⠘⡇⠀⠀⠀⠀⠀⠘⠀⣷⠀⠀⠀⠀⠀⠀⠀⠀⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⡿⠆⠀⠀⠀⠀⠀⠀⣼⠁⠀⢻⡀⠀⠸⣇⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣼⠁⠀⣼⠁⠀⣿⠀⠀⠀⠀⠀⠀⠀⠻⣇⣀⠀⠀⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡀⠀⠀⠀⠀⠀⠀⠀⣰⡇⠀⠀⠀⠀⠀⠀⠀⣿⡆⠀⠈⣷⡀⠀⢻⡄⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⢰⡇⠀⢀⡏⠀⠀⢻⠀⠀⠀⠀⠀⠀⠀⠀⢻⣦⣄⠀⠀⠀⠀⣠⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣧⠀⠀⠀⠀⠀⣠⣾⣿⠀⠀⠀⠀⠀⠀⢠⣠⡇⡇⠀⠀⠸⣯⡀⠀⢷⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⡾⠀⠀⣼⠁⠀⠀⠸⡇⠀⠀⠀⠀⠀⠀⠀⠀⢿⣽⡧⠴⢶⣿⣿⠖⠒⠛⠛⠃⠀⠀⠀⠚⠋⠉⠉⠉⠙⠓⠲⢾⡛⠻⣽⠃⠀⠀⠀⠀⠀⠀⢸⣿⣃⠀⠀⠀⠀⢹⣷⠀⠘⣧⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⣸⠃⠀⢸⡏⠀⠀⠀⢀⣿⠀⠀⠀⠀⢀⣀⠀⠈⠈⢷⠖⠚⠋⠁⢹⡇⠀⢀⣴⠶⠶⢦⣄⣀⣤⡶⠶⠤⠤⠤⠶⠾⠇⢰⡏⠀⠀⠀⠀⠀⠀⠻⣿⣿⣤⡀⠀⠀⠀⠀⢿⣀⠀⠸⣆⠀⠀⠀
⠀⠀⠀⠀⠀⢰⠏⠀⢀⡿⠀⠀⠀⣰⠟⢹⣿⡄⠀⠀⠀⠻⣄⠀⠀⠘⣷⣤⣄⣀⣈⡙⠛⢹⡷⢶⣦⣴⣾⣛⣻⢯⣴⣦⠴⠖⠃⠀⢀⡾⠀⠀⠀⠀⠀⠀⠀⢰⣿⡇⠀⠹⣆⠀⠀⠸⡞⣧⣆⠀⠹⣄⠀⠀
⠀⠀⠀⠀⢠⡟⠀⠀⡼⠁⠀⠀⣰⠏⠀⠈⣟⣧⠀⠀⠀⠀⢻⣆⠀⠀⠈⣧⡿⠀⠈⠉⠛⠛⣻⣿⡿⢿⣿⡍⠉⠀⠀⠀⠀⠀⠀⠀⡾⠁⠀⠀⠀⢀⣴⠀⠀⡾⣿⠁⠀⠀⠘⣧⠀⠀⢿⠘⣟⠀⠀⢻⡄⠀
⠀⠀⢀⣠⡟⠀⢀⡾⠃⠀⠀⣰⡏⠀⠀⠀⣻⠘⣧⠀⠀⠀⠀⢻⣷⡄⠀⠘⢷⡀⠀⠀⠀⠀⠩⣉⠁⠈⣛⡁⠀⠀⠀⠀⠀⠀⣀⣾⠁⠀⠀⣀⣠⣟⠁⠀⣠⣤⡟⠀⠀⠀⠀⠘⣧⡄⠘⡇⠙⣇⠀⠀⠻
*/
#include <bits/stdc++.h>
using namespace std;
#define i64 long long
#define ld long double
#define bit(n,i) ((n>>i)&1)
#define pii pair<int,int>
#define sz(x) (int)x.size()
#define FOR(i,a,b) for(int i=a; i<=b; i++)
#define FOD(i,a,b) for(int i=a; i>=b; i--)
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(),x.end()
#define __sumairu__ signed main()
#define die_hard_onimai_fan void seggs()
#define file(name) if(fopen(name".inp","r")){freopen(name".inp","r",stdin);freopen(name".out","w",stdout);}
#define brute(name) if(fopen(name".inp","r")){freopen(name".inp","r",stdin);freopen(name".ans","w",stdout);}
#define TIME (1.0*clock()/CLOCKS_PER_SEC)
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ai(n) array<int,n>
#define dbg(x) {cerr<<#x<<' '<<x<<endl;}
#define dbgArr(arr,n) {cerr<<#arr;FOR(_i,1,n)cerr<<' '<<(arr)[_i];cerr<<endl;}
template <typename T,typename U>
ostream& operator<<(ostream &os,pair<T,U>p){
return os<<"{"<<p.fi<<", "<<p.se<<"}";
}
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
i64 Rand(i64 l,i64 r)
{
i64 ans=l+1ll*rd()*rd()%(r-l+1);
assert(l<=ans&&ans<=r);
return ans;
}
//const i64 base=1e9+7;
//const i64 mod=(1ll<<53)+5;
#define i64 long long
#define debug 0
const int mod=1e9+7;
//const int mod=998244353;
const int inf=1e9;
///check the limits, dummy
const int N=1e5+5;
int a[N];
int n,m,q;
vector<int>adj[N];
int in[N],timer;
int val[N];
void dfs(int u,int p=0)
{
in[u]=++timer;
val[timer]=u;
for(int v:adj[u])if(v!=p)
{
dfs(v,u);
}
}
vector<int>ord;
int tin[N],tout[N];
int f[N*2][20],h[N],L[N*2];
void euler(int u,int p=0)
{
tin[u]=++timer;
ord.pb(u);
for(int v:adj[u])if(v!=p)
{
h[v]=h[u]+1;
euler(v,u);
++timer;
ord.pb(u);
}
tout[u]=timer;
}
void init()
{
timer=-1;
euler(1);
FOR(i,2,sz(ord))L[i]=L[i/2]+1;
FOR(i,0,sz(ord)-1)f[i][0]=ord[i];
FOR(j,1,19)
{
for(int i=0;i+(1<<j)-1<sz(ord);i++)
{
int x=f[i][j-1],y=f[i+(1<<(j-1))][j-1];
if(h[x]<h[y])f[i][j]=x;
else f[i][j]=y;
}
}
}
int lca(int u,int v)
{
if(tin[u]>tin[v])swap(u,v);
int l=tin[u],r=tin[v];
int lg=L[r-l+1];
int x=f[l][lg],y=f[r-(1<<lg)+1][lg];
if(h[x]<h[y])return x;
return y;
}
int dist(int x,int y){return h[x]+h[y]-2*h[lca(x,y)];}
const int S=320;
ai(3) Q[N];
multiset<int>s;
int res;
void add(int u)
{
// cout<<"add: "<<u<<'\n';
auto it=s.lower_bound(in[u]);
int pre=-1,nxt=-1;
if(it!=s.end())nxt=val[*it];
if(it!=s.begin())pre=val[*prev(it)];
if(pre!=-1)res+=dist(pre,u);
if(nxt!=-1)res+=dist(nxt,u);
if(pre!=-1&&nxt!=-1)res-=dist(pre,nxt);
s.insert(in[u]);
}
void erase(int u)
{
// cout<<"erase: "<<u<<'\n';
s.erase(s.find(in[u]));
auto it=s.lower_bound(in[u]);
int pre=-1,nxt=-1;
if(it!=s.end())nxt=val[*it];
if(it!=s.begin())pre=val[*prev(it)];
if(pre!=-1)res-=dist(pre,u);
if(nxt!=-1)res-=dist(nxt,u);
if(pre!=-1&&nxt!=-1)res+=dist(pre,nxt);
}
int ans[N];
die_hard_onimai_fan
{
cin>>n>>m>>q;
FOR(i,2,n)
{
int u,v;
cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1);
init();
FOR(i,1,m)cin>>a[i];
FOR(i,1,q)
{
cin>>Q[i][0]>>Q[i][1];
Q[i][2]=i;
}
sort(Q+1,Q+q+1,[&](auto x,auto y){
if(x[0]/S!=y[0]/S)return x[0]/S<y[0]/S;
return x[1]<y[1];
});
int l=1,r=0;
FOR(i,1,q)
{
while(l>Q[i][0])add(a[--l]);
while(r<Q[i][1])add(a[++r]);
while(l<Q[i][0])erase(a[l++]);
while(r>Q[i][1])erase(a[r--]);
ans[Q[i][2]]=(res+(sz(s)>1?dist(val[*s.begin()],val[*s.rbegin()]):0))/2+1;
// cout<<l<<' '<<r<<'\n';
// cout<<Q[i][0]<<' '<<Q[i][1]<<": ";
// for(int x:s)cout<<val[x]<<' ';
// cout<<'\n';
}
FOR(i,1,q)cout<<ans[i]<<'\n';
// dbgArr(in,n);
// dbgArr(val,n);
}
__sumairu__
{
FAST
file("cum");
int tt=1;//cin>>tt;
while(tt--)seggs();
cerr<<"\nTime elapsed: "<<TIME<<" s.\n";
}
/**
7 6 2
1 2
1 3
2 4
2 5
3 6
3 7
2 3 6 4 5 7
1 3
4 6
*/
Compilation message
tourism.cpp: In function 'int main()':
tourism.cpp:59:53: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | #define file(name) if(fopen(name".inp","r")){freopen(name".inp","r",stdin);freopen(name".out","w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:236:5: note: in expansion of macro 'file'
236 | file("cum");
| ^~~~
tourism.cpp:59:83: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | #define file(name) if(fopen(name".inp","r")){freopen(name".inp","r",stdin);freopen(name".out","w",stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
tourism.cpp:236:5: note: in expansion of macro 'file'
236 | file("cum");
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2688 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
3 ms |
2908 KB |
Output is correct |
5 |
Correct |
2 ms |
2924 KB |
Output is correct |
6 |
Correct |
2 ms |
2908 KB |
Output is correct |
7 |
Correct |
2 ms |
2908 KB |
Output is correct |
8 |
Correct |
2 ms |
2932 KB |
Output is correct |
9 |
Correct |
3 ms |
2908 KB |
Output is correct |
10 |
Correct |
5 ms |
2836 KB |
Output is correct |
11 |
Correct |
3 ms |
2908 KB |
Output is correct |
12 |
Correct |
1 ms |
2944 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
2 ms |
2904 KB |
Output is correct |
15 |
Correct |
4 ms |
2908 KB |
Output is correct |
16 |
Correct |
3 ms |
2908 KB |
Output is correct |
17 |
Correct |
3 ms |
2908 KB |
Output is correct |
18 |
Correct |
3 ms |
2908 KB |
Output is correct |
19 |
Correct |
4 ms |
2908 KB |
Output is correct |
20 |
Correct |
3 ms |
2908 KB |
Output is correct |
21 |
Correct |
3 ms |
2908 KB |
Output is correct |
22 |
Correct |
3 ms |
2908 KB |
Output is correct |
23 |
Correct |
3 ms |
2908 KB |
Output is correct |
24 |
Correct |
3 ms |
2908 KB |
Output is correct |
25 |
Correct |
3 ms |
2908 KB |
Output is correct |
26 |
Correct |
3 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
1 ms |
2908 KB |
Output is correct |
29 |
Correct |
1 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2688 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
3 ms |
2908 KB |
Output is correct |
5 |
Correct |
2 ms |
2924 KB |
Output is correct |
6 |
Correct |
2 ms |
2908 KB |
Output is correct |
7 |
Correct |
2 ms |
2908 KB |
Output is correct |
8 |
Correct |
2 ms |
2932 KB |
Output is correct |
9 |
Correct |
3 ms |
2908 KB |
Output is correct |
10 |
Correct |
5 ms |
2836 KB |
Output is correct |
11 |
Correct |
3 ms |
2908 KB |
Output is correct |
12 |
Correct |
1 ms |
2944 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
2 ms |
2904 KB |
Output is correct |
15 |
Correct |
4 ms |
2908 KB |
Output is correct |
16 |
Correct |
3 ms |
2908 KB |
Output is correct |
17 |
Correct |
3 ms |
2908 KB |
Output is correct |
18 |
Correct |
3 ms |
2908 KB |
Output is correct |
19 |
Correct |
4 ms |
2908 KB |
Output is correct |
20 |
Correct |
3 ms |
2908 KB |
Output is correct |
21 |
Correct |
3 ms |
2908 KB |
Output is correct |
22 |
Correct |
3 ms |
2908 KB |
Output is correct |
23 |
Correct |
3 ms |
2908 KB |
Output is correct |
24 |
Correct |
3 ms |
2908 KB |
Output is correct |
25 |
Correct |
3 ms |
2908 KB |
Output is correct |
26 |
Correct |
3 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
1 ms |
2908 KB |
Output is correct |
29 |
Correct |
1 ms |
2908 KB |
Output is correct |
30 |
Correct |
16 ms |
3160 KB |
Output is correct |
31 |
Correct |
19 ms |
3316 KB |
Output is correct |
32 |
Correct |
24 ms |
3480 KB |
Output is correct |
33 |
Correct |
25 ms |
3416 KB |
Output is correct |
34 |
Correct |
23 ms |
3416 KB |
Output is correct |
35 |
Correct |
5 ms |
3420 KB |
Output is correct |
36 |
Correct |
4 ms |
3356 KB |
Output is correct |
37 |
Correct |
4 ms |
3420 KB |
Output is correct |
38 |
Correct |
24 ms |
3536 KB |
Output is correct |
39 |
Correct |
23 ms |
3420 KB |
Output is correct |
40 |
Correct |
23 ms |
3420 KB |
Output is correct |
41 |
Correct |
4 ms |
3420 KB |
Output is correct |
42 |
Correct |
4 ms |
3420 KB |
Output is correct |
43 |
Correct |
5 ms |
3432 KB |
Output is correct |
44 |
Correct |
24 ms |
3420 KB |
Output is correct |
45 |
Correct |
23 ms |
3492 KB |
Output is correct |
46 |
Correct |
23 ms |
3420 KB |
Output is correct |
47 |
Correct |
5 ms |
3420 KB |
Output is correct |
48 |
Correct |
5 ms |
3416 KB |
Output is correct |
49 |
Correct |
4 ms |
3348 KB |
Output is correct |
50 |
Correct |
24 ms |
3496 KB |
Output is correct |
51 |
Correct |
23 ms |
3420 KB |
Output is correct |
52 |
Correct |
23 ms |
3416 KB |
Output is correct |
53 |
Correct |
23 ms |
3484 KB |
Output is correct |
54 |
Correct |
24 ms |
3420 KB |
Output is correct |
55 |
Correct |
23 ms |
3420 KB |
Output is correct |
56 |
Correct |
12 ms |
2908 KB |
Output is correct |
57 |
Correct |
2 ms |
3164 KB |
Output is correct |
58 |
Correct |
4 ms |
3164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
12 ms |
3012 KB |
Output is correct |
4 |
Execution timed out |
5062 ms |
26288 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
40 ms |
16336 KB |
Output is correct |
3 |
Correct |
56 ms |
17840 KB |
Output is correct |
4 |
Correct |
50 ms |
19396 KB |
Output is correct |
5 |
Correct |
96 ms |
31912 KB |
Output is correct |
6 |
Correct |
118 ms |
30408 KB |
Output is correct |
7 |
Correct |
110 ms |
28180 KB |
Output is correct |
8 |
Correct |
105 ms |
27468 KB |
Output is correct |
9 |
Correct |
102 ms |
27332 KB |
Output is correct |
10 |
Correct |
92 ms |
27328 KB |
Output is correct |
11 |
Correct |
80 ms |
27332 KB |
Output is correct |
12 |
Correct |
72 ms |
27528 KB |
Output is correct |
13 |
Correct |
73 ms |
27588 KB |
Output is correct |
14 |
Correct |
71 ms |
28292 KB |
Output is correct |
15 |
Correct |
79 ms |
30316 KB |
Output is correct |
16 |
Correct |
97 ms |
29552 KB |
Output is correct |
17 |
Correct |
102 ms |
29640 KB |
Output is correct |
18 |
Correct |
93 ms |
29592 KB |
Output is correct |
19 |
Correct |
90 ms |
31944 KB |
Output is correct |
20 |
Correct |
104 ms |
31876 KB |
Output is correct |
21 |
Correct |
107 ms |
29124 KB |
Output is correct |
22 |
Correct |
106 ms |
28296 KB |
Output is correct |
23 |
Correct |
109 ms |
27840 KB |
Output is correct |
24 |
Correct |
96 ms |
27304 KB |
Output is correct |
25 |
Correct |
91 ms |
27460 KB |
Output is correct |
26 |
Correct |
85 ms |
27508 KB |
Output is correct |
27 |
Correct |
80 ms |
27328 KB |
Output is correct |
28 |
Correct |
73 ms |
27332 KB |
Output is correct |
29 |
Correct |
73 ms |
27336 KB |
Output is correct |
30 |
Correct |
68 ms |
27588 KB |
Output is correct |
31 |
Correct |
70 ms |
27704 KB |
Output is correct |
32 |
Correct |
65 ms |
28108 KB |
Output is correct |
33 |
Correct |
72 ms |
28876 KB |
Output is correct |
34 |
Correct |
74 ms |
30400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
3 ms |
2652 KB |
Output is correct |
3 |
Correct |
16 ms |
2960 KB |
Output is correct |
4 |
Execution timed out |
5031 ms |
23508 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2688 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
3 ms |
2908 KB |
Output is correct |
5 |
Correct |
2 ms |
2924 KB |
Output is correct |
6 |
Correct |
2 ms |
2908 KB |
Output is correct |
7 |
Correct |
2 ms |
2908 KB |
Output is correct |
8 |
Correct |
2 ms |
2932 KB |
Output is correct |
9 |
Correct |
3 ms |
2908 KB |
Output is correct |
10 |
Correct |
5 ms |
2836 KB |
Output is correct |
11 |
Correct |
3 ms |
2908 KB |
Output is correct |
12 |
Correct |
1 ms |
2944 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
2 ms |
2904 KB |
Output is correct |
15 |
Correct |
4 ms |
2908 KB |
Output is correct |
16 |
Correct |
3 ms |
2908 KB |
Output is correct |
17 |
Correct |
3 ms |
2908 KB |
Output is correct |
18 |
Correct |
3 ms |
2908 KB |
Output is correct |
19 |
Correct |
4 ms |
2908 KB |
Output is correct |
20 |
Correct |
3 ms |
2908 KB |
Output is correct |
21 |
Correct |
3 ms |
2908 KB |
Output is correct |
22 |
Correct |
3 ms |
2908 KB |
Output is correct |
23 |
Correct |
3 ms |
2908 KB |
Output is correct |
24 |
Correct |
3 ms |
2908 KB |
Output is correct |
25 |
Correct |
3 ms |
2908 KB |
Output is correct |
26 |
Correct |
3 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
1 ms |
2908 KB |
Output is correct |
29 |
Correct |
1 ms |
2908 KB |
Output is correct |
30 |
Correct |
16 ms |
3160 KB |
Output is correct |
31 |
Correct |
19 ms |
3316 KB |
Output is correct |
32 |
Correct |
24 ms |
3480 KB |
Output is correct |
33 |
Correct |
25 ms |
3416 KB |
Output is correct |
34 |
Correct |
23 ms |
3416 KB |
Output is correct |
35 |
Correct |
5 ms |
3420 KB |
Output is correct |
36 |
Correct |
4 ms |
3356 KB |
Output is correct |
37 |
Correct |
4 ms |
3420 KB |
Output is correct |
38 |
Correct |
24 ms |
3536 KB |
Output is correct |
39 |
Correct |
23 ms |
3420 KB |
Output is correct |
40 |
Correct |
23 ms |
3420 KB |
Output is correct |
41 |
Correct |
4 ms |
3420 KB |
Output is correct |
42 |
Correct |
4 ms |
3420 KB |
Output is correct |
43 |
Correct |
5 ms |
3432 KB |
Output is correct |
44 |
Correct |
24 ms |
3420 KB |
Output is correct |
45 |
Correct |
23 ms |
3492 KB |
Output is correct |
46 |
Correct |
23 ms |
3420 KB |
Output is correct |
47 |
Correct |
5 ms |
3420 KB |
Output is correct |
48 |
Correct |
5 ms |
3416 KB |
Output is correct |
49 |
Correct |
4 ms |
3348 KB |
Output is correct |
50 |
Correct |
24 ms |
3496 KB |
Output is correct |
51 |
Correct |
23 ms |
3420 KB |
Output is correct |
52 |
Correct |
23 ms |
3416 KB |
Output is correct |
53 |
Correct |
23 ms |
3484 KB |
Output is correct |
54 |
Correct |
24 ms |
3420 KB |
Output is correct |
55 |
Correct |
23 ms |
3420 KB |
Output is correct |
56 |
Correct |
12 ms |
2908 KB |
Output is correct |
57 |
Correct |
2 ms |
3164 KB |
Output is correct |
58 |
Correct |
4 ms |
3164 KB |
Output is correct |
59 |
Correct |
1 ms |
2652 KB |
Output is correct |
60 |
Correct |
2 ms |
2652 KB |
Output is correct |
61 |
Correct |
12 ms |
3012 KB |
Output is correct |
62 |
Execution timed out |
5062 ms |
26288 KB |
Time limit exceeded |
63 |
Halted |
0 ms |
0 KB |
- |