
不要读歪题了,每个地雷都是不同的,一开始思考的是地雷可能重复,那么这题难度就骤然下降.实际上是在查询区间[L,R][L,R][L,R]包含多少种不同的区间。
考虑查询的区间为:[L,R][L,R][L,R]
考虑区间[l,r][l,r][l,r],如果l<=Rl<=Rl<=R,那么该区间可能与[L,R][L,R][L,R]相交.那么不能相交的情况就是那些r<Lr<Lr<L的情况,需要扣掉这部分的区间
考虑用两个树状数组维护以上信息,第一个维护lll的前缀和,第二个维护rrr的前缀和.
对于每次查询,输出tree1[R]−tree2[L−1]tree1[R]-tree2[L-1]tree1[R]−tree2[L−1]
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+5;
const int INF = 1e9+7;
typedef long long ll;
typedef pair<int,int> pii;
#define all(a) (a).begin(), (a).end()
#define pb(a) push_back(a)
vector<int> G[maxn];
int tr1[maxn],tr2[maxn];
int n,m;
void update(int *tr,int x){
for(int i=x;i<=n;i+=(i&(-i))){
tr[i]++;
}
}
int query(int *tr,int x){
int ans = 0;
for(int i=x;i>0;i-=(i&(-i))){
ans+=tr[i];
}
return ans;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
while(m--){
int op;cin>>op;
if(op==1){
int l,r;cin>>l>>r;
update(tr1,l);
update(tr2,r);
}
else{
int l,r;cin>>l>>r;
cout<<query(tr1,r) - query(tr2,l-1)<<"\n";
}
}
}
本文介绍了一种使用两个树状数组来高效解决区间查询问题的方法。通过维护特定区间的前缀和,可以快速计算出给定区间内不同子区间的数量。文章提供了完整的代码实现,并解释了如何更新和查询这些树状数组。
526

被折叠的 条评论
为什么被折叠?



