这题没有前缀和比较简单
想看完整的树状数组+差分求前缀和算法的点这里
上面包含了这题的方法,就不赘述了。。。。👆
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <map>
#include <math.h>
#include <cstring>
#include <string.h>
#include <queue>
#include<list>
#include<cstdarg>
#define ll long long
using namespace std;
ll c[10010000];
ll n;
inline ll lowbit(ll x) { return x & (-x); }
void add(ll x, ll d)
{
while (x <= n)
{
c[x] += d;
x += lowbit(x);
}
}
ll sum(ll x)
{
ll s = 0;
while (x)
{
s += c[x];
x -= lowbit(x);
}
return s;
}
int main()
{
ll t;
cin >> n >> t;
while (t--)
{
ll op;
cin >> op;
if (op == 0)
{
ll t1, t2;
cin >> t1 >> t2;
add(t1, 1);
add(t2 + 1, -1);
}
else
{
ll t1;
cin >> t1;
cout << sum(t1) << '\n';
}
}
}