P18676 [HackerEarth] Asif loves chocolates. He eats a lot of chocolates every day and has become very fat. So his father has decided to make him walk.
普及
题目描述

Asif loves chocolates. He eats a lot of chocolates every day and has become very fat. So his father has decided to make him walk.

Asif's father has given him a binary tree of N nodes. Some of these nodes would contain chocolates(A node can contain maximum one chocolate). Now Asif's father asks him to start from a node numbered M. Asif is allowed to eat chocolate only while standing on this node. Also he can carry only one chocolate at a time. So Asif will start from M, collect any one chocolate and bring it back to M, and eat it. He continues this process until no chocolates are left.

Initially there will be no chocolates on any nodes
You task is to process queries of 2 types.

Type 0: For the given M, find out the distance that Asif should walk to collect all the chocolates.

Type 1: Place a chocolate on node M(if it doesn't already contain a chocolate).

Note : After processing a query of type 0, you should not remove the chocolates from the nodes.

Root of the tree will be numbered 1, and a node numbered x will have children numbered 2x and 2x+1. Distance between any two connected nodes is 1.

Input:
First line will contain N(1 ≤ N ≤ 100000), no of nodes.
Next line will contain Q(1 ≤ Q ≤ 100000), number of queries you need to process
Each of the next Q lines will contain two integers, Type(0 or 1) and M(1 ≤ M ≤ N).

Output:
For each query of Type 0, print the distance that Asif should walk.

SAMPLE INPUT
6
9
0 1
1 4
0 2
1 5
0 1
1 4
0 1
1 1
0 6

SAMPLE OUTPUT
0
2
8
8
20

Explanation

0 1: There are no chocolate initially, so output 0
1 4: Chocolate added to node 4
0 2: Path traversed is 2->4->2 so output 2
1 5: Chocolate added to node 5
0 1: Path traversed, 1->2->4->2->1->2>5>2>1 output 8
1 4: Node 4 already had a chocolate. So do nothing
0 1: Path traversed, 1->2->4->2->1->2>5>2>1 output 8
1 1: Chocolate added to node 1
0 6: Path traversed 6->3->1>2->4->2->1->3->6->3->1->2->5->2->1->3->6 ->3->1->3->6 output 20.

输入格式

见题面

输出格式

见题面

样例输入
15 15 0 4 0 1 1 1 0 3 0 15 1 2 0 4 1 14 1 15 1 11 0 11 0 15 1 7 0 14 0 1
样例输出
88 76 92 130 168 236 174 200 444 392 514 426 496 514 514 550 546 590 512 548 562 682 614 502 582 738 726 666 816 782 918 890 1074 1042 1102 1110 1006 1120 1000 1124 972 1018 970 1238 1182 1386 1106 1350 1322 1568 1384 1552 1560 1548 1624 1404 1496 1656 1712 1668 1522 1832 1744 1806 1648 1842 1862 1954 1706 1742 1814 1962 1946 1466 1814 1616 1754 1686 1926 1986 2126 2182 1714 1918 1914 2350 2194 2394 2470 2058 2344 1984 1968 2442 2434 2380 1828 2520 2164 2580 2294 1458 2490 2314 2810 2614 2286 2558 1878 2734 2382 2682 2530 2634 2812 2916 2100 2824 2374 2902 2750 2950 2954 2006 2982 2982 3018 2798 3062 3314 3354 3412 3216 3030 2828 3366 3298 3692 2652 3080 3368 3356 3638 3662 3666 3440 3792 3498 2986 3526 3768 3648 3858 3578 3642 3626 3814 3958 3616 4214 4332 4072 4394 4098 3058 4190 2430 4278 4610 3042 4590 4222 4582 4300 4734 3848 4722 4932 5066 4374 5004 5066 4962 4978 3462 4354 3542 5258 5186 5400 5056 5472 5468 5544 4902 5576 5272 4044 5698 3832 5506 5076 5462 5382 5374 5638 4688 5296 6122 5762 5982 6024 5184 3696 5284 5622 6234 5000 5040 5390 5542 5514 5406 6070 5434 6496 6332 6448 5484 6072 6124 6540 6028 5794 6728 6180 6792 6684 6748 6192 6650 6842 6670 5774 6742 6966 7064 7016 4822 6708 6292 7232 6708 7164 7172 6292 6806 6646 6246 6826 7218 7394 7450 6940 6306 6974 6966 6982 7314 7624 7262 6784 7852 7696 7768 7668 7974 7962 7868 7480 7056 8088 8308 8338 8506 5336 8368 8728 7558 7606 7060 8238 8120 8248 7550 8342 8372 8904 8784 8852 7890 8336 8928 7158 8574 9118 8574 9226 9506 8778 8296 8036 8670 8770 9708 9656 9944 9864 9206 7184 9992 10052 9492 8622 9264 10270 10194 10306 10556 9184 9604 10302 10398 10556 9816 10424 10448 9256 10668 10552 10720 9844 9930 9910 9970 10154 11096 10916 11192 11112 10570 11090 11276 9730 10830 11602 10960 11400 11004 11392 9196 11036 11846 9236 10032 6604 6678 12054 9804 11172 12124 12128 12176 11576 12104 12340 11384 12420 11770 12238 12542 12134 11780 12262 12538 11520 12586 12274 12658 12554 12294 11446 11498 12644 9902 12466 12698 12790 11762 12728 11814 12144 12666 12760 13070 10598 12226 12266 12048 13030 10476 12120 11468 13536 11880 13424 13736 13436 13392 12738 13896 13840 13692 12730 13640 10142 12728 12242 12882 13806 11988 13910 11214 13072 14216 8868 14256 11240 14130 14394 13466 13474 14420 14428 13202 14186 13412 14452 12596 14236 13570 12442 13730 14492 14516 14560 14550 12530 14618 14710 13708 14648 13920 13952 14628 11728 14616 14856 11738 14784 14996 15028 14984 15088 14102 13778 13806 10060 14978 13242 14062 13046 10330 14066 15254 12056 10208 14088 15456 15486 15474