Under construction

Still being built. Things may change

Binary Search Tree

Time limit2sMemory limit256 MB

Summary
Given n BSTs, insert a value w into every tree in a range [l, r], or report the sum of node values visited when searching for a in tree x.
Level

Hard8 of 10

Topics
Segment tree, Tree, Binary search
Solved
No attempts yet

Problem

A binary search tree (BST) is a basic data structure. It is a rooted binary tree that stores a value in each node. 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. To keep the details uniform, here is 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. If xx has no left child (or right child), l[x]l[x] (r[x]r[x]) is 00. Let A(root,a)A(root, a) be the array of all nodes visited by find(root, a). The cost of find(root, a) is the sum of w[v]w[v] over all v∈A(root,a)v \in A(root, a). There are nn empty BSTs and mm operations, and you must process them quickly. There are two kinds of operations. Operation 1 is written as 1 l r w: for each i∈[l,r]i \in [l, r], insert the integer ww into the ii-th BST. It is guaranteed that ww is not already present in these BSTs. Insertion starts at the root and follows the same path as find, but instead of the final find(0, w) call, it creates a new node with value ww at that position and returns. Operation 2 is written as 2 x a: calculate the cost of finding aa in the xx-th BST.

Input

The first line contains two integers nn and mm (1≤n,m≤2⋅1051 \le n, m \le 2 \cdot 10^{5}), the number of BSTs and the number of operations. Then mm lines follow, each describing one operation in the form 1 l r w (1≤l≤r≤n1 \le l \le r \le n; 1≤w≤1091 \le w \le 10^{9}) or 2 x a (1≤x≤n1 \le x \le n; 1≤a≤1091 \le a \le 10^{9}). It is guaranteed that all inserted numbers ww are different from each other.

Output

For each operation of the second kind, output one line containing the cost as a single integer.

Examples1

  1. Example 1

    Input
    3 9
    1 1 2 2
    1 1 3 1
    1 2 3 3
    2 1 2
    2 1 4
    2 2 2
    2 2 4
    2 3 2
    2 3 4
    
    Expected output
    2
    2
    2
    5
    4
    4