Submission #1088789

# Submission time Handle Problem Language Result Execution time Memory
1088789 2024-09-15T05:40:07 Z hahahaha Tourism (JOI23_tourism) C++17
100 / 100
2857 ms 38088 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;

using uint = unsigned int ; using ll = long long ; using ull = unsigned long long ; template < class T > using V = vector <T>; template < class T > using VV = V <V<T>>;

int popcnt ( uint x ) { return __builtin_popcount(x); } int popcnt ( ull x ) { return __builtin_popcountll(x); } int bsr ( uint x ) { return 31 - __builtin_clz(x); } int bsr ( ull x ) { return 63 - __builtin_clzll(x); } int bsf ( uint x ) { return __builtin_ctz(x); } int bsf ( ull x ) { return __builtin_ctzll(x); }

struct FastSet {
    static constexpr uint B = 64 ;
    int n, lg;
    VV<ull> seg;
    FastSet( int _n) : n(_n) {
        do {
            seg.push_back(V<ull>((_n + B - 1 ) / B));
            _n = (_n + B - 1 ) / B;
        }while (_n > 1 );
        lg = seg.size();
    }
    bool operator []( int i) const {
        return (seg[ 0 ][i / B] >> (i % B) & 1 ) != 0 ;
    }
    void set ( int i) {
        for ( int h = 0 ; h < lg; h++) {
            seg[h][i / B] |= 1ULL << (i % B); i /= B;
        }
    }
    void reset ( int i) {
        for ( int h = 0 ; h < lg; h++) {
            seg[h][i / B] &= ~( 1ULL << (i % B));
            if (seg[h][i / B]) break ;
            i /= B;
        }
    }
    int next ( int i) {
        if (i < 0) i = 0;
        if (i > n) i = n;
        for ( int h = 0 ; h < lg; h++) {
            if (i / B == seg[h].size()) break ;
            ull d = seg[h][i / B] >> (i % B);
            if (!d) {
                i = i / B + 1 ;
                continue ;
            }
            i += bsf(d);
            for ( int g = h - 1 ; g >= 0 ; g--) {
                i *= B; i += bsf(seg[g][i / B]);
            }
            return i;
        }
        return n;
    }
    int prev ( int i) {
        if (i < 0) i = 0;
        if (i > n) i = n;
        i--;
        for ( int h = 0 ; h < lg; h++) {
            if (i == -1 ) break ;
            ull d = seg[h][i / B] << ( 63 - i % 64 );
            if (!d) {
                i = i / B - 1 ;
                continue ;
            }
            i += bsr(d) - (B - 1 );
            for ( int g = h - 1 ; g >= 0 ; g--) {
                i *= B;
                i += bsr(seg[g][i / B]);
            }
            return i;
        }
        return -1 ;
    }
};

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 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.set(u);
    };
    auto erase=[&](int p){
        int u=idx[p];
        s.reset(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("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:302:5: note: in expansion of macro 'file'
  302 |     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:302:5: note: in expansion of macro 'file'
  302 |     file("cum");
      |     ^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 2 ms 2648 KB Output is correct
4 Correct 2 ms 2908 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 2 ms 2908 KB Output is correct
7 Correct 1 ms 2908 KB Output is correct
8 Correct 1 ms 2944 KB Output is correct
9 Correct 2 ms 2908 KB Output is correct
10 Correct 2 ms 2908 KB Output is correct
11 Correct 2 ms 2908 KB Output is correct
12 Correct 1 ms 2908 KB Output is correct
13 Correct 3 ms 2908 KB Output is correct
14 Correct 2 ms 2960 KB Output is correct
15 Correct 2 ms 2908 KB Output is correct
16 Correct 2 ms 2908 KB Output is correct
17 Correct 2 ms 2908 KB Output is correct
18 Correct 2 ms 2908 KB Output is correct
19 Correct 2 ms 2908 KB Output is correct
20 Correct 2 ms 2908 KB Output is correct
21 Correct 2 ms 2904 KB Output is correct
22 Correct 2 ms 2908 KB Output is correct
23 Correct 2 ms 2908 KB Output is correct
24 Correct 2 ms 2908 KB Output is correct
25 Correct 2 ms 2908 KB Output is correct
26 Correct 4 ms 2904 KB Output is correct
27 Correct 2 ms 2652 KB Output is correct
28 Correct 1 ms 2908 KB Output is correct
29 Correct 2 ms 2908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 2 ms 2648 KB Output is correct
4 Correct 2 ms 2908 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 2 ms 2908 KB Output is correct
7 Correct 1 ms 2908 KB Output is correct
8 Correct 1 ms 2944 KB Output is correct
9 Correct 2 ms 2908 KB Output is correct
10 Correct 2 ms 2908 KB Output is correct
11 Correct 2 ms 2908 KB Output is correct
12 Correct 1 ms 2908 KB Output is correct
13 Correct 3 ms 2908 KB Output is correct
14 Correct 2 ms 2960 KB Output is correct
15 Correct 2 ms 2908 KB Output is correct
16 Correct 2 ms 2908 KB Output is correct
17 Correct 2 ms 2908 KB Output is correct
18 Correct 2 ms 2908 KB Output is correct
19 Correct 2 ms 2908 KB Output is correct
20 Correct 2 ms 2908 KB Output is correct
21 Correct 2 ms 2904 KB Output is correct
22 Correct 2 ms 2908 KB Output is correct
23 Correct 2 ms 2908 KB Output is correct
24 Correct 2 ms 2908 KB Output is correct
25 Correct 2 ms 2908 KB Output is correct
26 Correct 4 ms 2904 KB Output is correct
27 Correct 2 ms 2652 KB Output is correct
28 Correct 1 ms 2908 KB Output is correct
29 Correct 2 ms 2908 KB Output is correct
30 Correct 6 ms 3164 KB Output is correct
31 Correct 10 ms 3296 KB Output is correct
32 Correct 10 ms 3420 KB Output is correct
33 Correct 9 ms 3412 KB Output is correct
34 Correct 10 ms 3420 KB Output is correct
35 Correct 3 ms 3420 KB Output is correct
36 Correct 3 ms 3420 KB Output is correct
37 Correct 6 ms 3420 KB Output is correct
38 Correct 10 ms 3420 KB Output is correct
39 Correct 9 ms 3400 KB Output is correct
40 Correct 9 ms 3420 KB Output is correct
41 Correct 3 ms 3420 KB Output is correct
42 Correct 3 ms 3420 KB Output is correct
43 Correct 3 ms 3420 KB Output is correct
44 Correct 9 ms 3476 KB Output is correct
45 Correct 10 ms 3416 KB Output is correct
46 Correct 11 ms 3420 KB Output is correct
47 Correct 3 ms 3420 KB Output is correct
48 Correct 4 ms 3420 KB Output is correct
49 Correct 3 ms 3468 KB Output is correct
50 Correct 9 ms 3420 KB Output is correct
51 Correct 9 ms 3436 KB Output is correct
52 Correct 10 ms 3420 KB Output is correct
53 Correct 9 ms 3420 KB Output is correct
54 Correct 9 ms 3444 KB Output is correct
55 Correct 9 ms 3448 KB Output is correct
56 Correct 5 ms 2948 KB Output is correct
57 Correct 2 ms 3164 KB Output is correct
58 Correct 3 ms 3420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 5 ms 2700 KB Output is correct
4 Correct 1548 ms 23636 KB Output is correct
5 Correct 844 ms 30160 KB Output is correct
6 Correct 1519 ms 33776 KB Output is correct
7 Correct 2329 ms 38052 KB Output is correct
8 Correct 2291 ms 38088 KB Output is correct
9 Correct 2237 ms 38020 KB Output is correct
10 Correct 2296 ms 38012 KB Output is correct
11 Correct 2281 ms 38012 KB Output is correct
12 Correct 276 ms 37828 KB Output is correct
13 Correct 298 ms 37824 KB Output is correct
14 Correct 261 ms 37796 KB Output is correct
15 Correct 53 ms 34756 KB Output is correct
16 Correct 105 ms 37468 KB Output is correct
17 Correct 625 ms 7504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2652 KB Output is correct
2 Correct 38 ms 17068 KB Output is correct
3 Correct 57 ms 18888 KB Output is correct
4 Correct 49 ms 20164 KB Output is correct
5 Correct 75 ms 28504 KB Output is correct
6 Correct 74 ms 28452 KB Output is correct
7 Correct 74 ms 28528 KB Output is correct
8 Correct 73 ms 28604 KB Output is correct
9 Correct 76 ms 28420 KB Output is correct
10 Correct 77 ms 28564 KB Output is correct
11 Correct 80 ms 28544 KB Output is correct
12 Correct 74 ms 28540 KB Output is correct
13 Correct 77 ms 28868 KB Output is correct
14 Correct 94 ms 29380 KB Output is correct
15 Correct 110 ms 31424 KB Output is correct
16 Correct 87 ms 28888 KB Output is correct
17 Correct 76 ms 28864 KB Output is correct
18 Correct 90 ms 28868 KB Output is correct
19 Correct 63 ms 28612 KB Output is correct
20 Correct 76 ms 28612 KB Output is correct
21 Correct 67 ms 28612 KB Output is correct
22 Correct 73 ms 28612 KB Output is correct
23 Correct 71 ms 28612 KB Output is correct
24 Correct 72 ms 28616 KB Output is correct
25 Correct 75 ms 28512 KB Output is correct
26 Correct 77 ms 28616 KB Output is correct
27 Correct 74 ms 28556 KB Output is correct
28 Correct 85 ms 28772 KB Output is correct
29 Correct 74 ms 28608 KB Output is correct
30 Correct 73 ms 28612 KB Output is correct
31 Correct 73 ms 28872 KB Output is correct
32 Correct 82 ms 29208 KB Output is correct
33 Correct 85 ms 30148 KB Output is correct
34 Correct 98 ms 31432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 6 ms 2908 KB Output is correct
4 Correct 1922 ms 21024 KB Output is correct
5 Correct 2044 ms 21508 KB Output is correct
6 Correct 2496 ms 28112 KB Output is correct
7 Correct 2508 ms 31748 KB Output is correct
8 Correct 2564 ms 31828 KB Output is correct
9 Correct 2597 ms 31752 KB Output is correct
10 Correct 2386 ms 31744 KB Output is correct
11 Correct 2617 ms 31748 KB Output is correct
12 Correct 2585 ms 31688 KB Output is correct
13 Correct 2529 ms 31740 KB Output is correct
14 Correct 618 ms 7304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 2 ms 2648 KB Output is correct
4 Correct 2 ms 2908 KB Output is correct
5 Correct 2 ms 2908 KB Output is correct
6 Correct 2 ms 2908 KB Output is correct
7 Correct 1 ms 2908 KB Output is correct
8 Correct 1 ms 2944 KB Output is correct
9 Correct 2 ms 2908 KB Output is correct
10 Correct 2 ms 2908 KB Output is correct
11 Correct 2 ms 2908 KB Output is correct
12 Correct 1 ms 2908 KB Output is correct
13 Correct 3 ms 2908 KB Output is correct
14 Correct 2 ms 2960 KB Output is correct
15 Correct 2 ms 2908 KB Output is correct
16 Correct 2 ms 2908 KB Output is correct
17 Correct 2 ms 2908 KB Output is correct
18 Correct 2 ms 2908 KB Output is correct
19 Correct 2 ms 2908 KB Output is correct
20 Correct 2 ms 2908 KB Output is correct
21 Correct 2 ms 2904 KB Output is correct
22 Correct 2 ms 2908 KB Output is correct
23 Correct 2 ms 2908 KB Output is correct
24 Correct 2 ms 2908 KB Output is correct
25 Correct 2 ms 2908 KB Output is correct
26 Correct 4 ms 2904 KB Output is correct
27 Correct 2 ms 2652 KB Output is correct
28 Correct 1 ms 2908 KB Output is correct
29 Correct 2 ms 2908 KB Output is correct
30 Correct 6 ms 3164 KB Output is correct
31 Correct 10 ms 3296 KB Output is correct
32 Correct 10 ms 3420 KB Output is correct
33 Correct 9 ms 3412 KB Output is correct
34 Correct 10 ms 3420 KB Output is correct
35 Correct 3 ms 3420 KB Output is correct
36 Correct 3 ms 3420 KB Output is correct
37 Correct 6 ms 3420 KB Output is correct
38 Correct 10 ms 3420 KB Output is correct
39 Correct 9 ms 3400 KB Output is correct
40 Correct 9 ms 3420 KB Output is correct
41 Correct 3 ms 3420 KB Output is correct
42 Correct 3 ms 3420 KB Output is correct
43 Correct 3 ms 3420 KB Output is correct
44 Correct 9 ms 3476 KB Output is correct
45 Correct 10 ms 3416 KB Output is correct
46 Correct 11 ms 3420 KB Output is correct
47 Correct 3 ms 3420 KB Output is correct
48 Correct 4 ms 3420 KB Output is correct
49 Correct 3 ms 3468 KB Output is correct
50 Correct 9 ms 3420 KB Output is correct
51 Correct 9 ms 3436 KB Output is correct
52 Correct 10 ms 3420 KB Output is correct
53 Correct 9 ms 3420 KB Output is correct
54 Correct 9 ms 3444 KB Output is correct
55 Correct 9 ms 3448 KB Output is correct
56 Correct 5 ms 2948 KB Output is correct
57 Correct 2 ms 3164 KB Output is correct
58 Correct 3 ms 3420 KB Output is correct
59 Correct 1 ms 2652 KB Output is correct
60 Correct 2 ms 2652 KB Output is correct
61 Correct 5 ms 2700 KB Output is correct
62 Correct 1548 ms 23636 KB Output is correct
63 Correct 844 ms 30160 KB Output is correct
64 Correct 1519 ms 33776 KB Output is correct
65 Correct 2329 ms 38052 KB Output is correct
66 Correct 2291 ms 38088 KB Output is correct
67 Correct 2237 ms 38020 KB Output is correct
68 Correct 2296 ms 38012 KB Output is correct
69 Correct 2281 ms 38012 KB Output is correct
70 Correct 276 ms 37828 KB Output is correct
71 Correct 298 ms 37824 KB Output is correct
72 Correct 261 ms 37796 KB Output is correct
73 Correct 53 ms 34756 KB Output is correct
74 Correct 105 ms 37468 KB Output is correct
75 Correct 625 ms 7504 KB Output is correct
76 Correct 1 ms 2652 KB Output is correct
77 Correct 38 ms 17068 KB Output is correct
78 Correct 57 ms 18888 KB Output is correct
79 Correct 49 ms 20164 KB Output is correct
80 Correct 75 ms 28504 KB Output is correct
81 Correct 74 ms 28452 KB Output is correct
82 Correct 74 ms 28528 KB Output is correct
83 Correct 73 ms 28604 KB Output is correct
84 Correct 76 ms 28420 KB Output is correct
85 Correct 77 ms 28564 KB Output is correct
86 Correct 80 ms 28544 KB Output is correct
87 Correct 74 ms 28540 KB Output is correct
88 Correct 77 ms 28868 KB Output is correct
89 Correct 94 ms 29380 KB Output is correct
90 Correct 110 ms 31424 KB Output is correct
91 Correct 87 ms 28888 KB Output is correct
92 Correct 76 ms 28864 KB Output is correct
93 Correct 90 ms 28868 KB Output is correct
94 Correct 63 ms 28612 KB Output is correct
95 Correct 76 ms 28612 KB Output is correct
96 Correct 67 ms 28612 KB Output is correct
97 Correct 73 ms 28612 KB Output is correct
98 Correct 71 ms 28612 KB Output is correct
99 Correct 72 ms 28616 KB Output is correct
100 Correct 75 ms 28512 KB Output is correct
101 Correct 77 ms 28616 KB Output is correct
102 Correct 74 ms 28556 KB Output is correct
103 Correct 85 ms 28772 KB Output is correct
104 Correct 74 ms 28608 KB Output is correct
105 Correct 73 ms 28612 KB Output is correct
106 Correct 73 ms 28872 KB Output is correct
107 Correct 82 ms 29208 KB Output is correct
108 Correct 85 ms 30148 KB Output is correct
109 Correct 98 ms 31432 KB Output is correct
110 Correct 2 ms 2652 KB Output is correct
111 Correct 2 ms 2652 KB Output is correct
112 Correct 6 ms 2908 KB Output is correct
113 Correct 1922 ms 21024 KB Output is correct
114 Correct 2044 ms 21508 KB Output is correct
115 Correct 2496 ms 28112 KB Output is correct
116 Correct 2508 ms 31748 KB Output is correct
117 Correct 2564 ms 31828 KB Output is correct
118 Correct 2597 ms 31752 KB Output is correct
119 Correct 2386 ms 31744 KB Output is correct
120 Correct 2617 ms 31748 KB Output is correct
121 Correct 2585 ms 31688 KB Output is correct
122 Correct 2529 ms 31740 KB Output is correct
123 Correct 618 ms 7304 KB Output is correct
124 Correct 2032 ms 31444 KB Output is correct
125 Correct 1277 ms 28868 KB Output is correct
126 Correct 2763 ms 31868 KB Output is correct
127 Correct 2779 ms 31928 KB Output is correct
128 Correct 2696 ms 31836 KB Output is correct
129 Correct 2739 ms 31872 KB Output is correct
130 Correct 2785 ms 31936 KB Output is correct
131 Correct 2541 ms 37068 KB Output is correct
132 Correct 2626 ms 37812 KB Output is correct
133 Correct 2494 ms 34896 KB Output is correct
134 Correct 2857 ms 31916 KB Output is correct
135 Correct 2783 ms 31892 KB Output is correct
136 Correct 2673 ms 31872 KB Output is correct
137 Correct 2616 ms 32168 KB Output is correct
138 Correct 2589 ms 32164 KB Output is correct
139 Correct 2692 ms 32168 KB Output is correct
140 Correct 2739 ms 32076 KB Output is correct
141 Correct 2609 ms 32072 KB Output is correct
142 Correct 2602 ms 32180 KB Output is correct
143 Correct 62 ms 28616 KB Output is correct
144 Correct 114 ms 31424 KB Output is correct