// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#define dibs reserve
#define OVER9000 1234567890
#define tisic 47
#define soclose 10e-7
// mylittlepony
using namespace std;
struct IT {
// uses [a,b) intervals
struct node {
int zac,kon,val;
int son[2];}
n0; // null node
vector<node> T; // dat structure
void constT(int akt) { // construct interval tree with root akt
node n =T[akt];
if(n.zac+1 == n.kon) return; // this node is leaf, interval [a,a)
// split the interval in half, construct subtrees for each half
for(int i =0; i < 2; i++) {
T[akt].son[i] =T.size();
if(i == 0) n.kon =(T[akt].zac+T[akt].kon)/2;
else {
n.zac =n.kon;
n.kon =T[akt].kon;}
T.push_back(n);
constT(T[akt].son[i]);}
}
IT(int N) { // constructor; T is the interval [0,N)
n0.zac =n0.val =0;
n0.kon =N;
n0.son[0] =n0.son[1] =-1;
T.push_back(n0);
constT(0);}
int get(int akt, int pos) { // get value at position pos
node n =T[akt];
if(n.zac == pos && n.kon == pos+1) return n.val; // leaf corresponding to pos
if((n.zac+n.kon)/2 > pos) return get(n.son[0],pos); // pos is in left half
else return get(n.son[1],pos);} // pos is in right half
void add(int akt, int pos, int v) { // value[pos] +=v
node n =T[akt];
T[akt].val +=v;
if(zac+1 == kon) return; // leaf, can't go any deeper
// move to the half-interval containing position pos, update it
if((n.zac+n.kon)/2 > pos) add(n.son[0],pos,v); // it's the left one
else add(n.son[1],pos,v);}; // it's the right one
// look at my code
// my code is amazing
Ly8gaW9zdHJlYW0gaXMgdG9vIG1haW5zdHJlYW0KI2luY2x1ZGUgPGNzdGRpbz4KLy8gYml0Y2ggcGxlYXNlCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPHNldD4KI2luY2x1ZGUgPG1hcD4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPHF1ZXVlPgojaW5jbHVkZSA8c3RhY2s+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNkZWZpbmUgZGlicyByZXNlcnZlCiNkZWZpbmUgT1ZFUjkwMDAgMTIzNDU2Nzg5MAojZGVmaW5lIHRpc2ljIDQ3CiNkZWZpbmUgc29jbG9zZSAxMGUtNwovLyBteWxpdHRsZXBvbnkKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnN0cnVjdCBJVCB7CiAgICAvLyB1c2VzIFthLGIpIGludGVydmFscwogICAgc3RydWN0IG5vZGUgewogICAgICAgIGludCB6YWMsa29uLHZhbDsKICAgICAgICBpbnQgc29uWzJdO30KICAgIG4wOyAvLyBudWxsIG5vZGUKICAgIHZlY3Rvcjxub2RlPiBUOyAvLyBkYXQgc3RydWN0dXJlCgogICAgdm9pZCBjb25zdFQoaW50IGFrdCkgeyAvLyBjb25zdHJ1Y3QgaW50ZXJ2YWwgdHJlZSB3aXRoIHJvb3QgYWt0CiAgICAgICAgbm9kZSBuID1UW2FrdF07CiAgICAgICAgaWYobi56YWMrMSA9PSBuLmtvbikgcmV0dXJuOyAvLyB0aGlzIG5vZGUgaXMgbGVhZiwgaW50ZXJ2YWwgW2EsYSkKICAgICAgICAvLyBzcGxpdCB0aGUgaW50ZXJ2YWwgaW4gaGFsZiwgY29uc3RydWN0IHN1YnRyZWVzIGZvciBlYWNoIGhhbGYKICAgICAgICBmb3IoaW50IGkgPTA7IGkgPCAyOyBpKyspIHsKICAgICAgICAgICAgVFtha3RdLnNvbltpXSA9VC5zaXplKCk7CiAgICAgICAgICAgIGlmKGkgPT0gMCkgbi5rb24gPShUW2FrdF0uemFjK1RbYWt0XS5rb24pLzI7CiAgICAgICAgICAgIGVsc2UgewogICAgICAgICAgICAgICAgbi56YWMgPW4ua29uOwogICAgICAgICAgICAgICAgbi5rb24gPVRbYWt0XS5rb247fQogICAgICAgICAgICBULnB1c2hfYmFjayhuKTsKICAgICAgICAgICAgY29uc3RUKFRbYWt0XS5zb25baV0pO30KICAgICAgICB9CgogICAgSVQoaW50IE4pIHsgLy8gY29uc3RydWN0b3I7IFQgaXMgdGhlIGludGVydmFsIFswLE4pCiAgICAgICAgbjAuemFjID1uMC52YWwgPTA7CiAgICAgICAgbjAua29uID1OOwogICAgICAgIG4wLnNvblswXSA9bjAuc29uWzFdID0tMTsKICAgICAgICBULnB1c2hfYmFjayhuMCk7CiAgICAgICAgY29uc3RUKDApO30KCiAgICBpbnQgZ2V0KGludCBha3QsIGludCBwb3MpIHsgLy8gZ2V0IHZhbHVlIGF0IHBvc2l0aW9uIHBvcwogICAgICAgIG5vZGUgbiA9VFtha3RdOwogICAgICAgIGlmKG4uemFjID09IHBvcyAmJiBuLmtvbiA9PSBwb3MrMSkgcmV0dXJuIG4udmFsOyAvLyBsZWFmIGNvcnJlc3BvbmRpbmcgdG8gcG9zCiAgICAgICAgaWYoKG4uemFjK24ua29uKS8yID4gcG9zKSByZXR1cm4gZ2V0KG4uc29uWzBdLHBvcyk7IC8vIHBvcyBpcyBpbiBsZWZ0IGhhbGYKICAgICAgICBlbHNlIHJldHVybiBnZXQobi5zb25bMV0scG9zKTt9IC8vIHBvcyBpcyBpbiByaWdodCBoYWxmCgogICAgdm9pZCBhZGQoaW50IGFrdCwgaW50IHBvcywgaW50IHYpIHsgLy8gdmFsdWVbcG9zXSArPXYKICAgICAgICBub2RlIG4gPVRbYWt0XTsKICAgICAgICBUW2FrdF0udmFsICs9djsKICAgICAgICBpZih6YWMrMSA9PSBrb24pIHJldHVybjsgLy8gbGVhZiwgY2FuJ3QgZ28gYW55IGRlZXBlcgogICAgICAgIC8vIG1vdmUgdG8gdGhlIGhhbGYtaW50ZXJ2YWwgY29udGFpbmluZyBwb3NpdGlvbiBwb3MsIHVwZGF0ZSBpdAogICAgICAgIGlmKChuLnphYytuLmtvbikvMiA+IHBvcykgYWRkKG4uc29uWzBdLHBvcyx2KTsgLy8gaXQncyB0aGUgbGVmdCBvbmUKICAgICAgICBlbHNlIGFkZChuLnNvblsxXSxwb3Msdik7fTsgLy8gaXQncyB0aGUgcmlnaHQgb25lCiAgICAgICAgCi8vIGxvb2sgYXQgbXkgY29kZQovLyBteSBjb2RlIGlzIGFtYXppbmc=