Drainage
InterviewTime limit1sMemory limit1024 MB
Merge cities into groups with each project query, then count the cities in groups whose total rainfall exceeds their total drainage capacity.
- Level
Medium4 of 10
- Topics
- Union-find, Array, Implementation
- Solved
- No attempts yet
Problem
The ChAOS country has cities, numbered through . Each city has a drainage ditch that prevents flooding. The drainage ditch of city can prevent flooding only when the rainfall is at most .
To prepare for heavy rain in a single city, the country decides to build a project that pools the drainage capacity of two cities. For example, after building the project on cities and , if the sum of the rainfall of cities and is at most , both cities 1 and 2 are protected from flooding. Otherwise, both cities flood. If the project is then built on cities and as well, and the sum of the rainfall of cities , , and is at most , all three cities are protected. Otherwise, all three cities flood.
Heavy rain is now falling across the country. The current rainfall of city is . Write a program that processes two kinds of queries.
- : Build the project on city and city .
- : Print the number of cities that flood in the current state.
There is at least one query of type .
Input
The first line contains the number of cities () and the number of queries ().
The second line contains integers ().
The third line contains integers ().
From the fourth line through line , each line contains one query in the form or ().
Output
For each query of type , print the answer on its own line.