Submission #1114344

# Submission time Handle Problem Language Result Execution time Memory
1114344 2024-11-18T15:59:12 Z hahahaha Tourism (JOI23_tourism) C++17
100 / 100
2517 ms 38076 KB
/**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣤⡤⠤⣤⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡴⠛⠉⠀⠀⠀⠀⠈⠙⢷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⠶⣄⡀⠀⠀⠀⢠⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⡏⠀⠀⠉⠛⠶⣤⣸⡇⠀⠀⠀⠀⣀⣤⣶⣶⠒⠒⠒⠶⣬⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣤⠴⠶⣿⠀⠀⠀⠀⠀⠀⠈⠉⠉⠛⠒⠶⢿⣭⣀⡀⢻⡀⠀⠀⢠⡿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣤⠶⠛⠋⠁⠀⠀⢸⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠛⠷⣴⣞⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⡴⠞⠉⠀⠀⠀⠀⠀⠀⢰⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⢦⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠞⠋⠀⠀⠀⠀⢀⡤⠠⡄⠀⢰⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠿⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡞⠁⠀⠀⠀⠀⣠⠖⠋⠀⣸⠇⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠀⢀⡀⠀⠀⠀⠀⠀⠀⢦⡀⠀⠀⠀⠸⣷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡼⠋⠀⠀⠀⠀⢀⣴⠋⢀⢀⡴⠋⠀⢀⣼⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣽⠀⣼⢿⡄⠀⠀⠀⠀⣆⠀⠉⢦⡀⠀⠀⠀⠘⢧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡟⠁⠀⠀⠀⠀⣠⠟⠇⠀⠈⠉⠁⠀⣀⣾⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡟⢰⠏⠀⠻⣄⠀⠀⠀⠹⣄⣰⠟⠁⠀⠀⠀⠀⠀⢻⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⡟⠀⢀⣀⡖⠀⣰⠏⠀⠀⠀⠀⠀⢀⣼⣿⠋⠸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣧⡟⠀⠀⠀⠹⣦⠀⠀⠀⠀⠁⠀⣶⠀⠀⣴⠛⢧⠀⢻⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣠⠞⣩⡟⠀⠀⠈⠉⠀⢀⡟⠀⠀⢀⣀⣠⣤⣾⡿⠗⠒⠚⣿⠠⣤⠀⠀⠀⠀⠀⠀⠀⢸⣿⠓⠒⠲⠶⠶⠾⢷⣤⣀⣀⠀⠀⠙⣧⠀⠹⣆⣼⠃⠀⢷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⢀⡴⠟⢁⡼⣿⠁⠀⠀⠀⠀⠀⢸⠃⠀⠀⠈⠉⢠⡾⠋⠀⠀⠀⠀⠸⣆⠙⣧⡀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠻⣆⠉⠁⠀⠀⢹⡄⠀⠈⠁⠀⠀⠘⣷⢦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⣠⡶⠋⢀⡴⠋⢰⡏⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⣠⠟⠀⠀⠀⠀⠀⠀⠀⢻⡄⢸⣷⣄⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠈⢳⣄⠀⠀⠸⣧⠀⠀⠀⠀⠀⠀⢿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⢰⣾⡥⠴⠞⠋⠀⠀⣼⠀⠀⠀⠀⠀⠀⠀⣸⠀⠀⠀⣰⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⡄⣷⠙⠷⣄⡀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢷⣄⠀⣿⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣿⡆⠀⠀⠀⠀⠀⠀⢸⡄⠀⣼⢏⣀⣤⣶⣦⣤⣶⣶⣶⣶⣶⣿⣿⣾⡆⠀⠈⠻⢦⣼⡇⢰⣶⣶⣶⣶⣶⣶⣶⣤⣤⣤⣦⠙⢧⣿⠀⠀⠀⠀⠀⠀⢸⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣿⡇⠀⠀⠀⠀⠀⠀⠘⣇⣼⠏⠘⣿⡿⢿⣿⣿⣿⣿⣿⡏⠉⠉⠉⠙⠃⠀⠀⠀⠀⠉⠁⠘⠛⢻⣿⣿⣿⣿⣿⣟⠛⢛⠷⠀⠀⣿⠀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⡟⣷⠀⠀⠀⠀⠀⠀⠀⢻⡏⠀⠀⠀⠀⣸⣿⣿⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣾⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⢰⡏⠀⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣷⢹⣆⠀⠀⠀⠀⠀⠀⠈⣷⠀⠀⠀⠀⢹⣿⣿⣿⣿⣿⣿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⣾⠀⠀⠀⠀⠀⠀⢰⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⢿⠘⣿⣆⠀⠀⠀⠀⠀⠐⠘⣧⠀⠀⠀⠘⢿⣿⣿⣿⣿⠏⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿⣿⡿⠃⠀⠀⠀⣼⠃⠀⠀⠀⠀⠀⣰⠟⠐⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠸⣇⣿⠙⣧⣄⠀⠀⠀⠀⠀⠘⢧⡀⠀⠀⠈⢹⠿⢟⡋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢻⡿⠀⡀⠀⠀⣼⠃⠀⠀⠀⠀⣀⡾⠋⠀⣴⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⣿⠀⣿⠝⠳⣤⣀⡀⠀⠀⠈⢷⣤⠇⢠⡞⠠⠾⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠟⠁⡾⠃⣠⡾⠃⠀⠀⣀⣤⠾⠋⠀⠀⠀⡿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣸⣿⠀⠀⠀⠉⠙⠓⡶⠦⠤⣿⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠓⢰⡿⠤⠴⠶⣿⠉⠀⠀⠀⠀⠀⢠⡇⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⢻⠀⠀⠀⠀⠀⠃⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢲⣄⠀⠀⠀⠀⠀⠀⠀⢀⣴⠆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡟⠀⠀⠀⠀⠀⠀⢸⡇⣸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⡿⢸⡇⠀⠀⠀⠀⠀⢿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠛⠲⠤⣤⠤⠴⠞⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⡏⠀⠀⠀⠀⠀⠀⢸⠇⠈⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⡇⢸⡇⠀⠀⠀⠀⠀⢸⣿⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢿⡇⠀⠀⠀⠀⠀⠀⢺⢀⠀⢿⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⢷⠛⣇⠀⠀⠀⠀⠀⠈⣿⠉⠻⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⣅⣸⠀⠀⠀⠀⠀⠀⢀⣿⢸⡀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡿⠘⣀⣿⠀⠀⠀⠀⠀⢷⢸⡄⠀⠈⠙⠶⣤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⡴⠟⠁⠀⠈⣿⠀⠀⠀⠀⠀⠀⠈⡟⣼⡇⠀⣧⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⡇⠀⢿⢿⠀⠀⠀⠀⠀⠀⠈⣧⠀⠀⠀⠀⠀⠉⠛⠶⢤⣤⣀⣀⠀⣀⡀⠀⠀⠀⠀⠀⢀⣠⡴⠞⠋⠁⠀⠀⠀⠀⢀⡏⠀⠀⠀⠀⠀⠀⢠⡇⢹⣤⠀⢹⡄⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⠀⠀⡿⢸⡆⠀⠀⠀⠀⠀⠀⢻⡆⠀⠀⠀⠀⠀⠀⠀⠀⣿⡿⠟⠋⠉⠁⠀⠀⠀⠀⠀⢸⠁⠀⠀⠀⠀⠀⠀⠀⠀⢸⠇⠀⠀⠀⠀⠀⠀⢸⡇⠈⣟⠀⠈⣷⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⢰⡟⠀⢰⡇⠘⡇⠀⠀⠀⠀⠀⠘⠀⣷⠀⠀⠀⠀⠀⠀⠀⠀⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⡿⠆⠀⠀⠀⠀⠀⠀⣼⠁⠀⢻⡀⠀⠸⣇⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣼⠁⠀⣼⠁⠀⣿⠀⠀⠀⠀⠀⠀⠀⠻⣇⣀⠀⠀⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡀⠀⠀⠀⠀⠀⠀⠀⣰⡇⠀⠀⠀⠀⠀⠀⠀⣿⡆⠀⠈⣷⡀⠀⢻⡄⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⢰⡇⠀⢀⡏⠀⠀⢻⠀⠀⠀⠀⠀⠀⠀⠀⢻⣦⣄⠀⠀⠀⠀⣠⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣧⠀⠀⠀⠀⠀⣠⣾⣿⠀⠀⠀⠀⠀⠀⢠⣠⡇⡇⠀⠀⠸⣯⡀⠀⢷⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⡾⠀⠀⣼⠁⠀⠀⠸⡇⠀⠀⠀⠀⠀⠀⠀⠀⢿⣽⡧⠴⢶⣿⣿⠖⠒⠛⠛⠃⠀⠀⠀⠚⠋⠉⠉⠉⠙⠓⠲⢾⡛⠻⣽⠃⠀⠀⠀⠀⠀⠀⢸⣿⣃⠀⠀⠀⠀⢹⣷⠀⠘⣧⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⣸⠃⠀⢸⡏⠀⠀⠀⢀⣿⠀⠀⠀⠀⢀⣀⠀⠈⠈⢷⠖⠚⠋⠁⢹⡇⠀⢀⣴⠶⠶⢦⣄⣀⣤⡶⠶⠤⠤⠤⠶⠾⠇⢰⡏⠀⠀⠀⠀⠀⠀⠻⣿⣿⣤⡀⠀⠀⠀⠀⢿⣀⠀⠸⣆⠀⠀⠀
⠀⠀⠀⠀⠀⢰⠏⠀⢀⡿⠀⠀⠀⣰⠟⢹⣿⡄⠀⠀⠀⠻⣄⠀⠀⠘⣷⣤⣄⣀⣈⡙⠛⢹⡷⢶⣦⣴⣾⣛⣻⢯⣴⣦⠴⠖⠃⠀⢀⡾⠀⠀⠀⠀⠀⠀⠀⢰⣿⡇⠀⠹⣆⠀⠀⠸⡞⣧⣆⠀⠹⣄⠀⠀
⠀⠀⠀⠀⢠⡟⠀⠀⡼⠁⠀⠀⣰⠏⠀⠈⣟⣧⠀⠀⠀⠀⢻⣆⠀⠀⠈⣧⡿⠀⠈⠉⠛⠛⣻⣿⡿⢿⣿⡍⠉⠀⠀⠀⠀⠀⠀⠀⡾⠁⠀⠀⠀⢀⣴⠀⠀⡾⣿⠁⠀⠀⠘⣧⠀⠀⢿⠘⣟⠀⠀⢻⡄⠀
⠀⠀⢀⣠⡟⠀⢀⡾⠃⠀⠀⣰⡏⠀⠀⠀⣻⠘⣧⠀⠀⠀⠀⢻⣷⡄⠀⠘⢷⡀⠀⠀⠀⠀⠩⣉⠁⠈⣛⡁⠀⠀⠀⠀⠀⠀⣀⣾⠁⠀⠀⣀⣠⣟⠁⠀⣠⣤⡟⠀⠀⠀⠀⠘⣧⡄⠘⡇⠙⣇⠀⠀⠻
*/
#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;

template <const int D = 6> struct FastSet {
    int n;
    using u64 = uint64_t;
    vector<u64> a[D];
    FastSet(int n_ = (1 << std::min(31, D * 6))) : n(n_) {
        for(int i = 0; i < D; i++) {
            n_ = (n_ + 63) >> 6;
            a[i].assign(n_, 0);
        }
    }
    bool empty() const { return !a[D - 1][0]; }
    bool contains(int x) const { return (a[0][x >> 6] >> (x & 63)) & 1; }
    void insert(int x) {
        for(int i = 0; i < D; i++) {
            const int y = x & 63;
            x >>= 6;
            a[i][x] |= 1ULL << y;
        }
    }
    void erase(int x) {
        for(int i = 0; i < D; i++) {
            const int y = x & 63;
            x >>= 6;
            if((a[i][x] &= ~(1ULL << y))) break;
        }
    }
    int next(int x) const {
        for(int i = 0; i < D; i++) {
            const int k = x >> 6, y = x & 63;
            if(k >= sz(a[i])) return n;
            const u64 top = a[i][k] >> y;
            if(top) {
                x += __builtin_ctzll(top);
                for(int j = i - 1; j >= 0; --j) x = x << 6 | __builtin_ctzll(a[j][x]);
                return x;
            }
            x = k + 1;
        }
        return n;
    }
    int prev(int x) const {
        for(int i = 0; i < D; ++i) {
            if(x < 0) return -1;
            const int k = x >> 6, y = x & 63;
            const u64 bot = a[i][k] << (63 - y);
            if(bot) {
                x -= __builtin_clzll(bot);
                for(int j = i - 1; j >= 0; --j) x = x << 6 | (63 - __builtin_clzll(a[j][x]));
                return x;
            }
            x = k - 1;
        }
        return -1;
    }
    int max() const { return prev(n); }
    int min() const { return next(0); }
};

int n,m,q;
vector<int>adj[N];
int in[N],timer;
int val[N];
ai(3) a[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=370;
ai(3) Q[N];

int ans[N];
int idx[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][1];
        a[i][0]=in[a[i][1]];
        a[i][2]=i;
    }
    sort(a+1,a+m+1);

    FOR(i,1,m)idx[a[i][2]]=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];
    });

    FastSet<4> s(m+1);
    int res=0;

    auto add=[&](int p){
        int u=idx[p];

        int pre=s.prev(u),nxt=s.next(u);
        if(nxt==m+1)nxt=-1;

        if(pre!=-1)res+=dist(a[pre][1],a[u][1]);
        if(nxt!=-1)res+=dist(a[nxt][1],a[u][1]);
        if(pre!=-1&&nxt!=-1)res-=dist(a[pre][1],a[nxt][1]);

        s.insert(u);
    };
    auto erase=[&](int p){
        int u=idx[p];
        s.erase(u);

        int pre=s.prev(u),nxt=s.next(u);
        if(nxt==m+1)nxt=-1;

        if(pre!=-1)res-=dist(a[pre][1],a[u][1]);
        if(nxt!=-1)res-=dist(a[nxt][1],a[u][1]);
        if(pre!=-1&&nxt!=-1)res+=dist(a[pre][1],a[nxt][1]);
    };

    int l=1,r=0;
    FOR(i,1,q)
    {
        while(l>Q[i][0])add(--l);
        while(r<Q[i][1])add(++r);
        while(l<Q[i][0])erase(l++);
        while(r>Q[i][1])erase(r--);
        ans[Q[i][2]]=(res+dist(a[s.next(0)][1],a[s.prev(m+1)][1]))/2+1;
    }

    FOR(i,1,q)cout<<ans[i]<<'\n';
}

__sumairu__
{
    FAST
    file("");

    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:290:5: note: in expansion of macro 'file'
  290 |     file("");
      |     ^~~~
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:290:5: note: in expansion of macro 'file'
  290 |     file("");
      |     ^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5712 KB Output is correct
2 Correct 2 ms 5712 KB Output is correct
3 Correct 2 ms 5712 KB Output is correct
4 Correct 3 ms 5712 KB Output is correct
5 Correct 3 ms 5712 KB Output is correct
6 Correct 2 ms 5712 KB Output is correct
7 Correct 2 ms 5712 KB Output is correct
8 Correct 3 ms 5712 KB Output is correct
9 Correct 3 ms 5712 KB Output is correct
10 Correct 3 ms 5708 KB Output is correct
11 Correct 3 ms 5712 KB Output is correct
12 Correct 2 ms 5712 KB Output is correct
13 Correct 2 ms 5720 KB Output is correct
14 Correct 3 ms 5720 KB Output is correct
15 Correct 3 ms 5880 KB Output is correct
16 Correct 3 ms 5824 KB Output is correct
17 Correct 3 ms 5720 KB Output is correct
18 Correct 3 ms 5720 KB Output is correct
19 Correct 3 ms 5720 KB Output is correct
20 Correct 3 ms 5720 KB Output is correct
21 Correct 3 ms 5828 KB Output is correct
22 Correct 3 ms 5720 KB Output is correct
23 Correct 3 ms 5720 KB Output is correct
24 Correct 3 ms 5720 KB Output is correct
25 Correct 3 ms 5720 KB Output is correct
26 Correct 3 ms 5880 KB Output is correct
27 Correct 3 ms 9968 KB Output is correct
28 Correct 3 ms 5720 KB Output is correct
29 Correct 3 ms 5720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5712 KB Output is correct
2 Correct 2 ms 5712 KB Output is correct
3 Correct 2 ms 5712 KB Output is correct
4 Correct 3 ms 5712 KB Output is correct
5 Correct 3 ms 5712 KB Output is correct
6 Correct 2 ms 5712 KB Output is correct
7 Correct 2 ms 5712 KB Output is correct
8 Correct 3 ms 5712 KB Output is correct
9 Correct 3 ms 5712 KB Output is correct
10 Correct 3 ms 5708 KB Output is correct
11 Correct 3 ms 5712 KB Output is correct
12 Correct 2 ms 5712 KB Output is correct
13 Correct 2 ms 5720 KB Output is correct
14 Correct 3 ms 5720 KB Output is correct
15 Correct 3 ms 5880 KB Output is correct
16 Correct 3 ms 5824 KB Output is correct
17 Correct 3 ms 5720 KB Output is correct
18 Correct 3 ms 5720 KB Output is correct
19 Correct 3 ms 5720 KB Output is correct
20 Correct 3 ms 5720 KB Output is correct
21 Correct 3 ms 5828 KB Output is correct
22 Correct 3 ms 5720 KB Output is correct
23 Correct 3 ms 5720 KB Output is correct
24 Correct 3 ms 5720 KB Output is correct
25 Correct 3 ms 5720 KB Output is correct
26 Correct 3 ms 5880 KB Output is correct
27 Correct 3 ms 9968 KB Output is correct
28 Correct 3 ms 5720 KB Output is correct
29 Correct 3 ms 5720 KB Output is correct
30 Correct 7 ms 5976 KB Output is correct
31 Correct 12 ms 6300 KB Output is correct
32 Correct 10 ms 6236 KB Output is correct
33 Correct 10 ms 6492 KB Output is correct
34 Correct 10 ms 6492 KB Output is correct
35 Correct 5 ms 6236 KB Output is correct
36 Correct 5 ms 6236 KB Output is correct
37 Correct 4 ms 6236 KB Output is correct
38 Correct 11 ms 6348 KB Output is correct
39 Correct 10 ms 6236 KB Output is correct
40 Correct 10 ms 6344 KB Output is correct
41 Correct 5 ms 6224 KB Output is correct
42 Correct 5 ms 6224 KB Output is correct
43 Correct 4 ms 6224 KB Output is correct
44 Correct 10 ms 6224 KB Output is correct
45 Correct 10 ms 6224 KB Output is correct
46 Correct 10 ms 6224 KB Output is correct
47 Correct 4 ms 6228 KB Output is correct
48 Correct 4 ms 6392 KB Output is correct
49 Correct 4 ms 6224 KB Output is correct
50 Correct 10 ms 6252 KB Output is correct
51 Correct 12 ms 6224 KB Output is correct
52 Correct 10 ms 6252 KB Output is correct
53 Correct 10 ms 6224 KB Output is correct
54 Correct 12 ms 6224 KB Output is correct
55 Correct 10 ms 6224 KB Output is correct
56 Correct 6 ms 9808 KB Output is correct
57 Correct 3 ms 6224 KB Output is correct
58 Correct 4 ms 6224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5712 KB Output is correct
2 Correct 3 ms 9808 KB Output is correct
3 Correct 6 ms 9808 KB Output is correct
4 Correct 1485 ms 25288 KB Output is correct
5 Correct 816 ms 31940 KB Output is correct
6 Correct 1271 ms 35372 KB Output is correct
7 Correct 1967 ms 38028 KB Output is correct
8 Correct 2050 ms 38076 KB Output is correct
9 Correct 2024 ms 38040 KB Output is correct
10 Correct 1976 ms 38024 KB Output is correct
11 Correct 1940 ms 38028 KB Output is correct
12 Correct 266 ms 37812 KB Output is correct
13 Correct 263 ms 37820 KB Output is correct
14 Correct 281 ms 37820 KB Output is correct
15 Correct 59 ms 35772 KB Output is correct
16 Correct 108 ms 37668 KB Output is correct
17 Correct 692 ms 11344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5712 KB Output is correct
2 Correct 40 ms 19056 KB Output is correct
3 Correct 49 ms 19648 KB Output is correct
4 Correct 50 ms 22204 KB Output is correct
5 Correct 65 ms 28452 KB Output is correct
6 Correct 72 ms 28632 KB Output is correct
7 Correct 80 ms 28532 KB Output is correct
8 Correct 76 ms 28592 KB Output is correct
9 Correct 70 ms 28604 KB Output is correct
10 Correct 69 ms 28604 KB Output is correct
11 Correct 67 ms 28604 KB Output is correct
12 Correct 77 ms 28500 KB Output is correct
13 Correct 72 ms 28860 KB Output is correct
14 Correct 88 ms 29404 KB Output is correct
15 Correct 105 ms 31336 KB Output is correct
16 Correct 80 ms 28860 KB Output is correct
17 Correct 72 ms 28904 KB Output is correct
18 Correct 82 ms 28728 KB Output is correct
19 Correct 73 ms 28592 KB Output is correct
20 Correct 66 ms 28604 KB Output is correct
21 Correct 74 ms 28604 KB Output is correct
22 Correct 70 ms 28604 KB Output is correct
23 Correct 79 ms 28604 KB Output is correct
24 Correct 74 ms 28604 KB Output is correct
25 Correct 81 ms 28444 KB Output is correct
26 Correct 76 ms 28612 KB Output is correct
27 Correct 75 ms 28604 KB Output is correct
28 Correct 71 ms 28616 KB Output is correct
29 Correct 76 ms 28556 KB Output is correct
30 Correct 99 ms 28744 KB Output is correct
31 Correct 103 ms 28860 KB Output is correct
32 Correct 77 ms 29116 KB Output is correct
33 Correct 84 ms 30156 KB Output is correct
34 Correct 100 ms 31424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5712 KB Output is correct
2 Correct 3 ms 9808 KB Output is correct
3 Correct 6 ms 9808 KB Output is correct
4 Correct 1709 ms 22472 KB Output is correct
5 Correct 1834 ms 22632 KB Output is correct
6 Correct 2001 ms 28660 KB Output is correct
7 Correct 2328 ms 31760 KB Output is correct
8 Correct 2357 ms 31760 KB Output is correct
9 Correct 2332 ms 31760 KB Output is correct
10 Correct 2133 ms 31760 KB Output is correct
11 Correct 2222 ms 31756 KB Output is correct
12 Correct 2244 ms 31680 KB Output is correct
13 Correct 2283 ms 31760 KB Output is correct
14 Correct 657 ms 11324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5712 KB Output is correct
2 Correct 2 ms 5712 KB Output is correct
3 Correct 2 ms 5712 KB Output is correct
4 Correct 3 ms 5712 KB Output is correct
5 Correct 3 ms 5712 KB Output is correct
6 Correct 2 ms 5712 KB Output is correct
7 Correct 2 ms 5712 KB Output is correct
8 Correct 3 ms 5712 KB Output is correct
9 Correct 3 ms 5712 KB Output is correct
10 Correct 3 ms 5708 KB Output is correct
11 Correct 3 ms 5712 KB Output is correct
12 Correct 2 ms 5712 KB Output is correct
13 Correct 2 ms 5720 KB Output is correct
14 Correct 3 ms 5720 KB Output is correct
15 Correct 3 ms 5880 KB Output is correct
16 Correct 3 ms 5824 KB Output is correct
17 Correct 3 ms 5720 KB Output is correct
18 Correct 3 ms 5720 KB Output is correct
19 Correct 3 ms 5720 KB Output is correct
20 Correct 3 ms 5720 KB Output is correct
21 Correct 3 ms 5828 KB Output is correct
22 Correct 3 ms 5720 KB Output is correct
23 Correct 3 ms 5720 KB Output is correct
24 Correct 3 ms 5720 KB Output is correct
25 Correct 3 ms 5720 KB Output is correct
26 Correct 3 ms 5880 KB Output is correct
27 Correct 3 ms 9968 KB Output is correct
28 Correct 3 ms 5720 KB Output is correct
29 Correct 3 ms 5720 KB Output is correct
30 Correct 7 ms 5976 KB Output is correct
31 Correct 12 ms 6300 KB Output is correct
32 Correct 10 ms 6236 KB Output is correct
33 Correct 10 ms 6492 KB Output is correct
34 Correct 10 ms 6492 KB Output is correct
35 Correct 5 ms 6236 KB Output is correct
36 Correct 5 ms 6236 KB Output is correct
37 Correct 4 ms 6236 KB Output is correct
38 Correct 11 ms 6348 KB Output is correct
39 Correct 10 ms 6236 KB Output is correct
40 Correct 10 ms 6344 KB Output is correct
41 Correct 5 ms 6224 KB Output is correct
42 Correct 5 ms 6224 KB Output is correct
43 Correct 4 ms 6224 KB Output is correct
44 Correct 10 ms 6224 KB Output is correct
45 Correct 10 ms 6224 KB Output is correct
46 Correct 10 ms 6224 KB Output is correct
47 Correct 4 ms 6228 KB Output is correct
48 Correct 4 ms 6392 KB Output is correct
49 Correct 4 ms 6224 KB Output is correct
50 Correct 10 ms 6252 KB Output is correct
51 Correct 12 ms 6224 KB Output is correct
52 Correct 10 ms 6252 KB Output is correct
53 Correct 10 ms 6224 KB Output is correct
54 Correct 12 ms 6224 KB Output is correct
55 Correct 10 ms 6224 KB Output is correct
56 Correct 6 ms 9808 KB Output is correct
57 Correct 3 ms 6224 KB Output is correct
58 Correct 4 ms 6224 KB Output is correct
59 Correct 2 ms 5712 KB Output is correct
60 Correct 3 ms 9808 KB Output is correct
61 Correct 6 ms 9808 KB Output is correct
62 Correct 1485 ms 25288 KB Output is correct
63 Correct 816 ms 31940 KB Output is correct
64 Correct 1271 ms 35372 KB Output is correct
65 Correct 1967 ms 38028 KB Output is correct
66 Correct 2050 ms 38076 KB Output is correct
67 Correct 2024 ms 38040 KB Output is correct
68 Correct 1976 ms 38024 KB Output is correct
69 Correct 1940 ms 38028 KB Output is correct
70 Correct 266 ms 37812 KB Output is correct
71 Correct 263 ms 37820 KB Output is correct
72 Correct 281 ms 37820 KB Output is correct
73 Correct 59 ms 35772 KB Output is correct
74 Correct 108 ms 37668 KB Output is correct
75 Correct 692 ms 11344 KB Output is correct
76 Correct 2 ms 5712 KB Output is correct
77 Correct 40 ms 19056 KB Output is correct
78 Correct 49 ms 19648 KB Output is correct
79 Correct 50 ms 22204 KB Output is correct
80 Correct 65 ms 28452 KB Output is correct
81 Correct 72 ms 28632 KB Output is correct
82 Correct 80 ms 28532 KB Output is correct
83 Correct 76 ms 28592 KB Output is correct
84 Correct 70 ms 28604 KB Output is correct
85 Correct 69 ms 28604 KB Output is correct
86 Correct 67 ms 28604 KB Output is correct
87 Correct 77 ms 28500 KB Output is correct
88 Correct 72 ms 28860 KB Output is correct
89 Correct 88 ms 29404 KB Output is correct
90 Correct 105 ms 31336 KB Output is correct
91 Correct 80 ms 28860 KB Output is correct
92 Correct 72 ms 28904 KB Output is correct
93 Correct 82 ms 28728 KB Output is correct
94 Correct 73 ms 28592 KB Output is correct
95 Correct 66 ms 28604 KB Output is correct
96 Correct 74 ms 28604 KB Output is correct
97 Correct 70 ms 28604 KB Output is correct
98 Correct 79 ms 28604 KB Output is correct
99 Correct 74 ms 28604 KB Output is correct
100 Correct 81 ms 28444 KB Output is correct
101 Correct 76 ms 28612 KB Output is correct
102 Correct 75 ms 28604 KB Output is correct
103 Correct 71 ms 28616 KB Output is correct
104 Correct 76 ms 28556 KB Output is correct
105 Correct 99 ms 28744 KB Output is correct
106 Correct 103 ms 28860 KB Output is correct
107 Correct 77 ms 29116 KB Output is correct
108 Correct 84 ms 30156 KB Output is correct
109 Correct 100 ms 31424 KB Output is correct
110 Correct 2 ms 5712 KB Output is correct
111 Correct 3 ms 9808 KB Output is correct
112 Correct 6 ms 9808 KB Output is correct
113 Correct 1709 ms 22472 KB Output is correct
114 Correct 1834 ms 22632 KB Output is correct
115 Correct 2001 ms 28660 KB Output is correct
116 Correct 2328 ms 31760 KB Output is correct
117 Correct 2357 ms 31760 KB Output is correct
118 Correct 2332 ms 31760 KB Output is correct
119 Correct 2133 ms 31760 KB Output is correct
120 Correct 2222 ms 31756 KB Output is correct
121 Correct 2244 ms 31680 KB Output is correct
122 Correct 2283 ms 31760 KB Output is correct
123 Correct 657 ms 11324 KB Output is correct
124 Correct 1872 ms 31680 KB Output is correct
125 Correct 1056 ms 29916 KB Output is correct
126 Correct 2473 ms 31880 KB Output is correct
127 Correct 2347 ms 31880 KB Output is correct
128 Correct 2495 ms 31904 KB Output is correct
129 Correct 2355 ms 31876 KB Output is correct
130 Correct 2517 ms 31880 KB Output is correct
131 Correct 2265 ms 37076 KB Output is correct
132 Correct 2314 ms 37940 KB Output is correct
133 Correct 2410 ms 34912 KB Output is correct
134 Correct 2336 ms 31920 KB Output is correct
135 Correct 2465 ms 31888 KB Output is correct
136 Correct 2336 ms 31884 KB Output is correct
137 Correct 2425 ms 32072 KB Output is correct
138 Correct 2294 ms 32176 KB Output is correct
139 Correct 2278 ms 32064 KB Output is correct
140 Correct 2169 ms 32180 KB Output is correct
141 Correct 2270 ms 32180 KB Output is correct
142 Correct 2326 ms 32072 KB Output is correct
143 Correct 67 ms 29628 KB Output is correct
144 Correct 110 ms 31420 KB Output is correct