思路
这道题其实也是一道树状数组模板题,
我们要修改区间 [ l , r ] [l,r] [l,r] 的值,而树状数组不能直接修改。
所以我们考虑差分。
代码
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
long long c[1000010],c2[1000010];
long long n,m,x,y,k;
long long lowbit(long long x)
{
return x&(-x);
}
void query(long long x,long long w)
{
for(; x<=n; x+=lowbit(x))
c[x]+=w;
}
void query2(long long x,long long w)
{
for(; x<=n; x+=lowbit(x))
c2[x]+=w;
}
long long find(long long x)
{
long long ans=0;
for(; x; x-=lowbit(x))
ans+=c[x];
return ans;
}
long long find2(long long x)
{
long long ans=0;
for(; x; x-=lowbit(x))
ans+=c2[x];
return ans;
}
int main()
{
cin>>n>>m;
for(long long i=1; i<=m; i++)
{
scanf("%lld%lld%lld",&k,&x,&y);
if(k==1)
{
query(x,1);
query2(y+1,1); //因为是相减,所以y+1放1
}
else
cout<<find(y)-find2(x)<<endl;
}
return 0;
}