rt
#include <bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for (int i = (l); i <= (r); ++i)
#define forlk(i, x, nx) for (int i = (x); i; i = nx)
typedef long long ll;
const int MAXN = 200010;
int n, m;
struct Modify
{
ll y;
int val;
} c[MAXN * 2];
struct Query
{
ll c;
int id;
} q[MAXN];
int nc[MAXN * 2], nct;
int nq[MAXN], nqt;
struct Node
{
int ch[2];
int head1;
int head2;
} trie1[MAXN * 120];
int cnt1 = 1;
inline void addModify(Node& x, Modify y)
{
nc[++nct] = x.head1;
c[nct] = y;
x.head1 = nct;
}
inline void addQuery(Node& x, Query y)
{
nq[++nqt] = x.head2;
q[nqt] = y;
x.head2 = nqt;
}
inline void Create(int &x, int &cnt)
{
if (!x) x = ++cnt;
}
inline void InsModify(ll x, Modify y)
{
int pos = 1;
while (x > 1)
{
int ch = x & 1;
Create(trie1[pos].ch[ch], cnt1);
pos = trie1[pos].ch[ch];
x >>= 1;
}
Create(trie1[pos].ch[0], cnt1);
Create(trie1[pos].ch[1], cnt1);
addModify(trie1[trie1[pos].ch[0]], y);
addModify(trie1[trie1[pos].ch[1]], y);
}
inline void InsQuery(ll x, Query y)
{
int pos = 1;
while (x > 0)
{
int ch = x & 1;
Create(trie1[pos].ch[ch], cnt1);
pos = trie1[pos].ch[ch];
x >>= 1;
}
addQuery(trie1[pos], y);
}
struct Nod
{
int ch[2];
int val;
} trie2[MAXN * 64];
int cnt2 = 1;
inline void Ins(ll x, int y)
{
int pos = 1;
while (x > 1)
{
int ch = x & 1;
Create(trie2[pos].ch[ch], cnt2);
pos = trie2[pos].ch[ch];
x >>= 1;
}
Create(trie2[pos].ch[0], cnt2);
Create(trie2[pos].ch[1], cnt2);
trie2[trie2[pos].ch[0]].val += y;
trie2[trie2[pos].ch[1]].val += y;
}
inline ll query(ll x)
{
int pos = 1;
ll ret = 0;
while (x > 0 && pos)
{
int ch = x & 1;
pos = trie2[pos].ch[ch];
ret += trie2[pos].val;
x >>= 1;
}
return ret;
}
ll ans[MAXN];
inline void dfs(int x)
{
forlk(i, trie1[x].head1, nc[i]) Ins(c[i].y, c[i].val);
forlk(i, trie1[x].head2, nq[i]) ans[q[i].id] = query(q[i].c);
if (trie1[x].ch[0]) dfs(trie1[x].ch[0]);
if (trie1[x].ch[1]) dfs(trie1[x].ch[1]);
forlk(i, trie1[x].head1, nc[i]) Ins(c[i].y, -c[i].val);
}
signed main()
{
cin >> n >> m;
ll x, y;
int w;
fore(i, 1, n) cin >> x >> y >> w, InsModify(x, Modify{y, w});
fore(i, 1, m) cin >> x >> y, InsQuery(x, Query{y, i});
dfs(1);
fore(i, 1, m) cout << ans[i] << endl;
return 0;
}
和别人数组大小差不多,但是内存多占了一倍,计算所得应该不会超过140MB,实际却是250MB,疑惑求解释