P2184 贪婪大陆
这题也是一个树状数组模板题
不过更有意思 傻逼
应为求一个区间一共有多少个 为了避免树状数组有重合或者有遗漏所以增加一个head 和 tail 数组来存储
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int head[400005],tail[400005],n,m;
int lowbit(int x){
return x&(-x);
}
void addhead(int x){
while(x<=n){
head[x]++;
x+=lowbit(x);
}
}
void addtail(int x){
while(x<=n){
tail[x]++;
x+=lowbit(x);
}
}
int headquery(int x){
int res=0;
while(x>0){
res+=head[x];
x-=lowbit(x);
}
return res;
}
int tailquery(int x){
int res=0;
while(x>0){
res+=tail[x];
x-=lowbit(x);
}
return res;
}
int main() {
cin>>n>>m;
for(int i=1; i<=m; ++i) {
int q,x,y;
cin>>q>>x>>y;
if(q==1) {
addhead(x);
addtail(y);
} else {
cout<<headquery(y)-tailquery(x-1)<<endl;
}
}
return 0;
}
CSP-J-S 2021 RP++!! 倒计时 1 天!!