This page is still under construction.

Parts of this page are still being built. What you see may change.

Farm Management

Time limit1sMemory limit128 MB

Summary
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 NN farms connected by N−1N-1 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 11 to NN.

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 uu and farm vv.
  • Q u v: print the total number of trees planted on the roads along the path between farm uu and farm vv.

Initially no road has any tree planted on it. Process the queries in order.

Input

The first line contains the number of farms NN and the number of queries MM. (1≤N,M≤100,0001 \le N, M \le 100{,}000)

Each of the next N−1N-1 lines contains the numbers of the two farms connected by a road, one road per line.

Each of the following MM lines contains one query, consisting of a single character (P or Q) and two integers uu and vv, 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.

Examples4

  1. Example 1

    Input
    4 6
    1 4
    2 4
    3 4
    P 2 3
    P 1 3
    Q 3 4
    P 1 4
    Q 2 4
    Q 1 4
    
    Expected output
    2
    1
    2
    
  2. Example 2

    Input
    1 3
    P 1 1
    Q 1 1
    Q 1 1
    
    Expected output
    0
    0
    
  3. Example 3

    Input
    5 6
    1 2
    2 3
    3 4
    4 5
    P 1 5
    P 2 4
    Q 1 5
    Q 2 3
    Q 5 5
    Q 3 5
    
    Expected output
    6
    2
    0
    3
    
  4. Example 4

    Input
    5 6
    1 2
    1 3
    1 4
    1 5
    P 2 3
    P 4 5
    P 2 5
    Q 2 3
    Q 4 5
    Q 3 4
    
    Expected output
    3
    3
    2