/**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣤⡤⠤⣤⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡴⠛⠉⠀⠀⠀⠀⠈⠙⢷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⠶⣄⡀⠀⠀⠀⢠⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⡏⠀⠀⠉⠛⠶⣤⣸⡇⠀⠀⠀⠀⣀⣤⣶⣶⠒⠒⠒⠶⣬⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣤⠴⠶⣿⠀⠀⠀⠀⠀⠀⠈⠉⠉⠛⠒⠶⢿⣭⣀⡀⢻⡀⠀⠀⢠⡿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣤⠶⠛⠋⠁⠀⠀⢸⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠛⠷⣴⣞⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⡴⠞⠉⠀⠀⠀⠀⠀⠀⢰⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⢦⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⠞⠋⠀⠀⠀⠀⢀⡤⠠⡄⠀⢰⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠿⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡞⠁⠀⠀⠀⠀⣠⠖⠋⠀⣸⠇⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠀⢀⡀⠀⠀⠀⠀⠀⠀⢦⡀⠀⠀⠀⠸⣷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡼⠋⠀⠀⠀⠀⢀⣴⠋⢀⢀⡴⠋⠀⢀⣼⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣽⠀⣼⢿⡄⠀⠀⠀⠀⣆⠀⠉⢦⡀⠀⠀⠀⠘⢧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡟⠁⠀⠀⠀⠀⣠⠟⠇⠀⠈⠉⠁⠀⣀⣾⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡟⢰⠏⠀⠻⣄⠀⠀⠀⠹⣄⣰⠟⠁⠀⠀⠀⠀⠀⢻⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⡟⠀⢀⣀⡖⠀⣰⠏⠀⠀⠀⠀⠀⢀⣼⣿⠋⠸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣧⡟⠀⠀⠀⠹⣦⠀⠀⠀⠀⠁⠀⣶⠀⠀⣴⠛⢧⠀⢻⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣠⠞⣩⡟⠀⠀⠈⠉⠀⢀⡟⠀⠀⢀⣀⣠⣤⣾⡿⠗⠒⠚⣿⠠⣤⠀⠀⠀⠀⠀⠀⠀⢸⣿⠓⠒⠲⠶⠶⠾⢷⣤⣀⣀⠀⠀⠙⣧⠀⠹⣆⣼⠃⠀⢷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⢀⡴⠟⢁⡼⣿⠁⠀⠀⠀⠀⠀⢸⠃⠀⠀⠈⠉⢠⡾⠋⠀⠀⠀⠀⠸⣆⠙⣧⡀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠻⣆⠉⠁⠀⠀⢹⡄⠀⠈⠁⠀⠀⠘⣷⢦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⣠⡶⠋⢀⡴⠋⢰⡏⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⣠⠟⠀⠀⠀⠀⠀⠀⠀⢻⡄⢸⣷⣄⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠈⢳⣄⠀⠀⠸⣧⠀⠀⠀⠀⠀⠀⢿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⢰⣾⡥⠴⠞⠋⠀⠀⣼⠀⠀⠀⠀⠀⠀⠀⣸⠀⠀⠀⣰⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⡄⣷⠙⠷⣄⡀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢷⣄⠀⣿⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣿⡆⠀⠀⠀⠀⠀⠀⢸⡄⠀⣼⢏⣀⣤⣶⣦⣤⣶⣶⣶⣶⣶⣿⣿⣾⡆⠀⠈⠻⢦⣼⡇⢰⣶⣶⣶⣶⣶⣶⣶⣤⣤⣤⣦⠙⢧⣿⠀⠀⠀⠀⠀⠀⢸⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣿⡇⠀⠀⠀⠀⠀⠀⠘⣇⣼⠏⠘⣿⡿⢿⣿⣿⣿⣿⣿⡏⠉⠉⠉⠙⠃⠀⠀⠀⠀⠉⠁⠘⠛⢻⣿⣿⣿⣿⣿⣟⠛⢛⠷⠀⠀⣿⠀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⡟⣷⠀⠀⠀⠀⠀⠀⠀⢻⡏⠀⠀⠀⠀⣸⣿⣿⣿⣿⣿⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣾⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⢰⡏⠀⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣷⢹⣆⠀⠀⠀⠀⠀⠀⠈⣷⠀⠀⠀⠀⢹⣿⣿⣿⣿⣿⣿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⡇⠀⠀⠀⠀⣾⠀⠀⠀⠀⠀⠀⢰⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⢿⠘⣿⣆⠀⠀⠀⠀⠀⠐⠘⣧⠀⠀⠀⠘⢿⣿⣿⣿⣿⠏⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿⣿⡿⠃⠀⠀⠀⣼⠃⠀⠀⠀⠀⠀⣰⠟⠐⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠸⣇⣿⠙⣧⣄⠀⠀⠀⠀⠀⠘⢧⡀⠀⠀⠈⢹⠿⢟⡋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢻⡿⠀⡀⠀⠀⣼⠃⠀⠀⠀⠀⣀⡾⠋⠀⣴⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⣿⠀⣿⠝⠳⣤⣀⡀⠀⠀⠈⢷⣤⠇⢠⡞⠠⠾⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⠟⠁⡾⠃⣠⡾⠃⠀⠀⣀⣤⠾⠋⠀⠀⠀⡿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣸⣿⠀⠀⠀⠉⠙⠓⡶⠦⠤⣿⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠓⢰⡿⠤⠴⠶⣿⠉⠀⠀⠀⠀⠀⢠⡇⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⢻⠀⠀⠀⠀⠀⠃⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢲⣄⠀⠀⠀⠀⠀⠀⠀⢀⣴⠆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡟⠀⠀⠀⠀⠀⠀⢸⡇⣸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⡿⢸⡇⠀⠀⠀⠀⠀⢿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠛⠲⠤⣤⠤⠴⠞⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⡏⠀⠀⠀⠀⠀⠀⢸⠇⠈⣇⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⡇⢸⡇⠀⠀⠀⠀⠀⢸⣿⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢿⡇⠀⠀⠀⠀⠀⠀⢺⢀⠀⢿⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣸⢷⠛⣇⠀⠀⠀⠀⠀⠈⣿⠉⠻⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⣅⣸⠀⠀⠀⠀⠀⠀⢀⣿⢸⡀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡿⠘⣀⣿⠀⠀⠀⠀⠀⢷⢸⡄⠀⠈⠙⠶⣤⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⡴⠟⠁⠀⠈⣿⠀⠀⠀⠀⠀⠀⠈⡟⣼⡇⠀⣧⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⡇⠀⢿⢿⠀⠀⠀⠀⠀⠀⠈⣧⠀⠀⠀⠀⠀⠉⠛⠶⢤⣤⣀⣀⠀⣀⡀⠀⠀⠀⠀⠀⢀⣠⡴⠞⠋⠁⠀⠀⠀⠀⢀⡏⠀⠀⠀⠀⠀⠀⢠⡇⢹⣤⠀⢹⡄⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⠀⠀⡿⢸⡆⠀⠀⠀⠀⠀⠀⢻⡆⠀⠀⠀⠀⠀⠀⠀⠀⣿⡿⠟⠋⠉⠁⠀⠀⠀⠀⠀⢸⠁⠀⠀⠀⠀⠀⠀⠀⠀⢸⠇⠀⠀⠀⠀⠀⠀⢸⡇⠈⣟⠀⠈⣷⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⢰⡟⠀⢰⡇⠘⡇⠀⠀⠀⠀⠀⠘⠀⣷⠀⠀⠀⠀⠀⠀⠀⠀⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⡿⠆⠀⠀⠀⠀⠀⠀⣼⠁⠀⢻⡀⠀⠸⣇⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣼⠁⠀⣼⠁⠀⣿⠀⠀⠀⠀⠀⠀⠀⠻⣇⣀⠀⠀⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡀⠀⠀⠀⠀⠀⠀⠀⣰⡇⠀⠀⠀⠀⠀⠀⠀⣿⡆⠀⠈⣷⡀⠀⢻⡄⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⢰⡇⠀⢀⡏⠀⠀⢻⠀⠀⠀⠀⠀⠀⠀⠀⢻⣦⣄⠀⠀⠀⠀⣠⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣧⠀⠀⠀⠀⠀⣠⣾⣿⠀⠀⠀⠀⠀⠀⢠⣠⡇⡇⠀⠀⠸⣯⡀⠀⢷⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⡾⠀⠀⣼⠁⠀⠀⠸⡇⠀⠀⠀⠀⠀⠀⠀⠀⢿⣽⡧⠴⢶⣿⣿⠖⠒⠛⠛⠃⠀⠀⠀⠚⠋⠉⠉⠉⠙⠓⠲⢾⡛⠻⣽⠃⠀⠀⠀⠀⠀⠀⢸⣿⣃⠀⠀⠀⠀⢹⣷⠀⠘⣧⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⣸⠃⠀⢸⡏⠀⠀⠀⢀⣿⠀⠀⠀⠀⢀⣀⠀⠈⠈⢷⠖⠚⠋⠁⢹⡇⠀⢀⣴⠶⠶⢦⣄⣀⣤⡶⠶⠤⠤⠤⠶⠾⠇⢰⡏⠀⠀⠀⠀⠀⠀⠻⣿⣿⣤⡀⠀⠀⠀⠀⢿⣀⠀⠸⣆⠀⠀⠀
⠀⠀⠀⠀⠀⢰⠏⠀⢀⡿⠀⠀⠀⣰⠟⢹⣿⡄⠀⠀⠀⠻⣄⠀⠀⠘⣷⣤⣄⣀⣈⡙⠛⢹⡷⢶⣦⣴⣾⣛⣻⢯⣴⣦⠴⠖⠃⠀⢀⡾⠀⠀⠀⠀⠀⠀⠀⢰⣿⡇⠀⠹⣆⠀⠀⠸⡞⣧⣆⠀⠹⣄⠀⠀
⠀⠀⠀⠀⢠⡟⠀⠀⡼⠁⠀⠀⣰⠏⠀⠈⣟⣧⠀⠀⠀⠀⢻⣆⠀⠀⠈⣧⡿⠀⠈⠉⠛⠛⣻⣿⡿⢿⣿⡍⠉⠀⠀⠀⠀⠀⠀⠀⡾⠁⠀⠀⠀⢀⣴⠀⠀⡾⣿⠁⠀⠀⠘⣧⠀⠀⢿⠘⣟⠀⠀⢻⡄⠀
⠀⠀⢀⣠⡟⠀⢀⡾⠃⠀⠀⣰⡏⠀⠀⠀⣻⠘⣧⠀⠀⠀⠀⢻⣷⡄⠀⠘⢷⡀⠀⠀⠀⠀⠩⣉⠁⠈⣛⡁⠀⠀⠀⠀⠀⠀⣀⣾⠁⠀⠀⣀⣠⣟⠁⠀⣠⣤⡟⠀⠀⠀⠀⠘⣧⡄⠘⡇⠙⣇⠀⠀⠻
*/
#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__ 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;
vector<pii>segments;
struct seg
{
struct node
{
int mn,mx;
node(){mn=inf,mx=-inf;}
}t[N*4];
int lz[N*4];
node combine(node&a,node&b)
{
node ans;
ans.mn=min(a.mn,b.mn);
ans.mx=max(a.mx,b.mx);
return ans;
}
void push(int id)
{
if(!lz[id])return;
int x=lz[id];
FOR(i,id*2,id*2+1)t[i].mn=t[i].mx=lz[i]=x;
lz[id]=0;
}
void up(int id,int l,int r,int x,int y,int v)
{
if(x>r||y<l)return;
if(x<=l&&r<=y)
{
t[id].mn=t[id].mx=lz[id]=v;
assert(v!=0);
return;
}
push(id);
int m=(l+r)/2;
up(id*2,l,m,x,y,v);
up(id*2+1,m+1,r,x,y,v);
t[id]=combine(t[id*2],t[id*2+1]);
}
void get(int id,int l,int r,int x,int y)
{
if(x>r||y<l)return;
if(x<=l&&r<=y&&t[id].mn==t[id].mx)
{
segments.pb({r-l+1,t[id].mn});
return;
}
int m=(l+r)/2;
push(id);
get(id*2+1,m+1,r,x,y);
get(id*2,l,m,x,y);
}
int Find(int id,int l,int r,int p)
{
if(p>r||p<l)return 0;
if(l==r)return t[id].mx;
int m=(l+r)/2;
push(id);
return Find(id*2,l,m,p)+Find(id*2+1,m+1,r,p);
}
}T;
struct Fenwick
{
int bit[N];
int n;
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;
}
}fen;
vector<int>adj[N];
int n,c[N];
int h[N],head[N],sz[N],heavy[N],par[N],id[N];
int cnt;
void dfs(int u,int p=0)
{
h[u]=h[p]+1;
sz[u]=1;
for(int v:adj[u])if(v!=p)
{
par[v]=u;
dfs(v,u);
sz[u]+=sz[v];
if(sz[v]>sz[heavy[u]])heavy[u]=v;
}
}
void hld(int u,int h=1)
{
head[u]=h;
id[u]=++cnt;
if(heavy[u])hld(heavy[u],h);
for(int v:adj[u])if(v!=par[u]&&v!=heavy[u])
{
hld(v,v);
}
}
i64 get(int u,int v)
{
segments.clear();
int x=1,y=u;
while(head[x]!=head[y])
{
if(h[head[x]]>h[head[y]])swap(x,y);
T.get(1,1,n,id[head[y]],id[y]);
T.up(1,1,n,id[head[y]],id[y],c[v]);
y=par[head[y]];
}
if(h[x]>h[y])swap(x,y);
T.get(1,1,n,id[x],id[y]);
T.up(1,1,n,id[x],id[y],c[v]);
i64 ans=0;
for(auto&&[len,val]:segments)
{
ans+=1ll*len*fen.get(val-1);
fen.add(val,len);
}
for(auto&&[len,val]:segments)fen.add(val,-len);
return ans;
}
die_hard_onimai_fan
{
cin>>n;
vector<int>val;
FOR(i,1,n)
{
cin>>c[i];
val.pb(c[i]);
}
sort(all(val));
val.erase(unique(all(val)),val.end());
FOR(i,1,n)c[i]=lower_bound(all(val),c[i])-val.begin()+1;
vector<pii>eds;
FOR(i,2,n)
{
int u,v;
cin>>u>>v;
eds.pb({u,v});
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1);
hld(1);
FOR(i,1,n)T.up(1,1,n,id[i],id[i],c[i]);
for(auto&&[u,v]:eds)cout<<get(u,v)<<'\n';
}
__sumairu__
{
FAST
file("");
int tt=1;//cin>>tt;
while(tt--)seggs();
cerr<<"\nTime elapsed: "<<TIME<<" s.\n";
}
/**
10
1 7 3 4 8 6 2 9 10 5
1 2
1 3
2 4
3 5
2 6
3 7
4 8
5 9
6 10
*/
Compilation message
construction.cpp:57:21: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
57 | #define __sumairu__ main()
| ^~~~
construction.cpp:260:1: note: in expansion of macro '__sumairu__'
260 | __sumairu__
| ^~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.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);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
construction.cpp:263:5: note: in expansion of macro 'file'
263 | file("");
| ^~~~
construction.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);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:263:5: note: in expansion of macro 'file'
263 | file("");
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5976 KB |
Output is correct |
2 |
Correct |
3 ms |
5980 KB |
Output is correct |
3 |
Correct |
3 ms |
5980 KB |
Output is correct |
4 |
Correct |
3 ms |
5980 KB |
Output is correct |
5 |
Correct |
3 ms |
5976 KB |
Output is correct |
6 |
Correct |
3 ms |
5980 KB |
Output is correct |
7 |
Correct |
4 ms |
5980 KB |
Output is correct |
8 |
Correct |
2 ms |
5980 KB |
Output is correct |
9 |
Correct |
3 ms |
5956 KB |
Output is correct |
10 |
Correct |
3 ms |
5980 KB |
Output is correct |
11 |
Correct |
3 ms |
5980 KB |
Output is correct |
12 |
Correct |
3 ms |
5980 KB |
Output is correct |
13 |
Correct |
3 ms |
5952 KB |
Output is correct |
14 |
Correct |
3 ms |
5976 KB |
Output is correct |
15 |
Correct |
3 ms |
5952 KB |
Output is correct |
16 |
Correct |
6 ms |
5980 KB |
Output is correct |
17 |
Correct |
3 ms |
5980 KB |
Output is correct |
18 |
Correct |
4 ms |
5980 KB |
Output is correct |
19 |
Correct |
3 ms |
5980 KB |
Output is correct |
20 |
Correct |
3 ms |
5980 KB |
Output is correct |
21 |
Correct |
3 ms |
5980 KB |
Output is correct |
22 |
Correct |
3 ms |
5980 KB |
Output is correct |
23 |
Correct |
3 ms |
5960 KB |
Output is correct |
24 |
Correct |
3 ms |
5980 KB |
Output is correct |
25 |
Correct |
3 ms |
5980 KB |
Output is correct |
26 |
Correct |
3 ms |
5864 KB |
Output is correct |
27 |
Correct |
3 ms |
5952 KB |
Output is correct |
28 |
Correct |
3 ms |
5980 KB |
Output is correct |
29 |
Correct |
3 ms |
5980 KB |
Output is correct |
30 |
Correct |
3 ms |
5980 KB |
Output is correct |
31 |
Correct |
3 ms |
5980 KB |
Output is correct |
32 |
Correct |
3 ms |
5976 KB |
Output is correct |
33 |
Correct |
3 ms |
5980 KB |
Output is correct |
34 |
Correct |
3 ms |
5980 KB |
Output is correct |
35 |
Correct |
3 ms |
5980 KB |
Output is correct |
36 |
Correct |
4 ms |
5976 KB |
Output is correct |
37 |
Correct |
3 ms |
5976 KB |
Output is correct |
38 |
Correct |
4 ms |
5980 KB |
Output is correct |
39 |
Correct |
3 ms |
5824 KB |
Output is correct |
40 |
Correct |
3 ms |
5980 KB |
Output is correct |
41 |
Correct |
3 ms |
5980 KB |
Output is correct |
42 |
Correct |
3 ms |
5980 KB |
Output is correct |
43 |
Correct |
3 ms |
5948 KB |
Output is correct |
44 |
Correct |
3 ms |
5976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5976 KB |
Output is correct |
2 |
Correct |
3 ms |
5980 KB |
Output is correct |
3 |
Correct |
3 ms |
5980 KB |
Output is correct |
4 |
Correct |
3 ms |
5980 KB |
Output is correct |
5 |
Correct |
3 ms |
5976 KB |
Output is correct |
6 |
Correct |
3 ms |
5980 KB |
Output is correct |
7 |
Correct |
4 ms |
5980 KB |
Output is correct |
8 |
Correct |
2 ms |
5980 KB |
Output is correct |
9 |
Correct |
3 ms |
5956 KB |
Output is correct |
10 |
Correct |
3 ms |
5980 KB |
Output is correct |
11 |
Correct |
3 ms |
5980 KB |
Output is correct |
12 |
Correct |
3 ms |
5980 KB |
Output is correct |
13 |
Correct |
3 ms |
5952 KB |
Output is correct |
14 |
Correct |
3 ms |
5976 KB |
Output is correct |
15 |
Correct |
3 ms |
5952 KB |
Output is correct |
16 |
Correct |
6 ms |
5980 KB |
Output is correct |
17 |
Correct |
3 ms |
5980 KB |
Output is correct |
18 |
Correct |
4 ms |
5980 KB |
Output is correct |
19 |
Correct |
3 ms |
5980 KB |
Output is correct |
20 |
Correct |
3 ms |
5980 KB |
Output is correct |
21 |
Correct |
3 ms |
5980 KB |
Output is correct |
22 |
Correct |
3 ms |
5980 KB |
Output is correct |
23 |
Correct |
3 ms |
5960 KB |
Output is correct |
24 |
Correct |
3 ms |
5980 KB |
Output is correct |
25 |
Correct |
3 ms |
5980 KB |
Output is correct |
26 |
Correct |
3 ms |
5864 KB |
Output is correct |
27 |
Correct |
3 ms |
5952 KB |
Output is correct |
28 |
Correct |
3 ms |
5980 KB |
Output is correct |
29 |
Correct |
3 ms |
5980 KB |
Output is correct |
30 |
Correct |
3 ms |
5980 KB |
Output is correct |
31 |
Correct |
3 ms |
5980 KB |
Output is correct |
32 |
Correct |
3 ms |
5976 KB |
Output is correct |
33 |
Correct |
3 ms |
5980 KB |
Output is correct |
34 |
Correct |
3 ms |
5980 KB |
Output is correct |
35 |
Correct |
3 ms |
5980 KB |
Output is correct |
36 |
Correct |
4 ms |
5976 KB |
Output is correct |
37 |
Correct |
3 ms |
5976 KB |
Output is correct |
38 |
Correct |
4 ms |
5980 KB |
Output is correct |
39 |
Correct |
3 ms |
5824 KB |
Output is correct |
40 |
Correct |
3 ms |
5980 KB |
Output is correct |
41 |
Correct |
3 ms |
5980 KB |
Output is correct |
42 |
Correct |
3 ms |
5980 KB |
Output is correct |
43 |
Correct |
3 ms |
5948 KB |
Output is correct |
44 |
Correct |
3 ms |
5976 KB |
Output is correct |
45 |
Correct |
4 ms |
5980 KB |
Output is correct |
46 |
Correct |
11 ms |
6220 KB |
Output is correct |
47 |
Correct |
10 ms |
6220 KB |
Output is correct |
48 |
Correct |
10 ms |
6308 KB |
Output is correct |
49 |
Correct |
9 ms |
6564 KB |
Output is correct |
50 |
Correct |
8 ms |
6488 KB |
Output is correct |
51 |
Correct |
6 ms |
6488 KB |
Output is correct |
52 |
Correct |
6 ms |
6492 KB |
Output is correct |
53 |
Correct |
7 ms |
6492 KB |
Output is correct |
54 |
Correct |
7 ms |
6492 KB |
Output is correct |
55 |
Correct |
7 ms |
6492 KB |
Output is correct |
56 |
Correct |
6 ms |
6492 KB |
Output is correct |
57 |
Correct |
9 ms |
6236 KB |
Output is correct |
58 |
Correct |
12 ms |
6312 KB |
Output is correct |
59 |
Correct |
11 ms |
6236 KB |
Output is correct |
60 |
Correct |
11 ms |
6236 KB |
Output is correct |
61 |
Correct |
7 ms |
6492 KB |
Output is correct |
62 |
Correct |
7 ms |
6492 KB |
Output is correct |
63 |
Correct |
7 ms |
6492 KB |
Output is correct |
64 |
Correct |
8 ms |
6236 KB |
Output is correct |
65 |
Correct |
8 ms |
6372 KB |
Output is correct |
66 |
Correct |
9 ms |
6232 KB |
Output is correct |
67 |
Correct |
9 ms |
6312 KB |
Output is correct |
68 |
Correct |
6 ms |
6492 KB |
Output is correct |
69 |
Correct |
7 ms |
6428 KB |
Output is correct |
70 |
Correct |
6 ms |
6492 KB |
Output is correct |
71 |
Correct |
6 ms |
6312 KB |
Output is correct |
72 |
Correct |
10 ms |
6236 KB |
Output is correct |
73 |
Correct |
11 ms |
6224 KB |
Output is correct |
74 |
Correct |
7 ms |
6492 KB |
Output is correct |
75 |
Correct |
7 ms |
6236 KB |
Output is correct |
76 |
Correct |
7 ms |
6236 KB |
Output is correct |
77 |
Correct |
8 ms |
6420 KB |
Output is correct |
78 |
Correct |
7 ms |
6236 KB |
Output is correct |
79 |
Correct |
7 ms |
6216 KB |
Output is correct |
80 |
Correct |
8 ms |
6236 KB |
Output is correct |
81 |
Correct |
8 ms |
6464 KB |
Output is correct |
82 |
Correct |
7 ms |
6236 KB |
Output is correct |
83 |
Correct |
8 ms |
6236 KB |
Output is correct |
84 |
Correct |
8 ms |
6236 KB |
Output is correct |
85 |
Correct |
7 ms |
6216 KB |
Output is correct |
86 |
Correct |
9 ms |
6236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5976 KB |
Output is correct |
2 |
Correct |
3 ms |
5980 KB |
Output is correct |
3 |
Correct |
3 ms |
5980 KB |
Output is correct |
4 |
Correct |
3 ms |
5980 KB |
Output is correct |
5 |
Correct |
3 ms |
5976 KB |
Output is correct |
6 |
Correct |
3 ms |
5980 KB |
Output is correct |
7 |
Correct |
4 ms |
5980 KB |
Output is correct |
8 |
Correct |
2 ms |
5980 KB |
Output is correct |
9 |
Correct |
3 ms |
5956 KB |
Output is correct |
10 |
Correct |
3 ms |
5980 KB |
Output is correct |
11 |
Correct |
3 ms |
5980 KB |
Output is correct |
12 |
Correct |
3 ms |
5980 KB |
Output is correct |
13 |
Correct |
3 ms |
5952 KB |
Output is correct |
14 |
Correct |
3 ms |
5976 KB |
Output is correct |
15 |
Correct |
3 ms |
5952 KB |
Output is correct |
16 |
Correct |
6 ms |
5980 KB |
Output is correct |
17 |
Correct |
3 ms |
5980 KB |
Output is correct |
18 |
Correct |
4 ms |
5980 KB |
Output is correct |
19 |
Correct |
3 ms |
5980 KB |
Output is correct |
20 |
Correct |
3 ms |
5980 KB |
Output is correct |
21 |
Correct |
3 ms |
5980 KB |
Output is correct |
22 |
Correct |
3 ms |
5980 KB |
Output is correct |
23 |
Correct |
3 ms |
5960 KB |
Output is correct |
24 |
Correct |
3 ms |
5980 KB |
Output is correct |
25 |
Correct |
3 ms |
5980 KB |
Output is correct |
26 |
Correct |
3 ms |
5864 KB |
Output is correct |
27 |
Correct |
3 ms |
5952 KB |
Output is correct |
28 |
Correct |
3 ms |
5980 KB |
Output is correct |
29 |
Correct |
3 ms |
5980 KB |
Output is correct |
30 |
Correct |
3 ms |
5980 KB |
Output is correct |
31 |
Correct |
3 ms |
5980 KB |
Output is correct |
32 |
Correct |
3 ms |
5976 KB |
Output is correct |
33 |
Correct |
3 ms |
5980 KB |
Output is correct |
34 |
Correct |
3 ms |
5980 KB |
Output is correct |
35 |
Correct |
3 ms |
5980 KB |
Output is correct |
36 |
Correct |
4 ms |
5976 KB |
Output is correct |
37 |
Correct |
3 ms |
5976 KB |
Output is correct |
38 |
Correct |
4 ms |
5980 KB |
Output is correct |
39 |
Correct |
3 ms |
5824 KB |
Output is correct |
40 |
Correct |
3 ms |
5980 KB |
Output is correct |
41 |
Correct |
3 ms |
5980 KB |
Output is correct |
42 |
Correct |
3 ms |
5980 KB |
Output is correct |
43 |
Correct |
3 ms |
5948 KB |
Output is correct |
44 |
Correct |
3 ms |
5976 KB |
Output is correct |
45 |
Correct |
4 ms |
5980 KB |
Output is correct |
46 |
Correct |
11 ms |
6220 KB |
Output is correct |
47 |
Correct |
10 ms |
6220 KB |
Output is correct |
48 |
Correct |
10 ms |
6308 KB |
Output is correct |
49 |
Correct |
9 ms |
6564 KB |
Output is correct |
50 |
Correct |
8 ms |
6488 KB |
Output is correct |
51 |
Correct |
6 ms |
6488 KB |
Output is correct |
52 |
Correct |
6 ms |
6492 KB |
Output is correct |
53 |
Correct |
7 ms |
6492 KB |
Output is correct |
54 |
Correct |
7 ms |
6492 KB |
Output is correct |
55 |
Correct |
7 ms |
6492 KB |
Output is correct |
56 |
Correct |
6 ms |
6492 KB |
Output is correct |
57 |
Correct |
9 ms |
6236 KB |
Output is correct |
58 |
Correct |
12 ms |
6312 KB |
Output is correct |
59 |
Correct |
11 ms |
6236 KB |
Output is correct |
60 |
Correct |
11 ms |
6236 KB |
Output is correct |
61 |
Correct |
7 ms |
6492 KB |
Output is correct |
62 |
Correct |
7 ms |
6492 KB |
Output is correct |
63 |
Correct |
7 ms |
6492 KB |
Output is correct |
64 |
Correct |
8 ms |
6236 KB |
Output is correct |
65 |
Correct |
8 ms |
6372 KB |
Output is correct |
66 |
Correct |
9 ms |
6232 KB |
Output is correct |
67 |
Correct |
9 ms |
6312 KB |
Output is correct |
68 |
Correct |
6 ms |
6492 KB |
Output is correct |
69 |
Correct |
7 ms |
6428 KB |
Output is correct |
70 |
Correct |
6 ms |
6492 KB |
Output is correct |
71 |
Correct |
6 ms |
6312 KB |
Output is correct |
72 |
Correct |
10 ms |
6236 KB |
Output is correct |
73 |
Correct |
11 ms |
6224 KB |
Output is correct |
74 |
Correct |
7 ms |
6492 KB |
Output is correct |
75 |
Correct |
7 ms |
6236 KB |
Output is correct |
76 |
Correct |
7 ms |
6236 KB |
Output is correct |
77 |
Correct |
8 ms |
6420 KB |
Output is correct |
78 |
Correct |
7 ms |
6236 KB |
Output is correct |
79 |
Correct |
7 ms |
6216 KB |
Output is correct |
80 |
Correct |
8 ms |
6236 KB |
Output is correct |
81 |
Correct |
8 ms |
6464 KB |
Output is correct |
82 |
Correct |
7 ms |
6236 KB |
Output is correct |
83 |
Correct |
8 ms |
6236 KB |
Output is correct |
84 |
Correct |
8 ms |
6236 KB |
Output is correct |
85 |
Correct |
7 ms |
6216 KB |
Output is correct |
86 |
Correct |
9 ms |
6236 KB |
Output is correct |
87 |
Correct |
21 ms |
7084 KB |
Output is correct |
88 |
Correct |
67 ms |
9180 KB |
Output is correct |
89 |
Correct |
281 ms |
16708 KB |
Output is correct |
90 |
Correct |
281 ms |
16844 KB |
Output is correct |
91 |
Correct |
263 ms |
16656 KB |
Output is correct |
92 |
Correct |
129 ms |
24392 KB |
Output is correct |
93 |
Correct |
139 ms |
24528 KB |
Output is correct |
94 |
Correct |
135 ms |
24528 KB |
Output is correct |
95 |
Correct |
118 ms |
20944 KB |
Output is correct |
96 |
Correct |
136 ms |
21200 KB |
Output is correct |
97 |
Correct |
133 ms |
21196 KB |
Output is correct |
98 |
Correct |
132 ms |
21196 KB |
Output is correct |
99 |
Correct |
124 ms |
20572 KB |
Output is correct |
100 |
Correct |
331 ms |
16336 KB |
Output is correct |
101 |
Correct |
393 ms |
16592 KB |
Output is correct |
102 |
Correct |
415 ms |
16604 KB |
Output is correct |
103 |
Correct |
386 ms |
16588 KB |
Output is correct |
104 |
Correct |
171 ms |
20428 KB |
Output is correct |
105 |
Correct |
153 ms |
20432 KB |
Output is correct |
106 |
Correct |
152 ms |
20596 KB |
Output is correct |
107 |
Correct |
213 ms |
15564 KB |
Output is correct |
108 |
Correct |
241 ms |
15824 KB |
Output is correct |
109 |
Correct |
269 ms |
16220 KB |
Output is correct |
110 |
Correct |
124 ms |
23752 KB |
Output is correct |
111 |
Correct |
115 ms |
20932 KB |
Output is correct |
112 |
Correct |
127 ms |
20428 KB |
Output is correct |
113 |
Correct |
126 ms |
19492 KB |
Output is correct |
114 |
Correct |
306 ms |
16336 KB |
Output is correct |
115 |
Correct |
359 ms |
15564 KB |
Output is correct |
116 |
Correct |
147 ms |
19652 KB |
Output is correct |
117 |
Correct |
154 ms |
18376 KB |
Output is correct |
118 |
Correct |
147 ms |
17736 KB |
Output is correct |
119 |
Correct |
180 ms |
17280 KB |
Output is correct |
120 |
Correct |
151 ms |
17616 KB |
Output is correct |
121 |
Correct |
152 ms |
16848 KB |
Output is correct |
122 |
Correct |
170 ms |
16592 KB |
Output is correct |
123 |
Correct |
187 ms |
18380 KB |
Output is correct |
124 |
Correct |
180 ms |
17616 KB |
Output is correct |
125 |
Correct |
199 ms |
17352 KB |
Output is correct |
126 |
Correct |
183 ms |
17612 KB |
Output is correct |
127 |
Correct |
173 ms |
17088 KB |
Output is correct |
128 |
Correct |
192 ms |
16588 KB |
Output is correct |