QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#797813 | #4046. 钥匙 | sumairu | 100 ✓ | 1206ms | 152588kb | C++20 | 17.0kb | 2024-12-03 19:00:04 | 2024-12-03 19:00:05 |
Judging History
answer
/**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣤⡤⠤⣤⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡴⠛⠉⠀⠀⠀⠀⠈⠙⢷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⠶⣄⡀⠀⠀⠀⢠⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⡏⠀⠀⠉⠛⠶⣤⣸⡇⠀⠀⠀⠀⣀⣤⣶⣶⠒⠒⠒⠶⣬⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣤⠴⠶⣿⠀⠀⠀⠀⠀⠀⠈⠉⠉⠛⠒⠶⢿⣭⣀⡀⢻⡀⠀⠀⢠⡿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣤⠶⠛⠋⠁⠀⠀⢸⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠛⠷⣴⣞⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⡴⠞⠉⠀⠀⠀⠀⠀⠀⢰⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⢦⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠞⠋⠀⠀⠀⠀⢀⡤⠠⡄⠀⢰⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠿⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡞⠁⠀⠀⠀⠀⣠⠖⠋⠀⣸⠇⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠀⢀⡀⠀⠀⠀⠀⠀⠀⢦⡀⠀⠀⠀⠸⣷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡼⠋⠀⠀⠀⠀⢀⣴⠋⢀⢀⡴⠋⠀⢀⣼⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣽⠀⣼⢿⡄⠀⠀⠀⠀⣆⠀⠉⢦⡀⠀⠀⠀⠘⢧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡟⠁⠀⠀⠀⠀⣠⠟⠇⠀⠈⠉⠁⠀⣀⣾⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡟⢰⠏⠀⠻⣄⠀⠀⠀⠹⣄⣰⠟⠁⠀⠀⠀⠀⠀⢻⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⡟⠀⢀⣀⡖⠀⣰⠏⠀⠀⠀⠀⠀⢀⣼⣿⠋⠸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣧⡟⠀⠀⠀⠹⣦⠀⠀⠀⠀⠁⠀⣶⠀⠀⣴⠛⢧⠀⢻⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣠⠞⣩⡟⠀⠀⠈⠉⠀⢀⡟⠀⠀⢀⣀⣠⣤⣾⡿⠗⠒⠚⣿⠠⣤⠀⠀⠀⠀⠀⠀⠀⢸⣿⠓⠒⠲⠶⠶⠾⢷⣤⣀⣀⠀⠀⠙⣧⠀⠹⣆⣼⠃⠀⢷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⢀⡴⠟⢁⡼⣿⠁⠀⠀⠀⠀⠀⢸⠃⠀⠀⠈⠉⢠⡾⠋⠀⠀⠀⠀⠸⣆⠙⣧⡀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠻⣆⠉⠁⠀⠀⢹⡄⠀⠈⠁⠀⠀⠘⣷⢦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⣠⡶⠋⢀⡴⠋⢰⡏⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⣠⠟⠀⠀⠀⠀⠀⠀⠀⢻⡄⢸⣷⣄⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠈⢳⣄⠀⠀⠸⣧⠀⠀⠀⠀⠀⠀⢿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⢰⣾⡥⠴⠞⠋⠀⠀⣼⠀⠀⠀⠀⠀⠀⠀⣸⠀⠀⠀⣰⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⡄⣷⠙⠷⣄⡀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢷⣄⠀⣿⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣿⡆⠀⠀⠀⠀⠀⠀⢸⡄⠀⣼⢏⣀⣤⣶⣦⣤⣶⣶⣶⣶⣶⣿⣿⣾⡆⠀⠈⠻⢦⣼⡇⢰⣶⣶⣶⣶⣶⣶⣶⣤⣤⣤⣦⠙⢧⣿⠀⠀⠀⠀⠀⠀⢸⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣿⡇⠀⠀⠀⠀⠀⠀⠘⣇⣼⠏⠘⣿⡿⢿⣿⣿⣿⣿⣿⡏⠉⠉⠉⠙⠃⠀⠀⠀⠀⠉⠁⠘⠛⢻⣿⣿⣿⣿⣿⣟⠛⢛⠷⠀⠀⣿⠀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⡟⣷⠀⠀⠀⠀⠀⠀⠀⢻⡏⠀⠀⠀⠀⣸⣿⣿⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣾⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⢰⡏⠀⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣷⢹⣆⠀⠀⠀⠀⠀⠀⠈⣷⠀⠀⠀⠀⢹⣿⣿⣿⣿⣿⣿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⣾⠀⠀⠀⠀⠀⠀⢰⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⢿⠘⣿⣆⠀⠀⠀⠀⠀⠐⠘⣧⠀⠀⠀⠘⢿⣿⣿⣿⣿⠏⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿⣿⡿⠃⠀⠀⠀⣼⠃⠀⠀⠀⠀⠀⣰⠟⠐⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠸⣇⣿⠙⣧⣄⠀⠀⠀⠀⠀⠘⢧⡀⠀⠀⠈⢹⠿⢟⡋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢻⡿⠀⡀⠀⠀⣼⠃⠀⠀⠀⠀⣀⡾⠋⠀⣴⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⣿⠀⣿⠝⠳⣤⣀⡀⠀⠀⠈⢷⣤⠇⢠⡞⠠⠾⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠟⠁⡾⠃⣠⡾⠃⠀⠀⣀⣤⠾⠋⠀⠀⠀⡿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣸⣿⠀⠀⠀⠉⠙⠓⡶⠦⠤⣿⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠓⢰⡿⠤⠴⠶⣿⠉⠀⠀⠀⠀⠀⢠⡇⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⢻⠀⠀⠀⠀⠀⠃⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢲⣄⠀⠀⠀⠀⠀⠀⠀⢀⣴⠆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡟⠀⠀⠀⠀⠀⠀⢸⡇⣸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⡿⢸⡇⠀⠀⠀⠀⠀⢿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠛⠲⠤⣤⠤⠴⠞⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⡏⠀⠀⠀⠀⠀⠀⢸⠇⠈⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⡇⢸⡇⠀⠀⠀⠀⠀⢸⣿⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢿⡇⠀⠀⠀⠀⠀⠀⢺⢀⠀⢿⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⢷⠛⣇⠀⠀⠀⠀⠀⠈⣿⠉⠻⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⣅⣸⠀⠀⠀⠀⠀⠀⢀⣿⢸⡀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡿⠘⣀⣿⠀⠀⠀⠀⠀⢷⢸⡄⠀⠈⠙⠶⣤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⡴⠟⠁⠀⠈⣿⠀⠀⠀⠀⠀⠀⠈⡟⣼⡇⠀⣧⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⡇⠀⢿⢿⠀⠀⠀⠀⠀⠀⠈⣧⠀⠀⠀⠀⠀⠉⠛⠶⢤⣤⣀⣀⠀⣀⡀⠀⠀⠀⠀⠀⢀⣠⡴⠞⠋⠁⠀⠀⠀⠀⢀⡏⠀⠀⠀⠀⠀⠀⢠⡇⢹⣤⠀⢹⡄⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⠀⠀⡿⢸⡆⠀⠀⠀⠀⠀⠀⢻⡆⠀⠀⠀⠀⠀⠀⠀⠀⣿⡿⠟⠋⠉⠁⠀⠀⠀⠀⠀⢸⠁⠀⠀⠀⠀⠀⠀⠀⠀⢸⠇⠀⠀⠀⠀⠀⠀⢸⡇⠈⣟⠀⠈⣷⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⢰⡟⠀⢰⡇⠘⡇⠀⠀⠀⠀⠀⠘⠀⣷⠀⠀⠀⠀⠀⠀⠀⠀⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⡿⠆⠀⠀⠀⠀⠀⠀⣼⠁⠀⢻⡀⠀⠸⣇⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣼⠁⠀⣼⠁⠀⣿⠀⠀⠀⠀⠀⠀⠀⠻⣇⣀⠀⠀⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡀⠀⠀⠀⠀⠀⠀⠀⣰⡇⠀⠀⠀⠀⠀⠀⠀⣿⡆⠀⠈⣷⡀⠀⢻⡄⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⢰⡇⠀⢀⡏⠀⠀⢻⠀⠀⠀⠀⠀⠀⠀⠀⢻⣦⣄⠀⠀⠀⠀⣠⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣧⠀⠀⠀⠀⠀⣠⣾⣿⠀⠀⠀⠀⠀⠀⢠⣠⡇⡇⠀⠀⠸⣯⡀⠀⢷⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⡾⠀⠀⣼⠁⠀⠀⠸⡇⠀⠀⠀⠀⠀⠀⠀⠀⢿⣽⡧⠴⢶⣿⣿⠖⠒⠛⠛⠃⠀⠀⠀⠚⠋⠉⠉⠉⠙⠓⠲⢾⡛⠻⣽⠃⠀⠀⠀⠀⠀⠀⢸⣿⣃⠀⠀⠀⠀⢹⣷⠀⠘⣧⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⣸⠃⠀⢸⡏⠀⠀⠀⢀⣿⠀⠀⠀⠀⢀⣀⠀⠈⠈⢷⠖⠚⠋⠁⢹⡇⠀⢀⣴⠶⠶⢦⣄⣀⣤⡶⠶⠤⠤⠤⠶⠾⠇⢰⡏⠀⠀⠀⠀⠀⠀⠻⣿⣿⣤⡀⠀⠀⠀⠀⢿⣀⠀⠸⣆⠀⠀⠀
⠀⠀⠀⠀⠀⢰⠏⠀⢀⡿⠀⠀⠀⣰⠟⢹⣿⡄⠀⠀⠀⠻⣄⠀⠀⠘⣷⣤⣄⣀⣈⡙⠛⢹⡷⢶⣦⣴⣾⣛⣻⢯⣴⣦⠴⠖⠃⠀⢀⡾⠀⠀⠀⠀⠀⠀⠀⢰⣿⡇⠀⠹⣆⠀⠀⠸⡞⣧⣆⠀⠹⣄⠀⠀
⠀⠀⠀⠀⢠⡟⠀⠀⡼⠁⠀⠀⣰⠏⠀⠈⣟⣧⠀⠀⠀⠀⢻⣆⠀⠀⠈⣧⡿⠀⠈⠉⠛⠛⣻⣿⡿⢿⣿⡍⠉⠀⠀⠀⠀⠀⠀⠀⡾⠁⠀⠀⠀⢀⣴⠀⠀⡾⣿⠁⠀⠀⠘⣧⠀⠀⢿⠘⣟⠀⠀⢻⡄⠀
⠀⠀⢀⣠⡟⠀⢀⡾⠃⠀⠀⣰⡏⠀⠀⠀⣻⠘⣧⠀⠀⠀⠀⢻⣷⡄⠀⠘⢷⡀⠀⠀⠀⠀⠩⣉⠁⠈⣛⡁⠀⠀⠀⠀⠀⠀⣀⣾⠁⠀⠀⣀⣠⣟⠁⠀⣠⣤⡟⠀⠀⠀⠀⠘⣧⡄⠘⡇⠙⣇⠀⠀⠻
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define i64 long long
#define ld long double
#define ull unsigned long long
#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) {cout<<#x<<' '<<x<<endl;}
#define dbgF(arr,l,r) {cout<<#arr;FOR(_i,l,r)cout<<' '<<(arr)[_i];cout<<endl;}
#define dbgArr(arr,n) {cout<<#arr;FOR(_i,1,n)cout<<' '<<(arr)[_i];cout<<endl;}
#define el '\n'
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+rd()%(r-l+1);
assert(l<=ans&&ans<=r);
return ans;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
//const i64 base=1e9+7;
//const i64 mod=(1ll<<53)+5;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://x...content-available-to-author-only...i.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
#define i64 long long
#define debug 0
//const int mod=1e9+7;
//const int mod=998244353;
const int inf=1e9;
const i64 infl=1e18;
///check the limits, dummy
const int N=5e5+5;
int n,q;
int t[N],c[N];
vector<pii>query[N];
vector<ai(3)>events[N];
vector<int>adj[N],nadj[N];
int in[N],out[N],timer;
vector<int>key[N],chest[N];
int dp[N],npar[N];
int ans[N],fa[N],sz[N],dep[N],top[N],seq[N];
void dfs_sz(int u){
if (fa[u] != 0){
adj[u].erase(find(adj[u].begin(), adj[u].end(), fa[u]));
}
sz[u] = 1;
for(auto &j : adj[u]){
fa[j] = u;
dep[j] = dep[u] + 1;
dfs_sz(j);
sz[u] += sz[j];
if (sz[j] > sz[adj[u][0]]) swap(j, adj[u][0]);
}
}
void dfs_hld(int u){
in[u] = ++timer;
seq[timer] = u;
for(auto j : adj[u]){
top[j] = (j == adj[u][0] ? top[u] : j);
dfs_hld(j);
}
out[u] = timer;
}
int lca(int u, int v){
while (top[u] != top[v]){
if (dep[top[u]] > dep[top[v]]){
u = fa[top[u]];
}
else{
v = fa[top[v]];
}
}
return dep[u] < dep[v] ? u : v;
}
int jump(int u, int k) {
if (dep[u] < k){
return -1;
}
int d = dep[u] - k;
while (dep[top[u]] > d){
u = fa[top[u]];
}
return seq[in[u] - dep[u] + d];
}
int dist(int x, int y){
int anc = lca(x, y);
return dep[x] + dep[y] - 2 * dep[anc];
}
bool isAnc(int u,int v){ return in[u]<=in[v]&&out[u]>=out[v];}
bool cmp(int x,int y){ return in[x]<in[y];}
stack<int>st;
int build(vector<int>&node)
{
int lim=sz(node)-2;
sort(all(node),cmp);
FOR(i,0,lim)
{
int u=node[i],v=node[i+1];
node.pb(lca(u,v));
}
sort(all(node),cmp);
node.erase(unique(all(node)),node.end());
while(!st.empty())st.pop();
int rt=node[0];
st.push(rt);
FOR(i,2,sz(node))
{
int u=node[i-1];
while(!isAnc(st.top(),u))st.pop();
int p=st.top();
nadj[p].pb(u);
st.push(u);
}
return rt;
}
struct Fenwick
{
vector<int>bit;
int n;
Fenwick(int n)
{
this->n=n+10;
bit.assign(n+10,0);
}
Fenwick(vector<int>a):Fenwick(sz(a))
{
FOR(i,0,sz(a)-1)add(i,a[i]);
}
int get(int i)
{
int ans=0;
for(++i;i>0;i-=i&-i)ans+=bit[i];
return ans;
}
int get(int l,int r)
{
return get(r)-get(l-1);
}
void add(int i,int v)
{
for(++i;i<n;i+=i&-i)bit[i]+=v;
}
};
die_hard_onimai_fan
{
cin>>n>>q;
FOR(i,1,n)
{
int op,x;
cin>>op>>x;
if(op==1)key[x].pb(i);
else chest[x].pb(i);
}
FOR(i,2,n)
{
int u,v;
cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
dep[1] = 1;
dfs_sz(1);
top[1] = 1;
dfs_hld(1);
FOR(i,1,q)
{
int s,t;
cin>>s>>t;
query[in[s]].pb({in[t],i});
}
FOR(i,1,n)if(sz(key[i])&&sz(chest[i]))
{
auto node=chest[i];
node.insert(node.end(),all(key[i]));
int rt=build(node);
for(int x:chest[i])dp[x]=1;
auto dfs=[&](auto&&dfs,int u)->void{
for(int v:nadj[u])
{
npar[v]=u;
dp[v]+=dp[u];
dfs(dfs,v);
}
};
dfs(dfs,rt);
auto get=[&](int x,int y){
int p=lca(x,y);
return dp[x]+dp[y]-dp[p]-dp[npar[p]];
};
for(int a:key[i])for(int b:chest[i])
{
int anc=lca(a,b);
int dis=dep[a]+dep[b]-2*dep[anc];
vector<pii>p;
for(int x:key[i])if(dist(a,x)+dist(x,b)==dis)p.pb({dist(a,x),x});
sort(all(p));
bool can=1;
int cnt=0;
FOR(i,1,sz(p)-1)
{
int x=p[i-1].se,y=p[i].se;
cnt++;
cnt=max(0,cnt-get(x,y));
if(cnt==0)
{
can=0;
break;
}
}
if(!can||cnt+1!=get(p.back().se,b))continue;
if(anc==a)
{
int t=jump(b,dep[b]-dep[a]-1);
int l1=1,r1=in[t]-1;
int l2=out[t]+1,r2=n;
int l3=in[b],r3=out[b];
events[l1].pb({l3,r3,1});
events[r1+1].pb({l3,r3,-1});
events[l2].pb({l3,r3,1});
events[r2+1].pb({l3,r3,-1});
}
else if(anc==b)
{
int t=jump(a,dep[a]-dep[b]-1);
int l1=1,r1=in[t]-1;
int l2=out[t]+1,r2=n;
int l3=in[a],r3=out[a];
events[l3].pb({l1,r1,1});
events[r3+1].pb({l1,r1,-1});
events[l3].pb({l2,r2,1});
events[r3+1].pb({l2,r2,-1});
}
else
{
int l1=in[a],r1=out[a];
int l2=in[b],r2=out[b];
events[l1].pb({l2,r2,1});
events[r1+1].pb({l2,r2,-1});
}
}
for(int x:node)
{
dp[x]=npar[x]=0;
nadj[x].clear();
}
}
Fenwick f(n);
FOR(i,1,n)
{
for(auto&&[l,r,v]:events[i])
{
f.add(l,v);
f.add(r+1,-v);
}
for(auto&&[x,id]:query[i])ans[id]=f.get(x);
}
FOR(i,1,q)cout<<ans[i]<<el;
}
__sumairu__
{
FAST
file("");
int tt=1;//cin>>tt;
while(tt--)seggs();
cerr<<"\nTime elapsed: "<<TIME<<" s.\n";
}
/**
5 3
1 2
2 2
1 3
2 3
2 2
1 2
1 3
3 4
3 5
2 4
2 5
4 2
*/
詳細信息
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 5ms
memory: 16328kb
input:
100 100 2 1 2 5 2 5 2 1 2 5 1 3 2 1 1 5 2 6 2 2 1 6 2 1 1 1 2 1 2 6 1 5 2 3 2 6 1 7 2 4 2 4 1 7 2 4 2 6 2 6 2 5 1 2 1 5 2 3 2 3 2 1 2 2 1 4 1 2 2 3 2 3 2 1 1 7 2 3 2 2 2 6 2 3 1 3 2 6 2 3 2 2 2 5 2 5 2 5 2 5 1 5 2 1 2 5 2 4 2 4 2 4 2 1 1 2 2 3 1 3 2 4 2 2 2 1 2 1 2 3 1 4 1 4 1 1 1 2 2 3 2 2 2 3 2 4 ...
output:
0 0 0 2 0 0 1 0 0 0 0 1 2 0 0 0 0 0 0 1 1 1 2 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 2 0 1 1 0 0 2 0 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 2 0 1 0 0 0 1 0 0 0 0 1 0 0 2 0 0 0 0 1 0 0 1 0 0 3 0 0 0 0 0 0 1
result:
ok 100 numbers
Test #2:
score: 10
Accepted
time: 15ms
memory: 19628kb
input:
5000 5000 1 24 2 102 1 215 2 24 2 141 2 109 2 252 1 235 2 77 2 278 2 292 1 12 2 201 2 227 2 238 1 152 1 116 2 204 2 122 1 149 2 284 2 254 2 115 2 234 2 203 2 238 2 291 2 58 1 289 2 105 1 33 2 236 2 184 2 57 2 121 2 17 2 245 2 134 1 245 2 73 1 26 2 240 2 15 1 129 2 196 1 23 2 279 2 168 2 48 2 206 2 3...
output:
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 0 1 0 1 1 0 0 0 1 0 1 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 1 0 0 0 0 0 0 ...
result:
ok 5000 numbers
Test #3:
score: 10
Accepted
time: 9ms
memory: 19708kb
input:
5000 5000 2 230 2 243 2 77 2 149 2 298 2 176 1 103 2 131 1 127 2 110 1 115 1 220 1 23 2 259 2 290 2 77 2 211 2 249 2 232 2 163 2 55 2 277 2 148 2 171 2 14 2 226 2 70 1 194 1 269 2 277 2 4 2 107 1 246 2 226 2 79 2 219 1 21 2 203 2 4 2 129 2 87 1 114 2 180 2 37 2 202 2 5 2 39 1 28 2 168 2 35 1 184 2 1...
output:
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 5000 numbers
Test #4:
score: 10
Accepted
time: 137ms
memory: 43012kb
input:
100000 100000 2 56 1 2499 2 5148 2 2178 2 106 2 5422 2 2276 1 1085 2 1681 1 528 1 4743 2 3591 2 4902 2 5943 1 2664 2 5377 2 1923 2 195 2 1765 2 3177 2 3947 2 649 2 4772 2 3331 2 547 2 2908 2 4684 2 2741 2 5343 2 2494 2 3140 2 3823 1 1817 1 5225 2 3689 2 2768 2 6070 1 3584 1 2328 1 1506 2 4544 2 3323...
output:
5205 2591 208 3767 2284 3926 1985 438 1821 416 2614 3977 96 1397 2536 322 1682 100 396 1550 3809 1219 0 594 2058 169 860 2089 2549 5999 1837 2585 23 424 3309 371 383 4510 446 1975 967 547 1136 5210 2279 3656 0 126 1893 311 7977 454 4762 258 2000 2440 3498 2989 7478 2691 608 2620 5296 4403 4699 1925 ...
result:
ok 100000 numbers
Test #5:
score: 10
Accepted
time: 117ms
memory: 42344kb
input:
100000 100000 1 771 2 5185 2 1153 1 1611 2 2438 2 3680 2 5272 2 13 2 3069 2 3468 2 2209 2 6050 2 3287 2 1728 2 4981 2 1757 2 3630 2 1746 2 3057 1 726 2 2900 2 2033 2 2601 1 1478 2 214 1 5337 2 3948 2 1703 2 5112 2 5533 2 3558 2 1891 2 3887 2 2952 1 3425 1 177 1 1297 1 1916 2 1754 2 710 2 5212 1 1117...
output:
2149 1638 699 2224 2602 2596 401 1004 4 800 557 2105 3555 2186 2732 3767 1474 1972 3163 4410 2246 1899 1484 143 2349 388 1692 574 691 1414 3048 3147 1865 1487 1970 973 2104 1430 3776 618 750 887 3095 131 2560 3480 1730 3088 1026 3257 1164 122 3208 4370 3212 997 335 524 628 5280 1407 749 321 116 315 ...
result:
ok 100000 numbers
Test #6:
score: 10
Accepted
time: 724ms
memory: 138552kb
input:
500000 1000000 1 64861 1 119062 1 65144 1 143894 1 73264 2 33253 2 173191 2 52204 1 40950 2 71504 1 94677 2 28492 2 212348 2 155025 1 189945 2 71636 2 137081 2 129336 1 150409 2 200735 1 127414 2 155968 1 83751 2 29575 1 25125 1 111882 2 83939 1 64246 1 195870 1 14834 2 10887 2 98830 1 197278 2 1160...
output:
16 1 15 4 9 21 36 70 11 24 53 18 2 62 16 30 24 51 32 10 59 122 16 8 5 55 53 22 11 12 17 12 42 70 37 26 13 34 6 101 11 22 18 21 84 30 35 39 67 24 11 8 0 25 40 1 30 42 38 121 23 37 9 9 37 45 30 33 24 5 22 43 43 20 20 14 41 6 10 20 54 36 19 97 32 19 6 48 14 48 48 80 33 2 3 12 18 11 33 1 47 27 54 17 93 ...
result:
ok 1000000 numbers
Test #7:
score: 10
Accepted
time: 731ms
memory: 138676kb
input:
500000 1000000 2 99254 2 207080 2 40178 2 203616 2 143664 1 172563 1 235135 1 39267 2 34014 2 240014 2 122830 2 65547 1 190038 1 133892 1 195039 2 242508 1 16681 1 171326 1 179195 1 28872 2 91496 2 196836 1 12722 1 133176 1 49610 1 160238 2 137275 2 82747 2 22809 2 148101 2 18675 1 120676 1 86886 1 ...
output:
90 11 22 19 119 155 49 44 37 66 62 24 11 62 89 56 28 60 10 22 177 10 95 67 57 52 84 126 93 5 106 34 53 37 8 47 53 33 135 30 105 50 51 57 4 101 91 168 11 70 1 46 142 3 131 70 49 98 121 13 61 1 68 61 38 94 20 112 31 159 23 47 47 4 155 135 109 78 142 35 36 9 24 49 0 34 58 95 98 187 91 126 6 32 31 58 63...
result:
ok 1000000 numbers
Test #8:
score: 10
Accepted
time: 709ms
memory: 138644kb
input:
500000 1000000 1 89685 2 77808 2 60421 1 3478 1 174801 2 79073 1 171086 2 189510 2 141242 2 42469 2 197806 1 17953 2 165030 1 200212 2 65004 2 67818 2 86070 1 111422 1 229646 2 68851 1 14764 1 230949 2 71728 1 45456 1 5844 1 191082 1 54319 2 135488 2 24463 1 2976 2 226836 1 57817 2 45114 2 58082 1 3...
output:
21 33 31 22 54 59 25 2 38 39 7 42 45 132 98 12 18 24 5 34 38 36 4 11 41 14 36 36 18 2 33 8 35 42 28 8 13 64 18 41 12 29 67 32 6 34 26 22 33 80 39 66 8 11 84 52 45 120 74 53 9 37 28 59 65 36 29 11 4 38 18 10 6 66 20 22 38 32 21 27 12 37 35 22 29 37 31 6 39 33 38 13 36 68 39 21 77 28 46 38 33 30 12 7 ...
result:
ok 1000000 numbers
Test #9:
score: 10
Accepted
time: 1183ms
memory: 152588kb
input:
500000 1000000 1 6055 2 7862 2 1392 2 1440 2 15064 2 460 1 29587 2 3993 2 29185 2 29543 2 583 1 19479 2 28783 2 26722 1 17041 2 10894 2 3234 2 25053 1 15206 1 6122 2 25838 1 28948 2 25858 2 27787 2 19146 2 27381 2 20541 2 24645 1 16937 2 3163 2 3194 2 25925 2 11649 2 20379 1 7937 2 1913 2 18048 1 94...
output:
411 2 789 108 281 204 76 509 45 647 9 16 279 164 433 63 173 217 324 86 107 43 202 535 464 94 64 227 267 46 13 350 216 218 575 73 128 230 365 285 131 200 75 364 9 46 8 35 271 81 100 563 436 225 545 129 255 115 150 854 115 133 119 108 517 108 726 255 32 147 292 190 78 319 79 64 176 100 360 100 289 349...
result:
ok 1000000 numbers
Test #10:
score: 10
Accepted
time: 1206ms
memory: 152492kb
input:
500000 1000000 1 21782 1 23178 2 3615 1 22855 2 689 2 18959 2 17609 2 21923 1 28277 2 16406 2 7256 1 10175 2 25925 2 15485 2 25959 2 25908 1 20117 2 14563 1 25592 2 15719 1 9408 2 9539 1 28533 2 14576 2 2884 2 2701 1 13792 1 4222 2 21169 1 9207 2 17859 2 21128 1 3543 1 25941 2 4987 2 2529 2 20743 2 ...
output:
22 96 178 186 24 410 355 316 152 349 7 628 247 152 573 216 101 30 172 189 76 8 127 95 351 69 181 139 204 175 261 6 195 96 211 39 159 550 126 40 259 324 252 48 126 342 143 513 235 331 142 173 153 257 133 120 252 318 178 775 161 216 146 18 221 78 214 248 180 172 196 180 262 501 155 149 149 228 127 187...
result:
ok 1000000 numbers
Extra Test:
score: 0
Extra Test Passed