luoguP2184 贪婪大陆
当询问区间\([l,r]\)中地雷种类数时,我们只需要用\(r\)前面的区间开头数量减去\(l\)前面的区间结尾数量即可.
原因很简单.
对于询问区间\([L,R]\),我们要求它与先前埋下的地雷区间有交的区间数量,所以只要区间\([l,r]\)的\(l<R\)去掉不合法的\(r<L\)即满足要求.
所以这个题我们可以使用两个树状数组来解决.
\(tree[0]\)维护位置\(x\)之前的区间结尾的数量.
\(tree[1]\)维护位置\(x\)之前的区间开头的数量.
答案即为\(a[1][r]-a[0][l-1]\).
#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define il inline
#define rg register
#define gi read<int>
using namespace std;
typedef long long ll;
const int O = 1e5 + 10;
il int lowbit(int x) { return x & (-x); }
template<class TT>
il TT read() {
TT o = 0,fl = 1; char ch = getchar();
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') fl = -1, ch = getchar();
while (isdigit(ch)) o = o * 10 + ch - '0', ch = getchar();
return fl * o;
}
int n, m, tr[2][O << 1];
il void Modify(int x, int type) {
for (; x <= n; x += lowbit(x))
++ tr[type][x];
}
il int Query(int x, int type) {
int res = 0;
for (; x ; x -= lowbit(x))
res += tr[type][x];
return res;
}
int main() {
n = gi(), m = gi();
for (int i = 1; i <= m; ++i) {
int Q = gi() - 1, l = gi(), r = gi();
if (Q) printf("%d\n", Query(r, 1) - Query(l - 1, 0));
else Modify(r, 0), Modify(l, 1);
}
return 0;
}