题目
解析
差分?
在所修改的区间的开头位置+1,表示从这个位置开始往后开始埋一种地雷,在结尾位置+1,表示在这个位置有一种地雷被埋完
查询的时候我们就只需要查询
- \([1,r]\)中开头的位置,表示\(1\)到r***埋了多少种类型的地雷
- \([1,l-1]\)中结尾的个数,表示\(1\)到\(l-1\)中有多少种类型的地雷被埋完,因为在\(l\)之前就埋完了,所以不会影响\([l,r]\)内的答案
所以查询时输出前者减后者的差就可以了。
树状数组维护单点修改+区间查询
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e6 + 10;
int n, m;
int ans;
int sta[N], End[N];
inline int lowbit(int x) {
return x & (-x);
}
inline void add_sta(int x) {
while (x <= n) {
++sta[x];
x += lowbit(x);
}
}
inline void add_End(int x) {
while (x <= n) {
++End[x];
x += lowbit(x);
}
}
inline int sum_sta(int x) {
int s = 0;
while (x > 0) {
s += sta[x];
x -= lowbit(x);
}
return s;
}
inline int sum_End(int x) {
int s = 0;
while (x > 0) {
s += End[x];
x -= lowbit(x);
}
return s;
}
signed main() {
cin >> n >> m;
for (int i = 1, x, y, opt; i <= m; ++i) {
cin >> opt >> x >> y;
if (opt == 1) add_sta(x), add_End(y);
else {
ans = sum_sta(y) - sum_End(x - 1);
printf("%d\n", ans);
}
}
return 0;
}