Binary Search Tree

아직 제출이 없습니다시간 제한2초메모리 제한256 MB

문제

This problem is about Binary Search Trees (BST), a basic data structure. The structure is a rooted binary tree which stores values in its nodes. If node xx contains value aa, all values in the left subtree of xx are less than aa, and all values in the right subtree of xx are greater than aa.

In order to unify the details, we provide an implementation of finding a value aa in a BST rooted at node xx:

Here, l\[x]l\[x] is the left child of xx, r\[x]r\[x] is the right child of xx, and w\[x]w\[x] is the value of xx. Specifically, if xx does not have a left child (right child), l\[x]l\[x] (r\[x]r\[x]) is 00.

We define A(root,a)A(\mathit{root}, a) as the array of all nodes visited by find(root,a)\text{find}(\mathit{root}, a). We also define the cost of find(root,a)\mathrm{find}(\mathit{root}, a) as _vA(root,a)w\[v].\sum\_{v \in A(\mathit{root}, a)} w\[v]\text{.}

Now there are nn empty BSTs and mm operations. Your task is to process these operations quickly. There are two different kinds of operations:

 

  • "1 ll rr ww". For each i\[l,r]i \in \[l, r], insert an integer ww into the ii-th BST. It is guaranteed that ww is not present in these BSTs. Insertion starts at the root, goes the same as find\mathrm{find}, but instead of making the last find(0,w)\mathrm{find}(0, w) call, creates a new node with value ww there and returns.
  • "2 xx aa". Calculate the cost of finding aa in the xx-th BST.

입력

The first line contains two integers, nn and mm (1n,m2105)1 \le n, m \le 2 \cdot 10^{5}), indicating the number of BSTs and the number of operations.

Then mm lines follow. Each line contains description of an operation and is formatted as either "1 ll rr ww" (1lrn1 \le l \le r \le n; 1w1091 \le w \le 10^9) or "2 xx aa" (1xn1 \le x \le n; 1a1091 \le a \le 10^9).

It is guaranteed that all inserted numbers (ww in operations of the first kind) are different from each other.

출력

For each operation of the second kind, output a single line with a single integer: the cost.