LuoguP2184 (树状数组)
注意操作地雷是累加的,不是覆盖。
放置一段地雷的l,r代表了他的开头和结尾。所以用两树状数组维护 1 - r 的l数 减去 1 - l - 1的r数
#include<bits/stdc++.h>
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
#define IOS ios::sync_with_stdio(false) ,cin.tie(0), cout.tie(0);
//#pragma GCC optimize(3,"Ofast","inline")
#define ll long long
#define li __int128_t
//#define int long long
const int N = 1e5 + 5;
const int M = 1e6 + 5;
const int mod = 1e9 + 7;
const ll LNF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
int treel[N], treer[N], n;
int lowbit( int x ) { return x & (- x); }
void updatel(int i) {
for (;i <= n ; i += lowbit(i)) ++ treel[i];
}
void updater(int i) {
for (;i <= n ; i += lowbit(i)) ++ treer[i];
}
int queryl( int i) {
int res = 0;
for (;i ; i -= lowbit(i)) res += treel[i];
return res;
}
int queryr( int i) {
int res = 0;
for (;i ; i -= lowbit(i)) res += treer[i];
return res;
}
int main() {
int m; cin >> n >> m;
while( m -- ) {
int opt, l, r; cin >> opt >> l >> r;
if(opt == 1) {
updatel(l); updater(r);
} else {
cout << queryl(r) - queryr(l - 1) << endl;
}
}
return 0;
}