Farm Management
Time limit1sMemory limit128 MB
A tree of N farms gets path updates that add 1 to every edge on a path, plus path queries that sum edge values on a path; process M operations online.
- Level
Hard8 of 10
- Topics
- Tree, Segment tree, Prefix sum, DFS
- Solved
- No attempts yet
Problem
There are farms connected by two-way roads. Between any two farms there is exactly one path; in other words, the farms and roads form a tree. The farms are numbered from to .
Jaehyun wants to plant trees along the roads. The work is given as queries, and there are two kinds:
P u v: plant one tree on every road along the path between farm and farm .Q u v: print the total number of trees planted on the roads along the path between farm and farm .
Initially no road has any tree planted on it. Process the queries in order.
Input
The first line contains the number of farms and the number of queries . ()
Each of the next lines contains the numbers of the two farms connected by a road, one road per line.
Each of the following lines contains one query, consisting of a single character (P or Q) and two integers and , in the format described above.
Output
For each Q query, print on its own line the number of trees planted on the roads along the given path.