#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = (1<<20);
multiset<ll> S[N];
ll path[N];
ll ans[N];
void modify(int d, int b, bool add) {
if(add) S[d].insert(b);
else S[d].erase(S[d].find(b));
for (; d >= 1; d /= 2){
ll path_childs = (2*d) >= N ? 0LL : max(path[2*d], path[2*d+1]);
ll ans_childs = (2*d) >= N ? 0LL : max(ans[2*d], ans[2*d+1]);
path[d] = 0LL;
if(!S[d].empty())
path[d] = max(0LL, max(path_childs, 0LL) + *prev(S[d].end()));
ans[d] = max(0LL, max(path[d], ans_childs));
}
}
int main(){
int q; cin >> q;
while(q--){
int t, d, b;
cin >> t >> d >> b;
modify(d, b, (t==1));
cout << max(ans[1], 0LL) << endl;
}
}
You saw that $$$-10^{17} \le b_i \le 10^{17}$$$, right?
Damn, I thought about it when I implemented modify() but not in the main function. C++ isn't my main language, thanks for your help
If the Python code is the same but the C++ code doesn't work, then it's likely due to a data type issue. Indeed, you set the variable $$$b$$$ as
intin the input part.Btw I've also been stuck in this problem for a few days because I don't know how to implement the solution.
Thank you, I'll think about it next time I'm stuck